ptum has asked for the wisdom of the Perl Monks concerning the following question:
I've recently been asked to parse a collection of ESRI shape files to determine whether a given point falls within them.
"What luck!" I chortled, as I happened across jasonk's module.
Grabbing a sample shape file, I slapped together the example shown below.
The problem is, the X and Y point coordinates that I received seem to be wildly divergent from my expectations. Am I experiencing some kind of weird floating point problem, or do I need to convert my point values from some kind of projection? I was hoping for X and Y coordinates in terms of latitudinal and longitudinal degrees; solid, stable values like 44.234112 and -70.43101 -- the kind of numbers you wouldn't be ashamed to have over for dinner. Instead, I get numbers that I would be afraid to meet in a dark alley.
The contents of the .prj file (which seems to define the projection) are as follows:
GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROI +D["GRS_1980",6378137,298.257222101]],PRIMEM["Greenwich",0],UNIT["Degr +ee",0.017453292519943295]]
Here's the code I've slapped together:
#!/usr/local/bin/perl -w use strict; use Geo::ShapeFile; use Geo::ShapeFile::Point comp_includes_m => 0, comp_includes_z => 0; my $shapefile = new Geo::ShapeFile("/my/shape/dir/shape_group"); for(1 .. $shapefile->shapes()) { my $shape = $shapefile->get_shp_record($_); print "===============================================\n"; print "ID: ", $shape->shape_id(), "\n"; print "Centroid: ", $shape->centroid(), "\n"; print "Parts: ", $shape->num_parts(), "\n"; print "Points: ", $shape->num_points(), "\n"; print "Type: ", $shape->shape_type(), "\n"; print "X min: ", $shape->x_min(), "\n"; print "Y min: ", $shape->y_min(), "\n"; print "X max: ", $shape->x_max(), "\n"; print "Y max: ", $shape->y_max(), "\n"; print "Height: ", $shape->height(), "\n"; print "Width: ", $shape->width(), "\n"; my @points = $shape->corners(); foreach my $this_corner (@points) { print "Corner X: ", $this_corner->X(), "\n"; print "Corner Y: ", $this_corner->Y(), "\n"; } print "===============================================\n"; }
... and here's the output:
=============================================== ID: 1 Centroid: Point(X=-1.36971256117202e+301,Y=5.19769721893395e+296) Parts: 1 Points: 189 Type: 5 X min: 3.99649220734208e-297 Y min: -6.04919389741984e-286 X max: 5.7228212357179e-306 Y max: 1.59100403324442e+218 Height: -3.99649220161926e-297 Width: 1.59100403324442e+218 Corner X: 3.99649220734208e-297 Corner Y: -6.04919389741984e-286 Corner X: 5.7228212357179e-306 Corner Y: -6.04919389741984e-286 Corner X: 5.7228212357179e-306 Corner Y: 1.59100403324442e+218 Corner X: 3.99649220734208e-297 Corner Y: 1.59100403324442e+218 ===============================================
I'm using version 2.51 of Geo::ShapeFile. Any insight that the monks can provide would be very much appreciated.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Geo::ShapeFile and Projection Conversion (floating point troubles?)
by GrandFather (Saint) on Sep 20, 2006 at 01:40 UTC | |
by jasonk (Parson) on Sep 20, 2006 at 14:44 UTC | |
|
Re: Geo::ShapeFile and Projection Conversion (floating point troubles?)
by jasonk (Parson) on Sep 20, 2006 at 14:39 UTC | |
by ptum (Priest) on Sep 20, 2006 at 15:07 UTC |