Pastakhov has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/50177


Change subject: Add geocoders (google)
......................................................................

Add geocoders (google)

Change-Id: I0a4d0d0597c4a5a0b2849735771b29c65d3677c9
---
M MultiMaps.php
M includes/BaseMapService.php
M includes/Bounds.php
A includes/Geocoders.php
M includes/Point.php
M includes/mapelements/BaseMapElement.php
M includes/mapelements/Circle.php
M includes/mapelements/Marker.php
M includes/mapelements/Rectangle.php
9 files changed, 153 insertions(+), 45 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MultiMaps 
refs/changes/77/50177/1

diff --git a/MultiMaps.php b/MultiMaps.php
index 39ea189..3b8b604 100644
--- a/MultiMaps.php
+++ b/MultiMaps.php
@@ -32,8 +32,8 @@
 $egMultiMapsScriptPath = ( $wgExtensionAssetsPath === false ? $wgScriptPath . 
'/extensions' : $wgExtensionAssetsPath ) . '/MultiMaps';
 
 // Allow translations for this extension
-$wgExtensionMessagesFiles['MultiMaps'] = $dir . '/MultiMaps.i18n.php';
-$wgExtensionMessagesFiles['MultiMapsMagic'] = $dir . 
'/MultiMaps.i18n.magic.php';
+$wgExtensionMessagesFiles['MultiMaps'] =               $dir . 
'/MultiMaps.i18n.php';
+$wgExtensionMessagesFiles['MultiMapsMagic'] =  $dir . 
'/MultiMaps.i18n.magic.php';
 
 // Include the settings file.
 require_once $dir . '/Settings.php';
@@ -49,20 +49,22 @@
 
 //Preparing classes for autoloading
 // TODO: $wgAutoloadClasses = array_merge( $wgAutoloadClasses, include 
'MultiMaps.classes.php' );
-$wgAutoloadClasses['MultiMaps'] =                                 $dir . 
'/MultiMaps.body.php';
-$wgAutoloadClasses['MultiMaps\\MapServices'] =            $dir . 
'/includes/MapServices.php';
+$wgAutoloadClasses['MultiMaps'] =                                      $dir . 
'/MultiMaps.body.php';
 
-$wgAutoloadClasses['MultiMaps\\BaseMapService'] =        $dir . 
'/includes/BaseMapService.php';
-$wgAutoloadClasses['MultiMaps\\Bounds'] =                 $dir . 
'/includes/Bounds.php';
+
+$wgAutoloadClasses['MultiMaps\\BaseMapService'] =      $dir . 
'/includes/BaseMapService.php';
+$wgAutoloadClasses['MultiMaps\\Bounds'] =                      $dir . 
'/includes/Bounds.php';
+$wgAutoloadClasses['MultiMaps\\Geocoders'] =           $dir . 
'/includes/Geocoders.php';
 $wgAutoloadClasses['MultiMaps\\GeoCoordinate'] =       $dir . 
'/includes/GeoCoordinate.php';
+$wgAutoloadClasses['MultiMaps\\MapServices'] =         $dir . 
'/includes/MapServices.php';
 $wgAutoloadClasses['MultiMaps\\Point'] =                       $dir . 
'/includes/Point.php';
 
-$wgAutoloadClasses['MultiMaps\\BaseMapElement'] =   $dir . 
'/includes/mapelements/BaseMapElement.php';
-$wgAutoloadClasses['MultiMaps\\Marker'] =                 $dir . 
'/includes/mapelements/Marker.php';
-$wgAutoloadClasses['MultiMaps\\Line'] =                         $dir . 
'/includes/mapelements/Line.php';
-$wgAutoloadClasses['MultiMaps\\Polygon'] =               $dir . 
'/includes/mapelements/Polygon.php';
+$wgAutoloadClasses['MultiMaps\\BaseMapElement'] =      $dir . 
'/includes/mapelements/BaseMapElement.php';
+$wgAutoloadClasses['MultiMaps\\Marker'] =                      $dir . 
'/includes/mapelements/Marker.php';
+$wgAutoloadClasses['MultiMaps\\Line'] =                                $dir . 
'/includes/mapelements/Line.php';
+$wgAutoloadClasses['MultiMaps\\Polygon'] =                     $dir . 
'/includes/mapelements/Polygon.php';
 $wgAutoloadClasses['MultiMaps\\Rectangle'] =           $dir . 
'/includes/mapelements/Rectangle.php';
-$wgAutoloadClasses['MultiMaps\\Circle'] =                 $dir . 
'/includes/mapelements/Circle.php';
+$wgAutoloadClasses['MultiMaps\\Circle'] =                      $dir . 
'/includes/mapelements/Circle.php';
 
 //define modules that can later be loaded during the output
 $wgResourceModules['ext.MultiMaps'] = array(
diff --git a/includes/BaseMapService.php b/includes/BaseMapService.php
index 86fa2a5..c067c45 100644
--- a/includes/BaseMapService.php
+++ b/includes/BaseMapService.php
@@ -325,7 +325,7 @@
                                continue;
                        }
                        $marker = new Marker();
