jenkins-bot has submitted this change and it was merged.

Change subject: Add Mapquest Nominatim geocode for Leaflet
......................................................................


Add Mapquest Nominatim geocode for Leaflet

Change-Id: Ib5cdd35ccb18d3c8312ef2f543df4cfc60dd94c0
---
M includes/GeoCoordinate.php
M includes/Geocoders.php
M includes/Point.php
M includes/mapelements/Line.php
M tests/phpunit/services/Leaflet/LeafletTest.php
5 files changed, 135 insertions(+), 5 deletions(-)

Approvals:
  Pastakhov: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/GeoCoordinate.php b/includes/GeoCoordinate.php
index 3bf833a..1750833 100644
--- a/includes/GeoCoordinate.php
+++ b/includes/GeoCoordinate.php
@@ -167,6 +167,14 @@
                $lon += ($east / (self::EQUATOR_LENGTH * cos(M_PI / 180 * 
$lat))) * 360;
        }
 
+       /**
+        * Returns the distance between two geographical points
+        * @param float $lat1 Latitude geographical point 1
+        * @param float $lon1 Longitude geographical point 1
+        * @param float $lat2 Latitude geographical point 2
+        * @param float $lon2 Longitude geographical point 2
+        * @return float Distance, in meters
+        */
        public static function getDistanceInMeters($lat1, $lon1, $lat2, $lon2) {
                $lat = abs($lat1 - $lat2);
                $lon = abs($lon1 - $lon2);
diff --git a/includes/Geocoders.php b/includes/Geocoders.php
index 033b72a..52de102 100644
--- a/includes/Geocoders.php
+++ b/includes/Geocoders.php
@@ -11,7 +11,7 @@
 
 class Geocoders {
 
-       public static function getCoordinates($address, $service) {
+       public static function getCoordinates($address, $service, $params = 
null) {
                switch ($service) {
                        case 'google':
                                return self::getCoordinatesUseGoogle($address);
@@ -19,8 +19,15 @@
                        case 'yandex':
                                return self::getCoordinatesUseYandex($address);
                                break;
+                       case 'leaflet':
+                               return 
self::getCoordinatesUseMapquestNominatim($address, $params);
+                               break;
                }
                return false;
+       }
+
+       private static function performRequest($url, $urlArgs) {
+               return \Http::get( $url.wfArrayToCgi($urlArgs) );
        }
 
        private static function getCoordinatesUseGoogle($address) {
@@ -96,8 +103,59 @@
                return $return;
        }
 
-       private static function performRequest($url, $urlArgs) {
-               return \Http::get( $url.wfArrayToCgi($urlArgs) );
+       public static function getCoordinatesUseMapquestNominatim($address, 
$params) {
+               $return = false;
+               $param_polygon = (isset( $params['polygon'] ) && 
$params['polygon'] === true) ? true : false;
+
+               $urlArgs = array(
+                       'format' => 'json',
+                       'addressdetails' => '0',
+                       'limit' => 1,
+                       'q' => $address,
+                       );
+               if( $param_polygon ) {
+                       $urlArgs['polygon'] = '1';
+               }
+               $response = self::performRequest( 
'http://open.mapquestapi.com/nominatim/v1/search.php?', $urlArgs );
+
+               if( $response !== false ) {
+                       $data = \FormatJson::decode( $response );
+                       if( isset($data[0]) ) {
+                               $data = $data[0];
+                               $lat = $data->lat;
+                               $lon = $data->lon;
+                               if( !is_null($lat) && !is_null($lon) ) {
+                                       $return = array('lat' => $lat, 'lon' => 
$lon );
+                                       $bounds = $data->boundingbox;
+                                       if( !is_null($bounds) ) {
+                                               $bounds_ne = new Point( 
$bounds[1], $bounds[3] );
+                                               $bounds_sw = new Point( 
$bounds[0], $bounds[2] );
+                                               if( $bounds_ne->isValid() && 
$bounds_sw->isValid() ) {
+                                                       $b = new Bounds( 
array($bounds_ne, $bounds_sw) );
+                                                       $return['bounds'] = $b;
+                                               }
+                                       }
+                                       if( $param_polygon ) {
+                                               $polygonpoints = 
$data->polygonpoints;
+                                               if( count($polygonpoints) > 1 ) 
{
+                                                       $points = array();
+                                                       foreach ($polygonpoints 
as $value) {
+                                                               $p = new 
Point($value[1], $value[0]);
+                                                               if( 
$p->isValid() ) {
+                                                                       
$points[] = $p;
+                                                               } else {
+                                                                       break;
+                                                               }
+                                                       }
+                                                       if( count($points) > 1 
) {
+                                                               
$return['polygon'] = $points;
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+               return $return;
        }
 
 }
\ No newline at end of file
diff --git a/includes/Point.php b/includes/Point.php
index 9f54216..748d624 100644
--- a/includes/Point.php
+++ b/includes/Point.php
@@ -107,8 +107,8 @@
                                $this->bounds = $coord['bounds'];
                        }
                }
-               $this->latitude = $coord['lat'];
-               $this->longitude = $coord['lon'];
+               $this->lat = $coord['lat'];
+               $this->lon = $coord['lon'];
                return true;
        }
 
diff --git a/includes/mapelements/Line.php b/includes/mapelements/Line.php
index 3bc3783..af88e72 100644
--- a/includes/mapelements/Line.php
+++ b/includes/mapelements/Line.php
@@ -34,4 +34,40 @@
                return 'Line'; //TODO i18n?
        }
 
+       /**
+        * Filling property 'coordinates'
+        * @global string $egMultiMaps_CoordinatesSeparator
+        * @param string $coordinates
+        * @param string $service Name of map service
+        * @return boolean
+        */
+       protected function parseCoordinates( $coordinates, $service = null ) {
+               global $egMultiMaps_CoordinatesSeparator;
+
+               $array = explode( $egMultiMaps_CoordinatesSeparator, 
$coordinates);
+
+               if( $service == 'leaflet' && count($array) == 1 ) {
+                       $value = $array[0];
+                       $coord = Geocoders::getCoordinates( $value, $service, 
array('polygon'=>true) );
+                       \MWDebug::log( var_export($coord, true));
+                       if( $coord !== false && is_array($coord['polygon']) ) {
+                               $this->coordinates = $coord['polygon'];
+                       } else {
+                               $this->errormessages[] = \wfMessage( 
'multimaps-unable-parse-coordinates', $value)->escaped();
+                               return false;
+                       }
+               } else {
+                       foreach ($array as $value) {
+                               $point = new Point();
+                               if( $point->parse($value, $service) ) {
+                                       $this->coordinates[] = $point;
+                               } else {
+                                       $this->errormessages[] = \wfMessage( 
'multimaps-unable-parse-coordinates', $value)->escaped();
+                                       return false;
+                               }
+                       }
+               }
+               return true;
+       }
+
 }
diff --git a/tests/phpunit/services/Leaflet/LeafletTest.php 
b/tests/phpunit/services/Leaflet/LeafletTest.php
index 0017e8d..bcf3919 100644
--- a/tests/phpunit/services/Leaflet/LeafletTest.php
+++ b/tests/phpunit/services/Leaflet/LeafletTest.php
@@ -331,4 +331,32 @@
                                );
        }
 
+       public function testParseGeocoderMarker() {
+               $this->assertRegExp(
+                               
'{"markers":\[{"pos":\[{"lat":[0-9\.]+,"lon":[0-9\.]+}\]}\],"bounds":{"ne":{"lat":[0-9\.]+,"lon":[0-9\.]+},"sw":{"lat":[0-9\.]+,"lon":[0-9\.]+}}}',
+                               \FormatJson::encode( $this->object->getMapData( 
array('Moscow', 'service=leaflet') ) )
+                               );
+       }
+
+       public function testParseGeocoderRectangle() {
+               $this->assertRegExp(
+                               
'{"rectangles":\[{"pos":\[{"lat":[0-9\.]+,"lon":[0-9\.]+},{"lat":[0-9\.]+,"lon":[0-9\.]+}\]}\],"bounds":{"ne":{"lat":[0-9\.]+,"lon":[0-9\.]+},"sw":{"lat":[0-9\.]+,"lon":[0-9\.]+}}}',
+                               \FormatJson::encode( $this->object->getMapData( 
array('rectangle=Moscow', 'service=leaflet') ) )
+                               );
+       }
+
+       public function testParseGeocoderRectangles() {
+               $this->assertRegExp(
+                               
'{"rectangles":\[{"pos":\[{"lat":[0-9\.]+,"lon":[0-9\.]+},{"lat":[0-9\.]+,"lon":[0-9\.]+}\]},{"pos":\[{"lat":[0-9\.]+,"lon":[0-9\.]+},{"lat":[-0-9\.]+,"lon":[-0-9\.]+}]}],"bounds":{"ne":{"lat":[0-9\.]+,"lon":[0-9\.]+},"sw":{"lat":[0-9\.]+,"lon":[-0-9\.]+}}}',
+                               \FormatJson::encode( $this->object->getMapData( 
array('rectangle=Moscow;London', 'service=leaflet') ) )
+                               );
+       }
+
+       public function testParseGeocoderCircle() {
+               $this->assertRegExp(
+                               
'{"circles":\[{"radius":\[[0-9\.]+\],"pos":\[{"lat":[0-9\.]+,"lon":[0-9\.]+}\]}\],"bounds":{"ne":{"lat":[0-9\.]+,"lon":[0-9\.]+},"sw":{"lat":[0-9\.]+,"lon":[0-9\.]+}}}',
+                               \FormatJson::encode( $this->object->getMapData( 
array('circle=Moscow', 'service=leaflet') ) )
+                               );
+       }
+
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib5cdd35ccb18d3c8312ef2f543df4cfc60dd94c0
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/MultiMaps
Gerrit-Branch: master
Gerrit-Owner: Pastakhov <[email protected]>
Gerrit-Reviewer: Pastakhov <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to