details support json output

This commit is contained in:
Marc Tobias Metten
2018-03-09 02:27:24 +01:00
committed by Sarah Hoffmann
parent 3cdbcbff8f
commit 1e28f2478c
4 changed files with 152 additions and 53 deletions

View File

@@ -0,0 +1,41 @@
<?php
$aPlaceDetails = $aPointDetails;
$aPlaceDetails['geojson'] = json_decode($aPointDetails['asgeojson']);
unset($aPlaceDetails['asgeojson']);
if ($aAddressLines) {
$aPlaceDetails['address_lines'] = $aAddressLines;
}
if ($aLinkedLines) {
$aPlaceDetails['linked_lines'] = $aLinkedLines;
}
if ($aPlaceSearchNameKeywords) {
$aPlaceDetails['place_search_name_keywords'] = $aPlaceSearchNameKeywords;
}
if ($aPlaceSearchAddressKeywords) {
$aPlaceDetails['place_search_address_keywords'] = $aPlaceSearchAddressKeywords;
}
if ($aParentOfLines) {
$aPlaceDetails['parentof_lines'] = $aParentOfLines;
if ($bGroupParents) {
$aGroupedAddressLines = [];
foreach ($aParentOfLines as $aAddressLine) {
if ($aAddressLine['type'] == 'yes') $sType = $aAddressLine['class'];
else $sType = $aAddressLine['type'];
if (!isset($aGroupedAddressLines[$sType]))
$aGroupedAddressLines[$sType] = [];
$aGroupedAddressLines[$sType][] = $aAddressLine;
}
$aPlaceDetails['parentof_lines'] = $aGroupedAddressLines;
}
}
javascript_renderData($aPlaceDetails);

View File

@@ -18,3 +18,36 @@ Feature: Object details
| keywords | | keywords |
| 1 | | 1 |
Then the result is valid html Then the result is valid html
Scenario: JSON Details
When sending json details query for W78099902
Then the result is valid json
And result has not attributes place_search_name_keywords,place_search_address_keywords,address_lines,linked_lines,parentof_lines
Scenario: JSON Details with keywords
When sending json details query for W78099902
| keywords |
| 1 |
Then the result is valid json
And result has attributes place_search_name_keywords,place_search_address_keywords
Scenario: JSON Details with addressdetails
When sending json details query for W78099902
| addressdetails |
| 1 |
Then the result is valid json
And result has attributes address_lines
Scenario: JSON Details with linkedplaces
When sending json details query for R123924
| linkedplaces |
| 1 |
Then the result is valid json
And result has attributes linked_lines
Scenario: JSON Details with childplaces
When sending json details query for W78099902
| childplaces |
| 1 |
Then the result is valid json
And result has attributes parentof_lines

View File

@@ -232,6 +232,10 @@ class DetailsResponse(GenericResponse):
options={'char-encoding' : 'utf8'}) options={'char-encoding' : 'utf8'})
self.result = {} self.result = {}
def parse_json(self):
self.result = [json.JSONDecoder(object_pairs_hook=OrderedDict).decode(self.page)]
@when(u'searching for "(?P<query>.*)"(?P<dups> with dups)?') @when(u'searching for "(?P<query>.*)"(?P<dups> with dups)?')
def query_cmd(context, query, dups): def query_cmd(context, query, dups):
""" Query directly via PHP script. """ Query directly via PHP script.
@@ -379,7 +383,12 @@ def website_details_request(context, fmt, query):
params['place_id'] = query params['place_id'] = query
outp, status = send_api_query('details', params, fmt, context) outp, status = send_api_query('details', params, fmt, context)
context.response = DetailsResponse(outp, 'html', status) if fmt is None:
outfmt = 'html'
else:
outfmt = fmt.strip()
context.response = DetailsResponse(outp, outfmt, status)
@when(u'sending (?P<fmt>\S+ )?lookup query for (?P<query>.*)') @when(u'sending (?P<fmt>\S+ )?lookup query for (?P<query>.*)')
def website_lookup_request(context, fmt, query): def website_lookup_request(context, fmt, query):

View File

