]> git.openstreetmap.org Git - nominatim.git/commitdiff
Merge branch 'geojson-output' of https://github.com/mtmail/Nominatim into mtmail...
authorSarah Hoffmann <lonvia@denofr.de>
Fri, 6 Jul 2018 18:26:33 +0000 (20:26 +0200)
committerSarah Hoffmann <lonvia@denofr.de>
Fri, 6 Jul 2018 18:26:33 +0000 (20:26 +0200)
1  2 
lib/PlaceLookup.php
test/bdd/api/reverse/simple.feature
website/reverse.php

diff --combined lib/PlaceLookup.php
index cf744929942cba2cac6a116ade375c3a63fbadc8,0bf8c2fe73f85785ff85280fb6b1fe050b1d3e86..57d6166e6c3e400e3d3803f535d026f820d6b23e
@@@ -11,6 -11,7 +11,7 @@@ class PlaceLooku
      protected $aLangPrefOrderSql = "''";
  
      protected $bAddressDetails = false;
+     protected $bAddressAdminLevels = false;
      protected $bExtraTags = false;
      protected $bNameDetails = false;
  
          $this->bIncludePolygonAsPoints = $b;
      }
  
+     public function setAddressDetails($b = true)
+     {
+         $this->bAddressDetails = $b;
+     }
+     public function setAddressAdminLevels($b = true)
+     {
+         $this->bAddressAdminLevels = $b;
+     }
      public function loadParamArray($oParams, $sGeomType = null)
      {
          $aLangs = $oParams->getPreferredLanguages();
  
          $this->bDeDupe = $oParams->getBool('dedupe', $this->bDeDupe);
  
-         if ($sGeomType === null || $sGeomType == 'text') {
-             $this->bIncludePolygonAsText = $oParams->getBool('polygon_text');
-         }
          if ($sGeomType === null || $sGeomType == 'geojson') {
              $this->bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson');
+             $this->bIncludePolygonAsPoints = false;
          }
-         if ($sGeomType === null || $sGeomType == 'kml') {
-             $this->bIncludePolygonAsKML = $oParams->getBool('polygon_kml');
-         }
-         if ($sGeomType === null || $sGeomType == 'svg') {
-             $this->bIncludePolygonAsSVG = $oParams->getBool('polygon_svg');
+         if ($oParams->getString('format', '') !== 'geojson') {
+             if ($sGeomType === null || $sGeomType == 'text') {
+                 $this->bIncludePolygonAsText = $oParams->getBool('polygon_text');
+             }
+             if ($sGeomType === null || $sGeomType == 'kml') {
+                 $this->bIncludePolygonAsKML = $oParams->getBool('polygon_kml');
+             }
+             if ($sGeomType === null || $sGeomType == 'svg') {
+                 $this->bIncludePolygonAsSVG = $oParams->getBool('polygon_svg');
+             }
          }
          $this->fPolygonSimplificationThreshold
              = $oParams->getFloat('polygon_threshold', 0.0);
                  );
              }
  