-                       if( !$marker->parse($markervalue) ) {
+                       if( !$marker->parse($markervalue, $this->classname) ) {
                                $return = false;
                                $this->errormessages = array_merge( 
$this->errormessages, $marker->getErrorMessages() );
                        }
@@ -351,7 +351,7 @@
                                continue;
                        }
                        $line = new Line();
-                       if( !$line->parse($linevalue) ) {
+                       if( !$line->parse($linevalue, $this->classname) ) {
                                $return = false;
                                $this->errormessages = array_merge( 
$this->errormessages, $line->getErrorMessages() );
                        }
@@ -377,7 +377,7 @@
                                continue;
                        }
                        $polygon = new Polygon();
-                       if( !$polygon->parse($polygonvalue) ) {
+                       if( !$polygon->parse($polygonvalue, $this->classname) ) 
{
                                $return = false;
                                $this->errormessages = array_merge( 
$this->errormessages, $polygon->getErrorMessages() );
                        }
@@ -403,7 +403,7 @@
                                continue;
                        }
                        $rectangle = new Rectangle();
-                       if( !$rectangle->parse($rectanglevalue) ) {
+                       if( !$rectangle->parse($rectanglevalue, 
$this->classname) ) {
                                $return = false;
                                $this->errormessages = array_merge( 
$this->errormessages, $rectangle->getErrorMessages() );
                        }
@@ -429,7 +429,7 @@
                                continue;
                        }
                        $circle = new Circle();
