OGR
Gregor Mosheh
gregor at HOSTGIS.COM
Tue Dec 19 07:08:19 PST 2006
Hi, Andreas.
Yeah, I found the OGR documentation very dense too -- then again, the OGR
stuff is for C instead of PHP, so it takes a little imagination and
experimentation to map it over. Here are a few functions I whipped up a
few months ago, which should get you started.
The first just opens a data source and returns the spatial extent....
function find_extent($shapefilename) {
// open the data source
// cheesy: we iterate over drivers 'til we find the one we know is right
OGRRegisterAll();
$numDrivers = OGRGetDriverCount();
for ($i=0; $i<$numDrivers; $i++) {
$driver = OGRGetDriver($i);
if (OGR_Dr_GetName($driver) == "ESRI Shapefile" ) { break; }
}
$datasource = OGR_Dr_Open($driver, $shapefilename, NULL);
// fetch the layer and its extent
$layer = OGR_DS_GetLayer($datasource,0);
OGR_L_GetExtent($layer,$extent,1);
return $extent;
}
This second one is more complex. It takes an array of associative arrays,
each one representing a point. It builds a pair of shapefiles, the sample
points and also lines connecting the points. We wrote this for a vehicle
tracking system.
function create_data_sources($basename,$points) {
global $TEMPDIR;
$shapefilename_points = "$TEMPDIR/points_$basename.shp";
$shapefilename_tracks = "$TEMPDIR/tracks_$basename.shp";
// fetch the driver we want: shapefile
// sadly, fetching a driver by name is rather roundabout
OGRRegisterAll();
$numDrivers = OGRGetDriverCount();
for ($i=0; $i<$numDrivers; $i++) {
$driver = OGRGetDriver($i);
if (OGR_Dr_GetName($driver) == "ESRI Shapefile" ) { break; }
}
// delete any existing data source by this name, in preparation for
overwriting
@unlink($shapefilename_points);
@unlink(basename($shapefilename_points,'.shp').'.shx');
@unlink(basename($shapefilename_points,'.shp').'.dbf');
@unlink($shapefilename_tracks);
@unlink(basename($shapefilename_tracks,'.shp').'.shx');
@unlink(basename($shapefilename_tracks,'.shp').'.dbf');
// fetch the SRS reference.
// we always use latlong NAD83, EPSG:4326 so "nothing" is fine for us.
$srs = NULL;
// create the target data sources, then create a layer in each source
$points_file = OGR_Dr_CreateDataSource($driver, $shapefilename_points,
NULL);
$tracks_file = OGR_Dr_CreateDataSource($driver, $shapefilename_tracks,
NULL);
// create the layer definition for the points file
$points_layer = OGR_DS_CreateLayer($points_file, 'points', $srs,
wkbPoint, NULL);
$field = OGR_Fld_Create( "POINTID", OFTInteger );
OGR_L_CreateField($points_layer, $field, 0);
$field = OGR_Fld_Create( "DIRECTION", OFTString );
OGR_L_CreateField($points_layer, $field, 0);
$points_layerdefn = OGR_L_GetLayerDefn($points_layer);
// create the layer definition for the tracks file
$tracks_layer = OGR_DS_CreateLayer($tracks_file, 'tracks', $srs,
wkbLineString, NULL);
$tracks_layerdefn = OGR_L_GetLayerDefn($tracks_layer);
// create a single line in the tracks file
$track_feature = OGR_F_Create($tracks_layerdefn);
$track_geom = OGR_G_CreateGeometry(wkbLineString);
// iterate through the supplied points, stuffing them into the points
and also forming a track
global $ZOOMMULTIPLIER;
for ($i=0; $i<sizeof($points); $i++) {
// figure out this point's relation to the next point; northeast,
south, etc.
$points[$i]['x'] = (float) $points[$i]['x'];
$points[$i]['y'] = (float) $points[$i]['y'];
$direction = '';
if ($i < sizeof($points)-1) {
$xdiff = $points[$i+1]['x'] - $points[$i]['x'];
$ydiff = $points[$i+1]['y'] - $points[$i]['y'];
if ($ydiff >= $ZOOMMULTIPLIER/10) { $direction .= 'N'; }
elseif ($ydiff <= -$ZOOMMULTIPLIER/10) { $direction .= 'S'; }
if ($xdiff >= $ZOOMMULTIPLIER/10) { $direction .= 'E'; }
elseif ($xdiff <= -$ZOOMMULTIPLIER/10) { $direction .= 'W'; }
}
// add the point to the points file
$point_feature = OGR_F_Create($points_layerdefn);
OGR_F_SetFieldInteger( $point_feature, 0, $points[$i]['id']);
OGR_F_SetFieldString( $point_feature, 1, $direction);
$point_geom = OGR_G_CreateGeometry(wkbPoint);
OGR_G_SetPoint($point_geom, 0, $points[$i]['x'], $points[$i]['y'], 0);
OGR_F_SetGeometry($point_feature, $point_geom);
OGR_L_CreateFeature( $points_layer, $point_feature );
// add the point to the track
OGR_G_AddPoint($track_geom, $points[$i]['x'], $points[$i]['y'], 0);
}
// go ahead and terminate the line
OGR_F_SetGeometry($track_feature, $track_geom);
OGR_L_CreateFeature($tracks_layer, $track_feature );
// done, close the data source
OGR_DS_Destroy($points_file);
OGR_DS_Destroy($tracks_file);
return true;
}
More information about the MapServer-users
mailing list