+             if ($this->bAddressAdminLevels) {
+                 $aPlace['aAddressAdminLevels'] = $this->getAddressAdminLevels(
+                     $aPlace['place_id'],
+                     $aPlace['housenumber']
+                 );
+             }
              if ($this->bExtraTags) {
                  if ($aPlace['extra']) {
                      $aPlace['sExtraTags'] = json_decode($aPlace['extra']);
          return $aAddress;
      }
  
+     /* "Downing Street, London"
+      * [
+      *   "level15" => "Covent Garden",
+      *   "level8" => "Westminster",
+      *   "level6" => "London",
+      *   "level5" => "Greater London",
+      *   "level4" => "England",
+      *   "level2" => "United Kingdom"
+      * ]
+      */
+     public function getAddressAdminLevels($iPlaceID, $sHousenumber = null)
+     {
+         $aAddressLines = $this->getAddressDetails(
+             $iPlaceID,
+             false,
+             $sHousenumber === null ? -1 : $sHousenumber
+         );
+         $aAddress = array();
+         foreach ($aAddressLines as $aLine) {
+             if (isset($aLine['admin_level'])) {
+                 $aAddress['level'.$aLine['admin_level']] = $aLine['localname'];
+             }
+         }
+         return $aAddress;
+     }
  
  
      /* returns an array which will contain the keys
       */
  
  
 -    public function getOutlines($iPlaceID, $fLon = null, $fLat = null, $fRadius = null)
 +    public function getOutlines($iPlaceID, $fLon = null, $fLat = null, $fRadius = null, $fLonReverse = null, $fLatReverse = null)
      {
  
          $aOutlineResult = array();
  
          if (CONST_Search_AreaPolygons) {
              // Get the bounding box and outline polygon
 -            $sSQL  = 'select place_id,0 as numfeatures,st_area(geometry) as area,';
 -            $sSQL .= 'ST_Y(centroid) as centrelat,ST_X(centroid) as centrelon,';
 -            $sSQL .= 'ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,';
 -            $sSQL .= 'ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon';
 +            $sSQL = 'select place_id,0 as numfeatures,st_area(geometry) as area,';
 +            if ($fLonReverse != null && $fLatReverse != null) {
 +                $sSQL .= ' ST_Y(closest_point) as centrelat,';
 +                $sSQL .= ' ST_X(closest_point) as centrelon,';
 +            } else {
 +                $sSQL .= ' ST_Y(centroid) as centrelat, ST_X(centroid) as centrelon,';
 +            }
 +            $sSQL .= ' ST_YMin(geometry) as minlat,ST_YMax(geometry) as maxlat,';
 +            $sSQL .= ' ST_XMin(geometry) as minlon,ST_XMax(geometry) as maxlon';
              if ($this->bIncludePolygonAsGeoJSON) $sSQL .= ',ST_AsGeoJSON(geometry) as asgeojson';
              if ($this->bIncludePolygonAsKML) $sSQL .= ',ST_AsKML(geometry) as askml';
              if ($this->bIncludePolygonAsSVG) $sSQL .= ',ST_AsSVG(geometry) as assvg';
              if ($this->bIncludePolygonAsText || $this->bIncludePolygonAsPoints) $sSQL .= ',ST_AsText(geometry) as astext';
 -            $sFrom = ' from placex where place_id = '.$iPlaceID;
 +            if ($fLonReverse != null && $fLatReverse != null) {
 +                $sFrom = ' from (SELECT * , CASE WHEN (class = \'highway\') AND (ST_GeometryType(geometry) = \'ST_LineString\') THEN ';
 +                $sFrom .=' ST_ClosestPoint(geometry, ST_SetSRID(ST_Point('.$fLatReverse.','.$fLonReverse.'),4326))';
 +                $sFrom .=' ELSE centroid END AS closest_point';
 +                $sFrom .= ' from placex where place_id = '.$iPlaceID.') as plx';
 +            } else {
 +                $sFrom = ' from placex where place_id = '.$iPlaceID;
 +            }
              if ($this->fPolygonSimplificationThreshold > 0) {
                  $sSQL .= ' from (select place_id,centroid,ST_SimplifyPreserveTopology(geometry,'.$this->fPolygonSimplificationThreshold.') as geometry'.$sFrom.') as plx';
              } else {
index ecaca483196cd08942a17c6cbab2acbf2b656899,3f307e0e2d1420e9f9070f161bab8dbba97192b2..115b0fd39554e33252d9051ebdb0ce9febf7572c
@@@ -11,6 -11,8 +11,8 @@@ Feature: Simple Reverse Test
          Then the result is valid json
          When sending jsonv2 reverse coordinates <lat>,<lon>
          Then the result is valid json
+         When sending geojson reverse coordinates <lat>,<lon>
+         Then the result is valid geojson
          When sending html reverse coordinates <lat>,<lon>
          Then the result is valid html
  
            | param       | value   |
            | <parameter> | <value> |
          Then the result is valid json
+         When sending geojson reverse coordinates 53.603,10.041
+           | param       | value   |
+           | <parameter> | <value> |
+         Then the result is valid geojson
  
      Examples:
       | parameter        | value |
          When sending <format> reverse coordinates 67.3245,0.456
          | json_callback |
          | foo |
-         Then the result is valid json
+         Then the result is valid <outformat>
  
      Examples:
-       | format |
-       | json |
-       | jsonv2 |
+       | format | outformat |
+       | json | json |
+       | jsonv2 | json |
+       | geojson | geojson |
  
      Scenario Outline: Boundingbox is returned
          When sending <format> reverse coordinates 14.62,108.1
            | zoom |
 -          | 4 |
 +          | 8 |
          Then result has bounding box in 9,20,102,113
  
      Examples:
        | format |
        | json |
        | jsonv2 |
+       | geojson |
        | xml |
  
      Scenario Outline: Reverse-geocoding with zoom
@@@ -89,6 -97,7 +97,7 @@@
        | format |
        | json |
        | jsonv2 |
+       | geojson |
        | html |
        | xml |
  
diff --combined website/reverse.php
index e131a84491a56592569d9497f65a5eb735006cc7,3c3824eb1526355d96fa5db040bb2629fda4352f..3d5a3ec78e9d35a777f681d76cee0bfe1ac9780a
@@@ -12,7 -12,7 +12,7 @@@ ini_set('memory_limit', '200M')
  $oParams = new Nominatim\ParameterParser();
  
  // Format for output
- $sOutputFormat = $oParams->getSet('format', array('html', 'xml', 'json', 'jsonv2'), 'xml');
+ $sOutputFormat = $oParams->getSet('format', array('html', 'xml', 'json', 'jsonv2', 'geojson', 'geocodejson'), 'xml');
  
  // Preferred language
  $aLangPrefOrder = $oParams->getPreferredLanguages();
@@@ -23,6 -23,10 +23,10 @@@ $hLog = logStart($oDB, 'reverse', $_SER
  
  $oPlaceLookup = new Nominatim\PlaceLookup($oDB);
  $oPlaceLookup->loadParamArray($oParams);
+ if ($sOutputFormat == 'geocodejson') {
+     $oPlaceLookup->setAddressDetails(true);
+     $oPlaceLookup->setAddressAdminLevels(true);
+ }
  
  $sOsmType = $oParams->getSet('osm_type', array('N', 'W', 'R'));
  $iOsmId = $oParams->getInt('osm_id', -1);
@@@ -55,9 -59,7 +59,9 @@@ if (isset($aPlace)) 
          $aPlace['place_id'],
          $aPlace['lon'],
          $aPlace['lat'],
 -        $fRadius
 +        $fRadius, 
 +        $fLat, 
 +        $fLon 
      );
  
      if ($aOutlineResult) {
@@@ -77,7 -79,12 +81,12 @@@ if ($sOutputFormat == 'html') 
      $sDataDate = chksql($oDB->getOne("select TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' from import_status limit 1"));
      $sTileURL = CONST_Map_Tile_URL;
      $sTileAttribution = CONST_Map_Tile_Attribution;
+ } elseif ($sOutputFormat == 'geocodejson') {
+     $sQuery = $fLat.','.$fLon;
+     if (isset($aPlace['place_id'])) {
+         $fDistance = chksql($oDB->getOne('SELECT ST_Distance(ST_SetSRID(ST_Point('.$fLon.','.$fLat.'),4326), centroid) FROM placex where place_id='.$aPlace['place_id']));
+     }
  }
  
- $sOutputTemplate = ($sOutputFormat=='jsonv2' ? 'json' : $sOutputFormat);
+ $sOutputTemplate = ($sOutputFormat == 'jsonv2') ? 'json' : $sOutputFormat;
  include(CONST_BasePath.'/lib/template/address-'.$sOutputTemplate.'.php');