Pastakhov has uploaded a new change for review.

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


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 tests/phpunit/services/Leaflet/LeafletTest.php
3 files changed, 97 insertions(+), 3 deletions(-)


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

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..00b0d6f 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($p) > 1 ) {
+                                                               
$params['polygon'] = $p;
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+               return $return;
        }
 
 }
\ No newline at end of file
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: newchange
Gerrit-Change-Id: Ib5cdd35ccb18d3c8312ef2f543df4cfc60dd94c0
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