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.


In reply to Geo::ShapeFile and Projection Conversion (floating point troubles?) by ptum

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.