From 1e8914fe508d06774f8bf4159587db35cb24b3ef Mon Sep 17 00:00:00 2001 From: Madhura Jayaratne Date: Thu, 21 Jul 2011 20:19:24 +0530 Subject: [PATCH] Avoid code duplication --- libraries/gis/pma_gis_geometry.php | 40 ++++++++++++++++++++++++++ libraries/gis/pma_gis_multipolygon.php | 28 +----------------- libraries/gis/pma_gis_polygon.php | 31 ++------------------ 3 files changed, 44 insertions(+), 55 deletions(-) diff --git a/libraries/gis/pma_gis_geometry.php b/libraries/gis/pma_gis_geometry.php index a09d30d269..8decdcc42e 100644 --- a/libraries/gis/pma_gis_geometry.php +++ b/libraries/gis/pma_gis_geometry.php @@ -156,5 +156,45 @@ abstract class PMA_GIS_Geometry return $points_arr; } + + /** + * Generates JavaScriipt for adding points for OpenLayers polygon. + * + * @param string $polygon points of a polygon in WKT form + * @param string $srid spatial reference id + * + * @return JavaScriipt for adding points for OpenLayers polygon + */ + protected function addPointsForOpenLayersPolygon($polygon, $srid) + { + $row = 'new OpenLayers.Geometry.Polygon(new Array('; + // If the polygon doesnt have an inner polygon + if (strpos($polygon, "),(") === false) { + $points_arr = $this->extractPoints($polygon, null); + $row .= 'new OpenLayers.Geometry.LinearRing(new Array('; + foreach ($points_arr as $point) { + $row .= '(new OpenLayers.Geometry.Point(' . $point[0] . ', ' . $point[1] . '))' + . '.transform(new OpenLayers.Projection("EPSG:' . $srid . '"), map.getProjectionObject()), '; + } + $row = substr($row, 0, strlen($row) - 2); + $row .= '))'; + } else { + // Seperate outer and inner polygons + $parts = explode("),(", $polygon); + foreach ($parts as $ring) { + $points_arr = $this->extractPoints($ring, null); + $row .= 'new OpenLayers.Geometry.LinearRing(new Array('; + foreach ($points_arr as $point) { + $row .= '(new OpenLayers.Geometry.Point(' . $point[0] . ', ' . $point[1] . '))' + . '.transform(new OpenLayers.Projection("EPSG:' . $srid . '"), map.getProjectionObject()), '; + } + $row = substr($row, 0, strlen($row) - 2); + $row .= ')), '; + } + $row = substr($row, 0, strlen($row) - 2); + } + $row .= ')), '; + return $row; + } } ?> diff --git a/libraries/gis/pma_gis_multipolygon.php b/libraries/gis/pma_gis_multipolygon.php index 9be480137a..db6f6cbb4a 100644 --- a/libraries/gis/pma_gis_multipolygon.php +++ b/libraries/gis/pma_gis_multipolygon.php @@ -257,33 +257,7 @@ class PMA_GIS_Multipolygon extends PMA_GIS_Geometry . 'new OpenLayers.Geometry.MultiPolygon(new Array('; foreach ($polygons as $polygon) { - $row .= 'new OpenLayers.Geometry.Polygon(new Array('; - // If the polygon doesnt have an inner polygon - if (strpos($polygon, "),(") === false) { - $points_arr = $this->extractPoints($polygon, null); - $row .= 'new OpenLayers.Geometry.LinearRing(new Array('; - foreach ($points_arr as $point) { - $row .= '(new OpenLayers.Geometry.Point(' . $point[0] . ', ' . $point[1] . '))' - . '.transform(new OpenLayers.Projection("EPSG:' . $srid . '"), map.getProjectionObject()), '; - } - $row = substr($row, 0, strlen($row) - 2); - $row .= '))'; - } else { - // Seperate outer and inner polygons - $parts = explode("),(", $polygon); - foreach ($parts as $ring) { - $points_arr = $this->extractPoints($ring, null); - $row .= 'new OpenLayers.Geometry.LinearRing(new Array('; - foreach ($points_arr as $point) { - $row .= '(new OpenLayers.Geometry.Point(' . $point[0] . ', ' . $point[1] . '))' - . '.transform(new OpenLayers.Projection("EPSG:' . $srid . '"), map.getProjectionObject()), '; - } - $row = substr($row, 0, strlen($row) - 2); - $row .= ')), '; - } - $row = substr($row, 0, strlen($row) - 2); - } - $row .= ')), '; + $row .= $this->addPointsForOpenLayersPolygon($polygon, $srid); } $row = substr($row, 0, strlen($row) - 2); $row .= ')), null, ' . json_encode($style_options) . '));'; diff --git a/libraries/gis/pma_gis_polygon.php b/libraries/gis/pma_gis_polygon.php index cc96f9a214..ce8ffa8cf7 100644 --- a/libraries/gis/pma_gis_polygon.php +++ b/libraries/gis/pma_gis_polygon.php @@ -234,34 +234,9 @@ class PMA_GIS_Polygon extends PMA_GIS_Geometry // Trim to remove leading 'POLYGON((' and trailing '))' $polygon = substr($spatial, 9, (strlen($spatial) - 11)); - $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(' - . 'new OpenLayers.Geometry.Polygon(new Array('; - // If the polygon doesnt have an inner polygon - if (strpos($polygon, "),(") === false) { - $points_arr = $this->extractPoints($polygon, null); - $row .= 'new OpenLayers.Geometry.LinearRing(new Array('; - foreach ($points_arr as $point) { - $row .= '(new OpenLayers.Geometry.Point(' . $point[0] . ', ' . $point[1] . '))' - . '.transform(new OpenLayers.Projection("EPSG:' . $srid . '"), map.getProjectionObject()), '; - } - $row = substr($row, 0, strlen($row) - 2); - $row .= '))'; - } else { - // Seperate outer and inner polygons - $parts = explode("),(", $polygon); - foreach ($parts as $ring) { - $points_arr = $this->extractPoints($ring, null); - $row .= 'new OpenLayers.Geometry.LinearRing(new Array('; - foreach ($points_arr as $point) { - $row .= '(new OpenLayers.Geometry.Point(' . $point[0] . ', ' . $point[1] . '))' - . '.transform(new OpenLayers.Projection("EPSG:' . $srid . '"), map.getProjectionObject()), '; - } - $row = substr($row, 0, strlen($row) - 2); - $row .= ')), '; - } - $row = substr($row, 0, strlen($row) - 2); - } - $row .= ')), null, ' . json_encode($style_options) . '));'; + $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('; + $row .= $this->addPointsForOpenLayersPolygon($polygon, $srid); + $row .= 'null, ' . json_encode($style_options) . '));'; return $row; }