Created
May 25, 2014 19:26
-
-
Save bmoregeo/e6a09a5e7f65b1f7a903 to your computer and use it in GitHub Desktop.
Choropleth MySQL PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To call this from your php script, insert $kml_output .= getGeo($row['SHAPE']); | |
// or whatever the geometry field in your MYSQL DB table is | |
function getGeo($shape){ | |
// Parse WKT style coordinates to KML style Coordinates | |
$shape = preg_replace("/([0-9.-]+) ([0-9.-]+),*/", "$1,$2,0 ", $shape); | |
// If the Geometry Type is a point parse the geometry type as a point | |
if (preg_match("/^POINT/", $shape)){ | |
$shape = substr($shape, 6); | |
$shape = substr($shape, 0,-1); | |
$kml = "<Point><coordinates>" . $shape . "</coordinates></Point>"; | |
} | |
// If the Geometry Type is a polyline parse the geometry type as a linestring | |
elseif(preg_match("/^LINESTRING/", $shape)){ | |
$shape = substr ($shape, 11); | |
$shape = substr($shape, 0, -1); | |
$kml = "<LineString>" . PHP_EOL . "<extrude>1</extrude>" . PHP_EOL . "<tessellate>1</tessellate>" . "<altitudeMode>relativeToGround</altitudeMode>" . PHP_EOL . "<coordinates>" . substr($shape, 0, -1) . "</coordinates>" . PHP_EOL . "</LineString>" . PHP_EOL; | |
} | |
// If the Geometry Type is a multipolyline parse the geometry type as a multilinestring | |
elseif (preg_match("/^MULTILINESTRING/", $shape)){ | |
$shape = substr($shape, 17); | |
$shape = substr($shape, 0, -2); | |
$polylines = explode("),(", $shape); $kml = "<MultiGeometry>" . PHP_EOL; foreach ($polylines as $polyline) { | |
$kml .= "<LineString>" . PHP_EOL . "<coordinates>" . $polylines[$polyline] . "</coordinates>" . PHP_EOL . "</LineString>" . PHP_EOL; | |
} | |
$kml .= "</MultiGeometry>" . PHP_EOL; | |
} | |
// If the Geometry Type is a polygon parse the geometry type as a polygon | |
elseif (preg_match("/^POLYGON/", $shape)){ | |
$shape = substr ($shape, 9); $shape = substr($shape, 0, -2); | |
$kml = "<Polygon>" . PHP_EOL . "<extrude>1</extrude>" . PHP_EOL . "<altitudeMode>relativeToGround</altitudeMode>" . PHP_EOL . "<outerBoundaryIs><LinearRing><coordinates>" . substr($shape, 0, -1) . "</coordinates></LinearRing></outerBoundaryIs>" . PHP_EOL . "</Polygon>" . PHP_EOL; | |
} | |
// If the Geometry Type is a multipolygon parse the geometry type as a multipolygon | |
elseif (preg_match("/^MULTIPOLYGON/", $shape)){ | |
$shape = substr($shape, 15); | |
$shape = substr($shape, 0, -3); | |
$polygons = explode(")),((", $shape); $kml = "<MultiGeometry>" . PHP_EOL; | |
foreach ($polygons as $polygon) { | |
$kml .= "<Polygon>" . PHP_EOL; $boundary = explode("),(", $polygon); | |
$kml .= "<outerBoundaryIs>" . PHP_EOL . "<LinearRing>" . PHP_EOL . "<coordinates> " . $boundary[0] . "</coordinates>" . PHP_EOL . "</LinearRing>" . PHP_EOL . "</outerBoundaryIs>" . PHP_EOL; | |
for ($i=1; $i < count($boundary); $i++){ | |
$kml .= "<innerBoundaryIs>" . PHP_EOL . "<LinearRing>" . PHP_EOL . "<coordinates> " . $boundary[$i] . "</coordinates>" . PHP_EOL . "</LinearRing>" . PHP_EOL . "</innerBoundaryIs>" . PHP_EOL; | |
} | |
$kml .= "</Polygon>" . PHP_EOL; | |
} | |
$kml .= "</MultiGeometry>" . PHP_EOL; | |
} return $kml; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment