Permalink
Browse files

Code refactoring to remove some duplications

  • Loading branch information...
madhuracj committed Apr 14, 2012
1 parent 8e62b92 commit 10727d665becaf9cc59080879d40b8d243851a88
@@ -97,10 +97,12 @@
*/
protected function getBoundsForOl($srid, $scale_data)
{
- return 'bound = new OpenLayers.Bounds(); bound.extend(new OpenLayers.LonLat('
+ return 'bound = new OpenLayers.Bounds(); '
+ . 'bound.extend(new OpenLayers.LonLat('
. $scale_data['minX'] . ', ' . $scale_data['minY']
. ').transform(new OpenLayers.Projection("EPSG:'
- . $srid . '"), map.getProjectionObject())); bound.extend(new OpenLayers.LonLat('
+ . $srid . '"), map.getProjectionObject())); '
+ . 'bound.extend(new OpenLayers.LonLat('
. $scale_data['maxX'] . ', ' . $scale_data['maxY']
. ').transform(new OpenLayers.Projection("EPSG:'
. $srid . '"), map.getProjectionObject()));';
@@ -215,49 +217,121 @@ protected function extractPoints($point_set, $scale_data, $linear = false)
return $points_arr;
}
+ /**
+ * Generates JavaScript for adding an array of polygons to OpenLayers.
+ *
+ * @param array $polygons x and y coordinates for each polygon
+ * @param string $srid spatial reference id
+ *
+ * @return string JavaScript for adding an array of polygons to OpenLayers
+ * @access protected
+ */
+ protected function getPolygonArrayForOpenLayers($polygons, $srid)
+ {
+ $ol_array = 'new Array(';
+ foreach ($polygons as $polygon) {
+ $rings = explode("),(", $polygon);
+ $ol_array .= $this->getPolygonForOpenLayers($rings, $srid) . ', ';
+ }
+ $ol_array = substr($ol_array, 0, strlen($ol_array) - 2);
+ $ol_array .= ')';
+
+ return $ol_array;
+ }
+
/**
* Generates JavaScript for adding points for OpenLayers polygon.
*
- * @param string $polygon points of a polygon in WKT form
+ * @param array $polygon x and y coordinates for each line
* @param string $srid spatial reference id
*
* @return string JavaScript for adding points for OpenLayers polygon
* @access protected
*/
- protected function addPointsForOpenLayersPolygon($polygon, $srid)
+ protected function getPolygonForOpenLayers($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);
+ return 'new OpenLayers.Geometry.Polygon('
+ . $this->getLineArrayForOpenLayers($polygon, $srid, false)
+ . ')';
+ }
+
+ /**
+ * Generates JavaScript for adding an array of LineString
+ * or LineRing to OpenLayers.
+ *
+ * @param array $lines x and y coordinates for each line
+ * @param string $srid spatial reference id
+ * @param bool $is_line_string whether it's an array of LineString
+ *
+ * @return string JavaScript for adding an array of LineString
+ * or LineRing to OpenLayers
+ * @access protected
+ */
+ protected function getLineArrayForOpenLayers($lines, $srid, $is_line_string = true)
+ {
+ $ol_array = 'new Array(';
+ foreach ($lines as $line) {
+ $points_arr = $this->extractPoints($line, null);
+ $ol_array .= $this->getLineForOpenLayers($points_arr, $srid, $is_line_string) . ', ';
+ }
+ $ol_array = substr($ol_array, 0, strlen($ol_array) - 2);
+ $ol_array .= ')';
+
+ return $ol_array;
+ }
+
+ /**
+ * Generates JavaScript for adding a LineString or LineRing to OpenLayers.
+ *
+ * @param array $points_arr x and y coordinates for each point
+ * @param string $srid spatial reference id
+ * @param bool $is_line_string whether it's a LineString
+ *
+ * @return string JavaScript for adding a LineString or LineRing to OpenLayers
+ * @access protected
+ */
+ protected function getLineForOpenLayers($points_arr, $srid, $is_line_string = true)
+ {
+ return 'new OpenLayers.Geometry.'
+ . ($is_line_string ? 'LineString' : 'LinearRing') . '('
+ . $this->getPointsArrayForOpenLayers($points_arr, $srid)
+ . ')';
+ }
+
+ /**
+ * Generates JavaScript for adding an array of points to OpenLayers.
+ *
+ * @param array $points_arr x and y coordinates for each point
+ * @param string $srid spatial reference id
+ *
+ * @return string JavaScript for adding an array of points to OpenLayers
+ * @access protected
+ */
+ protected function getPointsArrayForOpenLayers($points_arr, $srid)
+ {
+ $ol_array = 'new Array(';
+ foreach ($points_arr as $point) {
+ $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', ';
}
- $row .= ')), ';
- return $row;
+ $ol_array = substr($ol_array, 0, strlen($ol_array) - 2);
+ $ol_array .= ')';
+
+ return $ol_array;
+ }
+
+ /**
+ * Generates JavaScript for adding a point to OpenLayers.
+ *
+ * @param array $point array containing the x and y coordinates of the point
+ * @param string $srid spatial reference id
+ *
+ * @return string JavaScript for adding points to OpenLayers
+ * @access protected
+ */
+ protected function getPointForOpenLayers($point, $srid)
+ {
+ return '(new OpenLayers.Geometry.Point(' . $point[0] . ', ' . $point[1] . '))'
+ . '.transform(new OpenLayers.Projection("EPSG:' . $srid . '"), map.getProjectionObject())';
}
}
?>
@@ -202,18 +202,9 @@ public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data
$linesrting = substr($spatial, 11, (strlen($spatial) - 12));
$points_arr = $this->extractPoints($linesrting, null);
- $row = '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 .= ')';
-
$result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
- . 'new OpenLayers.Geometry.LineString(' . $row . '), null, '
- . json_encode($style_options) . '));';
+ . $this->getLineForOpenLayers($points_arr, $srid)
+ . ', null, ' . json_encode($style_options) . '));';
return $result;
}
@@ -233,20 +233,9 @@ public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data
$linestirngs = explode("),(", $multilinestirng);
$row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
- . 'new OpenLayers.Geometry.MultiLineString(new Array(';
- foreach ($linestirngs as $linestring) {
- $points_arr = $this->extractPoints($linestring, null);
- $row .= 'new OpenLayers.Geometry.LineString(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) . '));';
+ . 'new OpenLayers.Geometry.MultiLineString('
+ . $this->getLineArrayForOpenLayers($linestirngs, $srid)
+ . '), null, ' . json_encode($style_options) . '));';
return $row;
}
@@ -204,22 +204,10 @@ public function prepareRowAsOl($spatial, $srid, $label, $point_color, $scale_dat
$multipoint = substr($spatial, 11, (strlen($spatial) - 12));
$points_arr = $this->extractPoints($multipoint, null);
- $row = 'new Array(';
- foreach ($points_arr as $point) {
- if ($point[0] != '' && $point[1] != '') {
- $row .= '(new OpenLayers.Geometry.Point(' . $point[0] . ', ' . $point[1]
- . ')).transform(new OpenLayers.Projection("EPSG:' . $srid
- . '"), map.getProjectionObject()), ';
- }
- }
- if (substr($row, strlen($row) - 2) == ', ') {
- $row = substr($row, 0, strlen($row) - 2);
- }
- $row .= ')';
-
$result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
- . 'new OpenLayers.Geometry.MultiPoint(' . $row . '), null, '
- . json_encode($style_options) . '));';
+ . 'new OpenLayers.Geometry.MultiPoint('
+ . $this->getPointsArrayForOpenLayers($points_arr, $srid)
+ . '), null, ' . json_encode($style_options) . '));';
return $result;
}
@@ -283,13 +283,9 @@ public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data
$polygons = explode(")),((", $multipolygon);
$row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
- . 'new OpenLayers.Geometry.MultiPolygon(new Array(';
-
- foreach ($polygons as $polygon) {
- $row .= $this->addPointsForOpenLayersPolygon($polygon, $srid);
- }
- $row = substr($row, 0, strlen($row) - 2);
- $row .= ')), null, ' . json_encode($style_options) . '));';
+ . 'new OpenLayers.Geometry.MultiPolygon('
+ . $this->getPolygonArrayForOpenLayers($polygons, $srid)
+ . '), null, ' . json_encode($style_options) . '));';
return $row;
}
@@ -195,10 +195,8 @@ public function prepareRowAsOl($spatial, $srid, $label, $point_color, $scale_dat
$points_arr = $this->extractPoints($point, null);
if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
- $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(('
- . 'new OpenLayers.Geometry.Point(' . $points_arr[0][0] . ', '
- . $points_arr[0][1] . ').transform(new OpenLayers.Projection("EPSG:'
- . $srid . '"), map.getProjectionObject())), null, '
+ $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
+ . $this->getPointForOpenLayers($points_arr[0], $srid). ', null, '
. json_encode($style_options) . '));';
}
return $result;
@@ -246,9 +246,11 @@ public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data
// Trim to remove leading 'POLYGON((' and trailing '))'
$polygon = substr($spatial, 9, (strlen($spatial) - 11));
- $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(';
- $row .= $this->addPointsForOpenLayersPolygon($polygon, $srid);
- $row .= 'null, ' . json_encode($style_options) . '));';
+ // Seperate outer and inner polygons
+ $parts = explode("),(", $polygon);
+ $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
+ . $this->getPolygonForOpenLayers($parts, $srid)
+ . ', null, ' . json_encode($style_options) . '));';
return $row;
}

3 comments on commit 10727d6

Contributor

roccivic replied May 7, 2012

Hi Madhura,

I've just spotted a regression in master and it looks like it happened in this commit. To reproduce the problem:

  • go to demo.phpmyadmin.net
  • select opengis database
  • select gis_all table
  • select insert tab
  • click the "edit/insert" link to get the gis_data_editor
  • select "MULTIPOINT"
  • click "Add a point"
  • then there is a JS error and the editor stops working
Contributor

roccivic replied May 7, 2012

Oh, and also the "Close" button disappears when selecting "MULTIPOINT"...

Member

madhuracj replied May 9, 2012

Fixed. Thanks for spotting.

Please sign in to comment.