penny has asked for the wisdom of the Perl Monks concerning the following question:

I have just discovered Geo::ShapeFile module that gives perl the means to access the geometry of Esri shapefiles. Although I have been successful in getting the x and y co-ords I have not been able to get the z (height) values from my shapefile.

Anyone else using this module and experience the same problem ??

Below is the code I wrote to get the x,y co-ords and z (height) values for a test shapefile.
use Geo::ShapeFile::Point comp_includes_z=>0; use Geo::ShapeFile; #reads the "test3D" shapefile layer. $shapefile = new Geo::ShapeFile("test3D"); #loops throught each shapefile and gets the shape #for each record for (1 ..$shapefile->shapes){ $shape = $shapefile->get_shp_record($_); #puts the points into an array of hashes @point = $shape->points; #for each point get the x,y co-ords and the #(hieght) values and print foreach $p (@point){ $x=$p->X; $y=$p->Y; $z=$p->Z; print "$x, $y, $z \n"; } }

Any help is greatly appricated.
Penny

Replies are listed 'Best First'.
Re: Geo::ShapeFile
by jasonk (Parson) on May 13, 2005 at 18:10 UTC

    Hmm, I thought I had responded to this already, but apparently not. For anyone else stumbling across this, penny has found a pretty big bug in Geo::ShapeFile that prevents the Z and M values from being imported from some types of shapes. Using the sample data she provided a fix was created and has been uploaded to CPAN as version 2.51


    We're not surrounded, we're in a target-rich environment!
Re: Geo::ShapeFile
by kvale (Monsignor) on May 11, 2005 at 03:47 UTC
    I believe that the comp_includes_z=>0 option explicitly excludes the Z coordinate (set include to 0 means exclude). Get rid of it.

    -Mark

      Removed the offending piece of code, unfortunately still does not seem to read the z values. :-(

      In this particular code, comp_includes_z doesn't have any function. All that does is indicate that when comparing two points you want to exclude the Z value from the comparison. For example:

      use Geo::ShapeFile::Point comp_includes_z => 0; my $p1 = Geo::ShapeFile::Point->new(X => 3, Y => 5, Z => 10); my $p2 = Geo::ShapeFile::Point->new(X => 3, Y => 5, Z => 25); # this will return false if comp_includes_z is true, # and return true if comp_includes_z is false if($p1 == $p2) { print "these points are the same spot\n"; }

      We're not surrounded, we're in a target-rich environment!
Re: Geo::ShapeFile
by jasonk (Parson) on May 11, 2005 at 13:43 UTC

    First of all, it's gratifying to find people on my favorite Perl site actually using one of my modules, it gives me more motivation to work on some of the enhancements I've wanted to add for a while.

    Secondly, are you sure that your test datafile actually contains Z-values? I've found that this is the most common cause of people having this type of problem. Try printing out the values for $shapefile->z_min and $shapefile->z_max to see what the range of values indicated in the shapefile header is, and if the range looks valid based on what you believe the shapefile contains. I tried your code using a test shapefile that I know contains Z-values, and it worked just fine (if you want to try it out, the datafile I used is called masspntz, and is included in the Geo::ShapeFile distribution in the t/test_data directory.)

    Finally, could you let me know what version of Geo::ShapeFile you are using, and what platform you are using it on? I'm also curious where your shapefile comes from, as there are some shapefile tools that generate very strange output. If you are still having troubles feel free to email me a copy of your shapefile and I'll be happy to see if I can reproduce your problem.


    We're not surrounded, we're in a target-rich environment!
      I stumbled across the Geo::Shape module on a web page for creating svg
      from shapefiles using perl.

      http://www.webmapper.net/svg/create/

      It is really exciting to have the means in perl to access the geometry of shapefiles.
      Thanks very much.

      I am certain the layer has Z values. When the above code was
      adjusted to read the zmax and zmin values. as suggested, it worked.
      Curiously when I tried masspntz with the code the z values were read
      successfully.

      The environment I am running the script is cygwin. ( I can’t have a Unix
      account at work ) and the module version is 0.01

      My shapfile is from Arcgis and is a drainage layer polylineZM layer.

      I will e-mail you the shapefile thanks very much for looking at this.

      Penny
Re: Geo::ShapeFile
by rupesh (Hermit) on May 11, 2005 at 06:56 UTC
    Just curious though, what does  comp_includes_z=>1; do?

    I had a look at the Module, and you seem to have  z_min() and the z_max() functions.
    Probably that would help.


    Cheers,
    Rupesh.