Using Geo::ShapeFile, basically what the title says - I have a long list of coordinates (~300K) that need to be matched to Census blocks (coverage is the entire US). The block information resides in a number of shapefiles - one for each county in each state, where each internal shape is a block.

Here's a naive nested loop implementation:

for my $shp ( @shapefiles ) { # Open the shapefile for processing my $shapefile = new Geo::ShapeFile($shp); # Point object for (x,y) address coordinate my $point = new Geo::ShapeFile::Point(X => $address->{'long'}, Y = +> $address->{'lat'}); if ( $shapefile->bounds_contains_point($point) ) { # Get data if the point falls inside this file for my $n ( 1..$shapefile->shapes() ) { my $shape = $shapefile->get_shp_record($n); if ( $shape->contains_point($point) ) { my %dbf = $shapefile->get_dbf_record($n); return \%dbf; } } } } # next shapefile

The bounds_contains_point method simply checks if the shapefile's max and min x,y values form a rectangle that contains the given point, so the same point could iterate through multiple shapefiles before finding its corresponding block ($shape->contains_point($point) actually does the leg work to see if the point falls inside).

I could potentially create a file with the centroid of each block(shape) mapping to its corresponding shapefile and the index of the shape within. Then I iterate through the centroids with minimum distance to the given point until I find the block(shape) that contains the given point. The centroids would have to be ordered or partitioned somehow so I wouldn't compute distances for each <point,centroid> combination. How you ask? That's a good question.

Of course it's more than likely this has been optimized before so if anyone knows about this I'd be grateful. Please don't point me to software like ArcGIS or Open Jump since I have access to these tools already and they don't offer an efficient solution. Otherwise, do comment!


In reply to Need an intelligent join algorithm for matching coordinates to shapefiles (.shp) by whakka

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.