Build shapefiles with PHP/MapScript
Lowell Filak
lfilak at MEDINACO.ORG
Mon Aug 30 05:44:18 PDT 2004
If it is actually producing a shapefile is it just a matter of latitude
& longitude being backwards ("latitude" = "ladder" rungs)?
Lowell
The following message was sent by G-Net Consulting
<joelf at GNETCONSULTING.COM> on Sun, 29 Aug 2004 17:55:15 -0400.
> Hello list!
>
> I'm new to MapServer, and, with the gmap example code,
> have built the map I'm looking for.
>
> Now I'm looking to build my own shapefile, based on the
> latitude/longitude of data in a MySQL db. The data is
> pulled from a monthly update published via CD. So I think
> the best way is to "simply" build the shapefile when I
> update the db, instead of querying everytime for data
> that's not changing.
>
> I'm having a problem finishing the code. My data is currently
> stored like the following (taken from the DBF file built):
>
> =============================================
> ID,State,Location Name,Serial #,Latitude,Longitude
> 5938, AK, CHUGIAK, NG 943265, 612701, 1491918
> 6120, AK, ANCHORAGE, NG 954376, 611341, 1494241
> 6125, AK, MULDOON, NG 954377, 611354, 1494234
> 6121, AK, FORT RICHARDSON, NG 954378, 611425, 1494110
> 6122, AK, FORT RICHARDSON, NG 954379, 611646, 1493744
> =============================================
>
> I was trying to use the code from the MapScript PHP example
> to do it. Here's a snippet:
>
> =============================================
> // produce shapefile
> function createPoint($longitude, $latitude, $plotID, $state, $plotName,
> $serialNum) {
> GLOBAL $shpFile, $dbfFile;
>
> // create our point
> $oShp = ms_newShapeObj(MS_SHP_POINT);
> $newPoint = ms_newPointObj();
> $newPoint->setXY($latitude, $longitude);
> $oLine = ms_newLineObj();
> $oLine->addXY($latitude, $longitude);
> $oShp->add($oLine);
> $shpFile->addShape($oShp);
>
> // Write attribute record
> dbase_add_record($dbfFile, array($plotID, $state, $plotName,
> $serialNum, $latitude, $longitude));
> }
>
> // create our array of dbase columns to write
> $arrFields = array(array("ID", "N", 4, 0),
> array("XSC", "C", 4),
> array("RAL", "C", 24),
> array("SER", "C", 10),
> array("XLA", "C", 6),
> array("XLG", "C", 7));
>
> // set our variables to save our files as
> $shpFname = "/var/www/html/gmf/data/gmf";
> $shpFile = ms_newShapeFileObj($shpFname, MS_SHP_POINT);
> $dbfFile = dbase_create("/var/www/html/gmf/data/gmf.dbf", $arrFields);
>
> // loop through our query, and process accordingly
> while ($qryRow = mysql_fetch_array($qryPlots)) {
> // create compliant lat/long values
> $latitude = substr($qryRow["XLA"], 0, strlen($qryRow["XLA"]) - 1);
> $longitude = substr($qryRow["XLG"], 0, strlen($qryRow["XLG"]) - 1);
> if (substr($longitude, 0, 1) == 0) {
> $longitude = substr($longitude, 1, strlen($qryRow["XLG"]));
> }
> // create our points
> createPoint($longitude, $latitude, $qryRow["ID"], $qryRow["XSC"],
> $qryRow["RAL"], $qryRow["SER"]);
> }
> =============================================
>
> I need to get those sensors to display on my map. A shapefile is created,
> but WAY not what I need it to be.
>
> Any help would be appreciated. Thanks for your time.
>
> Joel
>
> Details:
> PHP 4.3.8 (cgi)
> MapServer version 4.2.1 OUTPUT=PNG OUTPUT=JPEG OUTPUT=WBMP OUTPUT=PDF
> SUPPORTS=PROJ SUPPORTS=FREETYPE SUPPORTS=WMS_SERVER SUPPORTS=WMS_CLIENT
> SUPPORTS=WFS_SERVER SUPPORTS=WFS_CLIENT INPUT=OGR INPUT=GDAL
INPUT=SHAPEFILE
> Linux Red Hat 9 (Fedora)
More information about the MapServer-users
mailing list