whakka has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Need an intelligent join algorithm for matching coordinates to shapefiles (.shp)
by Joost (Canon) on Dec 17, 2008 at 19:59 UTC | |
Re: Need an intelligent join algorithm for matching coordinates to shapefiles (.shp)
by BrowserUk (Patriarch) on Dec 17, 2008 at 19:47 UTC | |
Re: Need an intelligent join algorithm for matching coordinates to shapefiles (.shp)
by kennethk (Abbot) on Dec 17, 2008 at 19:55 UTC | |
by whakka (Hermit) on Dec 17, 2008 at 20:01 UTC | |
by kennethk (Abbot) on Dec 17, 2008 at 20:20 UTC | |
by belg4mit (Prior) on Dec 22, 2008 at 05:30 UTC |