-                       if( !$circle->parse($circlevalue) ) {
+                       if( !$circle->parse($circlevalue, $this->classname) ) {
                                $return = false;
                                $this->errormessages = array_merge( 
$this->errormessages, $circle->getErrorMessages() );
                        }
diff --git a/includes/Bounds.php b/includes/Bounds.php
index c6bea3a..a449bd1 100644
--- a/includes/Bounds.php
+++ b/includes/Bounds.php
@@ -32,21 +32,44 @@
         * @param array $coordinates Array of Point objects
         */
        public function extend( $coordinates ) {
+               if( $coordinates instanceof Point ) {
+                       $coordinates = array($coordinates);
+               }
                foreach ($coordinates as $value) {
+                       $bounds = $value->bounds;
                        if( !$this->isValid() ) {
-                               $this->northEast = new Point($value->lat, 
$value->lon);
-                               $this->southWest = new Point($value->lat, 
$value->lon);
-                       } else {
-                               if( $value->lat < $this->southWest->lat ) {
-                                       $this->southWest->lat = $value->lat;
-                               } elseif ( $value->lat > $this->northEast->lat 
) {
-                                       $this->northEast->lat = $value->lat;
+                               if( $bounds ) {
+                                       $this->northEast = $bounds->ne;
+                                       $this->southWest = $bounds->sw;
+                               } else {
+                                       $this->northEast = new 
Point($value->lat, $value->lon);
+                                       $this->southWest = new 
Point($value->lat, $value->lon);
                                }
+                       } else {
+                               if( $bounds ) {
+                                       if( $bounds->sw->lat < 
$this->southWest->lat ) {
+                                               $this->southWest->lat = 
$bounds->sw->lat;
+                                       } elseif ( $bounds->ne->lat > 
$this->northEast->lat ) {
+                                               $this->northEast->lat = 
$bounds->ne->lat;
+                                       }
 
-                               if( $value->lon < $this->southWest->lon ) {
-                                       $this->southWest->lon = $value->lon;
-                               } elseif ( $value->lon > $this->northEast->lon 
) {
-                                       $this->northEast->lon = $value->lon;
+                                       if( $bounds->sw->lon < 
$this->southWest->lon ) {
+                                               $this->southWest->lon = 
$bounds->sw->lon;
+                                       } elseif ( $bounds->ne->lon > 
$this->northEast->lon ) {
+                                               $this->northEast->lon = 
$bounds->ne->lon;
+                                       }
+                               } else {
+                                       if( $value->lat < $this->southWest->lat 
) {
+                                               $this->southWest->lat = 
$value->lat;
+                                       } elseif ( $value->lat > 
$this->northEast->lat ) {
+                                               $this->northEast->lat = 
$value->lat;
+                                       }
+
+                                       if( $value->lon < $this->southWest->lon 
) {
+                                               $this->southWest->lon = 
$value->lon;
+                                       } elseif ( $value->lon > 
$this->northEast->lon ) {
+                                               $this->northEast->lon = 
$value->lon;
+                                       }
                                }
                        }
                }
diff --git a/includes/Geocoders.php b/includes/Geocoders.php
new file mode 100644
index 0000000..8d38e2f
--- /dev/null
+++ b/includes/Geocoders.php
@@ -0,0 +1,63 @@
+<?php
+namespace MultiMaps;
+/**
+ *
+ *
+ * @file Geocoders.php
+ * @ingroup MultiMaps
+ * @author Pavel Astakhov <[email protected]>
+ * @licence GNU General Public Licence 2.0 or later
+ */
+
+class Geocoders {
+
+       public static function getCoordinates($address, $service) {
+               switch ($service) {
+                       case 'google':
+                               return self::getCoordinatesUseGoogle($address);
+                               break;
+               }
+               return false;
+       }
+
+       private static function getCoordinatesUseGoogle($address) {
+               $return = false;
+
+               $urlArgs = array(
+                       'sensor' => 'false',
+                       'address' => $address,
+                       );
+               $response = self::performRequest( 
'https://maps.googleapis.com/maps/api/geocode/json?', $urlArgs);
+
+               if( $response !== false ) {
+                       $data = \FormatJson::decode( $response );
+                       if( is_null($data) === false ) {
+                               if ( $data->status == 'OK' ) {
+                                       $geometry = $data->results[0]->geometry;
+                                       $location = $geometry->location;
+                                       $lat = $location->lat;
+                                       $lon = $location->lng;
+                                       if( !is_null($lat) && !is_null($lon) ) {
+                                               $return = array('lat' => $lat, 
'lon' => $lon );
+                                               $bounds = $geometry->bounds;
+                                               if( !is_null($bounds) ) {
+                                                       $bounds_ne = new Point( 
$bounds->northeast->lat, $bounds->northeast->lng );
+                                                       $bounds_sw = new Point( 
$bounds->southwest->lat, $bounds->southwest->lng );
+                                                       if( 
$bounds_ne->isValid() && $bounds_sw->isValid() ) {
+                                                               $b = new 
Bounds();
+                                                               $b->extend( 
array($bounds_ne, $bounds_sw) );
+                                                               
$return['bounds'] = $b;
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+               return $return;
+       }
+
+       private static function performRequest($url, $urlArgs) {
+               return \Http::get( $url.wfArrayToCgi($urlArgs) );
+       }
+
+}
\ No newline at end of file
diff --git a/includes/Point.php b/includes/Point.php
index aca43b6..1ca8a9b 100644
--- a/includes/Point.php
+++ b/includes/Point.php
@@ -10,6 +10,7 @@
  * @licence GNU General Public Licence 2.0 or later
  * @property-read float $lat Latitude coordinate
  * @property-read float $lon Longitude coordinate
+ * @property-read Bounds $bounds Bounds associated with the point, used in the 
geocoding
  */
 class Point {
        /**
@@ -23,6 +24,12 @@
         * @var float
         */
        protected $longitude = false;
+
+       /**
+        * Bounds associated with the point, used in the geocoding
+        * @var Bounds
+        */
+       protected $bounds = false;
 
        /**
         * Constructor
@@ -43,6 +50,9 @@
                                break;
                        case 'lon':
                                return $this->longitude;
+                               break;
+                       case 'bounds':
+                               return $this->bounds;
                                break;
                }
                return null;
@@ -67,17 +77,24 @@
                }
        }
 
-                       /**
+       /**
         * Parse geographic coordinates
         * @param string $string geographic coordinates
+        * @param string $service Name of map service
         * @return boolean
         */
-       public function parse($string) {
+       public function parse($string, $service = null) {
                $coord = GeoCoordinate::getLatLonFromString($string);
                if( is_array($coord) === false) {
-                       $this->latitude = false;
-                       $this->longitude = false;
-                       return false;
+                       $coord = Geocoders::getCoordinates( $string, $service );
+                       if( is_array($coord) === false) {
+                               $this->latitude = false;
+                               $this->longitude = false;
+                               return false;
+                       }
+                       if( isset($coord['bounds']) ) {
+                               $this->bounds = $coord['bounds'];
+                       }
                }
                $this->latitude = $coord['lat'];
                $this->longitude = $coord['lon'];
diff --git a/includes/mapelements/BaseMapElement.php 
b/includes/mapelements/BaseMapElement.php
index 95f9021..26fb47f 100644
--- a/includes/mapelements/BaseMapElement.php
+++ b/includes/mapelements/BaseMapElement.php
@@ -8,7 +8,7 @@
  * @ingroup MultiMaps
  * @author Pavel Astakhov <[email protected]>
  * @licence GNU General Public Licence 2.0 or later
- * @property-read float $pos Geographic coordinates
+ * @property-read array $pos Array of geographic coordinates
  * @property string $title Title of element
  * @property string $text Popup text of element
  */
@@ -16,7 +16,7 @@
 
        /**
         * Geographic coordinates
-        * @var array
+        * @var array Array of Point
         */
        protected $coordinates;
 
@@ -130,9 +130,10 @@
         * Filling properties of the object according to the obtained data
         * @global string $egMultiMaps_DelimiterParam
         * @param string $param
+        * @param string $service Name of map service
         * @return boolean returns false if there were errors during parsing, 
it does not mean that the item was not added. Check with isValid()
         */
-       public function parse( $param ) {
+       public function parse( $param, $service = null ) {
                global $egMultiMaps_DelimiterParam;
                $this->reset();
 
@@ -140,7 +141,7 @@
 
                //The first parameter should always be coordinates
                $coordinates = array_shift($arrayparam);
-               if( $this->parseCoordinates($coordinates) === false ) {
+               if( $this->parseCoordinates($coordinates, $service ) === false 
) {
                        $this->errormessages[] = \wfMessage( 
'multimaps-unable-create-element', $this->getElementName() )->escaped();
                        return false;
                }
@@ -154,15 +155,16 @@
         * Filling property 'coordinates'
         * @global string $egMultiMaps_CoordinatesSeparator
         * @param string $coordinates
+        * @param string $service Name of map service
         * @return boolean
         */
-       protected function parseCoordinates( $coordinates ) {
+       protected function parseCoordinates( $coordinates, $service = null ) {
                global $egMultiMaps_CoordinatesSeparator;
 
                $array = explode( $egMultiMaps_CoordinatesSeparator, 
$coordinates);
                foreach ($array as $value) {
                        $point = new Point();
-                       if( $point->parse($value) ) {
+                       if( $point->parse($value, $service) ) {
                                $this->coordinates[] = $point;
                        } else {
                                $this->errormessages[] = \wfMessage( 
'multimaps-unable-parse-coordinates', $value)->escaped();
diff --git a/includes/mapelements/Circle.php b/includes/mapelements/Circle.php
index 0bf8d44..30e93b6 100644
--- a/includes/mapelements/Circle.php
+++ b/includes/mapelements/Circle.php
@@ -30,9 +30,10 @@
         * Filling property 'coordinates'
         * @global string $egMultiMaps_CoordinatesSeparator
         * @param string $coordinates
+        * @param string $service Name of map service
         * @return boolean
         */
-       protected function parseCoordinates($coordinates) {
+       protected function parseCoordinates($coordinates, $service = null) {
                global $egMultiMaps_CoordinatesSeparator;
 
                $array = explode( $egMultiMaps_CoordinatesSeparator, 
$coordinates);
@@ -40,7 +41,7 @@
                if( count($array) == 2 )
                {
                        $point = new Point();
-                       if( $point->parse($array[0]) ) {
+                       if( $point->parse($array[0], $service) ) {
                                if(is_numeric($array[1]) ) {
                                        $this->coordinates[] = $point;
                                        $this->radiuses[] = (float)$array[1];
diff --git a/includes/mapelements/Marker.php b/includes/mapelements/Marker.php
index 8230783..8e025a2 100644
--- a/includes/mapelements/Marker.php
+++ b/includes/mapelements/Marker.php
@@ -43,5 +43,4 @@
                return parent::setProperty($name, $value);
        }
 
-
 }
diff --git a/includes/mapelements/Rectangle.php 
b/includes/mapelements/Rectangle.php
index e277e95..8fe0036 100644
--- a/includes/mapelements/Rectangle.php
+++ b/includes/mapelements/Rectangle.php
@@ -31,10 +31,11 @@
         * @assert ('10') === false
         *
         * @global type $egMultiMaps_CoordinatesSeparator
-        * @param type $coordinates
+        * @param string $coordinates
+        * @param string $service Name of map service
         * @return boolean
         */
-       protected function parseCoordinates($coordinates) {
+       protected function parseCoordinates($coordinates, $service = null) {
                global $egMultiMaps_CoordinatesSeparator;
 
                $array = explode( $egMultiMaps_CoordinatesSeparator, 
$coordinates);
@@ -43,8 +44,8 @@
                {
                        $point1 = new Point();
                        $point2 = new Point();
-                       if( $point1->parse($array[0]) ) {
-                               if( $point2->parse($array[1]) ) {
+                       if( $point1->parse($array[0], $service) ) {
+                               if( $point2->parse($array[1], $service) ) {
                                        $this->coordinates[] = $point1;
                                        $this->coordinates[] = $point2;
                                } else {

-- 
To view, visit https://gerrit.wikimedia.org/r/50177
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0a4d0d0597c4a5a0b2849735771b29c65d3677c9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MultiMaps
Gerrit-Branch: master
Gerrit-Owner: Pastakhov <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to