phpmapscript attributes from ShapefileObj

Steven Monai stevem at SPATIALMAPPING.COM
Tue Aug 23 13:51:03 EDT 2005


Dylan:

You can obtain a shape's attributes directly from the dbf file with
PHP's 'dBase' extension. The shapes in a shp file correspond one-to-one
with the records in the dbf file, so once you know the index of the shape
you want, the index to its attribute record in the dbf file is easily
derived. The shapefile record array is zero-based, while the dBase record
array is 1-based (in PHP, anyway), so just add one to the shape index to
get the dBase index.

Here's a quick'n'dirty example (no guarantee it'll work without
modification, since I just banged it out here without any testing):

/* load extensions as/if necessary */
dl("php_mapscript.dll");
dl("php_dbase.dll");

/* Configuration variables */
$shpfile_name = "/path/to/myfile";  // shapefile pathname, without .shp
$shpfile_mode = -1;                 // -1 == read-only, -2 == update
$dbase_mode = 0;                    // 0 == read-only, 1 == read/write

/* Open the .shp file */
$shape_file = ms_newShapefileObj($shpfile_name, $shpfile_mode);

/* Open the .dbf file */
$dbase_file = dbase_open($shpfile_name . ".dbf", $dbase_mode);

/* Code here to determine the index of the shape you want */
$shape_index = $shape_file->numshapes - 1;  // this is the last shape

/* Obtain the shape's corresponding dBase record index
$dbase_index = $shape_index + 1;  // adjust for the dBase 1-based index

/* Obtain the shape's attributes as an associative array */
$attrs_array = dbase_get_record_with_names($dbase_file, $dbase_index);

/* Do stuff with the attributes */
echo "ID=" . $attrs_array['ID'] . "\n";  // assume an 'ID' field exists

/* Clean up */
dbase_close($dbase_file); $dbase_file = NULL;
$shape_file->free();      $shape_file = NULL;


Hope this helps,
-SM
--


On Mon, 22 Aug 2005 15:42:07 -0700, Dylan Beaudette <dylan at IICI.NO-IP.ORG>
wrote:

>Greetings,
>
>Currently I am only able to get attributes for a shape stored within a
>shapfile through a 2 step process:
>
>1. lookup a shape based on the 'contains' method and a for() loop, giving
me a
>shape index:
>$num_shapes = $shapefile->numshapes;
>for($i=0; $i < $num_shapes ; $i++)
>    {
>    //now extract a single shape
>    $this_shape= $shapefile->getShape($i);
>
>    //test to see if our point is within the polygon SSURGO bound
>    $is_point_in_poly = $this_shape->contains($user_point);
>
>    //test for a positive match ($var == 1)
>    if($is_point_in_poly == 1)
>      {
>      //look-up the associated survey name from the shape index
>      //can only use that method if getShape is called from a layer
object...
>      $found_matching_survey_area = true;
>      break;    //and break loop
>      }
>
>      } //end for loop
>
>
>2. open a MAP file, open the layer that cooresponds to the previously
openend
>shape file, and lookup the attributes associated with a shape id:
>$a_layer = $map->getLayerByName('survey-areas');
>  $a_layer ->open();
>  $a_shape = $a_layer ->getShape(-1, $index);
>  $a_shape _attribute= $a_shape ->getvalue($a_layer, 'areasymbol');
>  $a_layer ->close();
>
>While this works, it is a little bit awkward - but here is the logic:
>1. i am only able to get the number of shapes in a shapfile object...
doesn't
>seem to be a method numShapes method for a layer object
>2. i am only able to lookup values (attributes) for a shape when getShape
is
>called from a layer object.
>3. there fore i have to first get a shape id via a shapefile object, then
>lookup the attributes for that shape id via a layer object.
>
>is there a cleaner way to do this, possibly without having to create a MAP
>file?
>
>thanks in advance,
>
>--
>Dylan Beaudette
>Soils and Biogeochemistry Graduate Group
>University of California at Davis
>530.754.7341



More information about the mapserver-users mailing list