@@ -9,7 +9,8 @@ ini_set('memory_limit', '200M');
$oParams = new Nominatim\ParameterParser(); $oParams = new Nominatim\ParameterParser();
$sOutputFormat = 'html'; $sOutputFormat = $oParams->getSet('format', array('html', 'json'), 'html');
$aLangPrefOrder = $oParams->getPreferredLanguages(); $aLangPrefOrder = $oParams->getPreferredLanguages();
$sLanguagePrefArraySQL = 'ARRAY['.join(',', array_map('getDBQuoted', $aLangPrefOrder)).']'; $sLanguagePrefArraySQL = 'ARRAY['.join(',', array_map('getDBQuoted', $aLangPrefOrder)).']';
@@ -18,6 +19,12 @@ $sOsmType = $oParams->getSet('osmtype', array('N', 'W', 'R'));
$iOsmId = $oParams->getInt('osmid', -1); $iOsmId = $oParams->getInt('osmid', -1);
$sClass = $oParams->getString('class'); $sClass = $oParams->getString('class');
$bIncludeAddressDetails = ($sOutputFormat == 'html') ? 1 : $oParams->getBool('addressdetails');
$bIncludeLinkedPlaces = ($sOutputFormat == 'html') ? 1 : $oParams->getBool('linkedplaces');
$bIncludeChildPlaces = ($sOutputFormat == 'html') ? 1 : $oParams->getBool('childplaces');
$bGroupParents = $oParams->getBool('group_parents', 0);
$oDB =& getDB(); $oDB =& getDB();
if ($sOsmType && $iOsmId > 0) { if ($sOsmType && $iOsmId > 0) {
@@ -154,62 +161,71 @@ if (PEAR::isError($aPointDetails['aExtraTags'])) { // possible timeout
} }
// Address // Address
$aAddressLines = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPointDetails['country_code'], -1, true); $aAddressLines = false;
if ($bIncludeAddressDetails) {
$aAddressLines = getAddressDetails($oDB, $sLanguagePrefArraySQL, $iPlaceID, $aPointDetails['country_code'], -1, true);
}
// Linked places // Linked places
$sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,'; $aLinkedLines = false;
$sSQL .= ' admin_level, rank_address, '; if ($bIncludeLinkedPlaces) {
$sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,"; $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
$sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, "; $sSQL .= ' admin_level, rank_address, ';
$sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, "; $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
$sSQL .= ' length(name::text) AS namelength '; $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
$sSQL .= ' FROM '; $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
$sSQL .= ' placex, '; $sSQL .= ' length(name::text) AS namelength ';
$sSQL .= ' ( '; $sSQL .= ' FROM ';
$sSQL .= ' SELECT centroid AS placegeometry '; $sSQL .= ' placex, ';
$sSQL .= ' FROM placex '; $sSQL .= ' ( ';
$sSQL .= " WHERE place_id = $iPlaceID "; $sSQL .= ' SELECT centroid AS placegeometry ';
$sSQL .= ' ) AS x'; $sSQL .= ' FROM placex ';
$sSQL .= " WHERE linked_place_id = $iPlaceID"; $sSQL .= " WHERE place_id = $iPlaceID ";
$sSQL .= ' ORDER BY '; $sSQL .= ' ) AS x';
$sSQL .= ' rank_address ASC, '; $sSQL .= " WHERE linked_place_id = $iPlaceID";
$sSQL .= ' rank_search ASC, '; $sSQL .= ' ORDER BY ';
$sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), "; $sSQL .= ' rank_address ASC, ';
$sSQL .= ' housenumber'; $sSQL .= ' rank_search ASC, ';
$aLinkedLines = $oDB->getAll($sSQL); $sSQL .= " get_name_by_language(name, $sLanguagePrefArraySQL), ";
if (PEAR::isError($aLinkedLines)) { // possible timeout $sSQL .= ' housenumber';
$aLinkedLines = []; $aLinkedLines = $oDB->getAll($sSQL);
if (PEAR::isError($aLinkedLines)) { // possible timeout
$aLinkedLines = [];
}
} }
// All places this is an imediate parent of // All places this is an imediate parent of
$sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,'; $aParentOfLines = false;
$sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,"; if ($bIncludeChildPlaces) {
$sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, "; $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
$sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, "; $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
$sSQL .= ' length(name::text) AS namelength '; $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
$sSQL .= ' FROM '; $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
$sSQL .= ' ( '; $sSQL .= ' length(name::text) AS namelength ';
$sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name '; $sSQL .= ' FROM ';
$sSQL .= ' FROM placex '; $sSQL .= ' ( ';
$sSQL .= " WHERE parent_place_id = $iPlaceID "; $sSQL .= ' SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
$sSQL .= ' ORDER BY '; $sSQL .= ' FROM placex ';
$sSQL .= ' rank_address ASC, '; $sSQL .= " WHERE parent_place_id = $iPlaceID ";
$sSQL .= ' rank_search ASC '; $sSQL .= ' ORDER BY ';
$sSQL .= ' LIMIT 500 '; $sSQL .= ' rank_address ASC, ';
$sSQL .= ' ) AS obj,'; $sSQL .= ' rank_search ASC ';
$sSQL .= ' ( '; $sSQL .= ' LIMIT 500 ';
$sSQL .= ' SELECT centroid AS placegeometry '; $sSQL .= ' ) AS obj,';
$sSQL .= ' FROM placex '; $sSQL .= ' ( ';
$sSQL .= " WHERE place_id = $iPlaceID "; $sSQL .= ' SELECT centroid AS placegeometry ';
$sSQL .= ' ) AS x'; $sSQL .= ' FROM placex ';
$sSQL .= ' ORDER BY '; $sSQL .= " WHERE place_id = $iPlaceID ";
$sSQL .= ' rank_address ASC, '; $sSQL .= ' ) AS x';
$sSQL .= ' rank_search ASC, '; $sSQL .= ' ORDER BY ';
$sSQL .= ' localname, '; $sSQL .= ' rank_address ASC, ';
$sSQL .= ' housenumber'; $sSQL .= ' rank_search ASC, ';
$aParentOfLines = $oDB->getAll($sSQL); $sSQL .= ' localname, ';
if (PEAR::isError($aParentOfLines)) { // possible timeout $sSQL .= ' housenumber';
$aParentOfLines = []; $aParentOfLines = $oDB->getAll($sSQL);
if (PEAR::isError($aParentOfLines)) { // possible timeout
$aParentOfLines = [];
}
} }
$aPlaceSearchNameKeywords = false; $aPlaceSearchNameKeywords = false;