From 917136d540f60c6e9f59aa11dfbc6a6e167853ef Mon Sep 17 00:00:00 2001 From: Hartmut Holzgraefe Date: Sun, 23 Sep 2018 20:17:42 +0000 Subject: [PATCH] use local places database instead of nominatim for city search --- scripts/import-places.php | 121 +++++++++++++++++++++++++++++++ www/maposmatic/views.py | 60 +++++++++++++++ www/static/img/place-node.png | Bin 0 -> 214 bytes www/static/img/place-polygon.png | Bin 0 -> 499 bytes www/urls.py | 3 +- 5 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 scripts/import-places.php create mode 100644 www/static/img/place-node.png create mode 100644 www/static/img/place-polygon.png diff --git a/scripts/import-places.php b/scripts/import-places.php new file mode 100644 index 00000000..74903922 --- /dev/null +++ b/scripts/import-places.php @@ -0,0 +1,121 @@ + $val) { + switch($columns[$key]) { + case 'name': + case 'alternative_names': + case 'osm_type': + case 'class': + case 'type': + case 'country_code': + case 'display_name': + $row[$columns[$key]] = pg_escape_literal($db, $val); + break; + case 'osm_id': + case 'place_rank': + case 'importance': + case 'lat': + case 'lon': + case 'west': + case 'east': + case 'north': + case 'south': + if (empty($val)) $val = 0; + if ($val == "NaN") $val = "NULL"; + $row[$columns[$key]] = $val; + break; + } + } + + if (!isset($row['class'])) { + // echo "no class\n"; + continue; + } + if (in_array(substr($row['class'],1,-1), $ignore_classes)) { + // echo "ignoring class\n"; + continue; + } + + if (strstr($row['type'], ",")) { + // echo "comma in type\n"; + continue; + } + + foreach ($ignore_columns as $key) { + unset($row[$key]); + } + + if (empty($row['place_rank'])) $row['place_rank'] = 0.0; + if (empty($row['importance'])) $row['importance'] = 0.0; + + $query = "INSERT INTO place (" . join(",", array_keys($row)) . ") VALUES (" . join(",", $row) . ");"; + + if (!pg_query($db, $query)) { + echo "$query\n"; + echo pg_last_error($db)."\n\n"; + exit; + } + + $i++; +} + +echo "COMMIT\n"; +pg_query($db, "COMMIT") or die("ooops6"); diff --git a/www/maposmatic/views.py b/www/maposmatic/views.py index 8c4cb94a..4160fd37 100644 --- a/www/maposmatic/views.py +++ b/www/maposmatic/views.py @@ -327,6 +327,7 @@ def api_postgis_reverse(request, lat, lon): cursor.execute(query) country_code = cursor.fetchone() + cursor.close() if country_code is None or len(country_code) < 1: raise Http404("postgis: country not found") @@ -340,6 +341,65 @@ def api_postgis_reverse(request, lat, lon): raise Http404("postgis: something went wrong") +def api_geosearch(request): + """Simple place name search.""" + exclude = request.GET.get('exclude', '') + squery = request.GET.get('q', '') + + contents = { "entries": [] } + + cursor = None + query = """SELECT name, display_name, class, type + , osm_type, osm_id + , lat, lon, west, east, north, south + , place_rank, importance + FROM place + WHERE name = '%s' + ORDER BY place_rank, importance DESC""" % squery + + try: + db = gisdb.get() + if db is None: + raise Http404("postgis: no database") + + cursor = db.cursor() + if cursor is None: + raise Http404("postgis: no cursor") + + cursor.execute(query) + + columns = [col[0] for col in cursor.description] + + for row in cursor.fetchall(): + values = dict(zip(columns, row)) + + values["boundingbox"] = "%f,%f,%f,%f" % (values["south"], values["north"], values["west"], values["east"]) + + if values["osm_type"] == "node": + values["icon"] = "../media/img/place-node.png" + values["ocitysmap_params"] = { + "valid": False, + "reason": "no-admin", + "reason_text": "No administrative boundary" + } + else: + values["icon"] = "../media/img/place-polygon.png" + values["ocitysmap_params"] = { + "valid": 1, + "table": "polygon", + "id": -values["osm_id"] + } + + contents["entries"].append(values) + + cursor.close() + + return HttpResponse(content=json.dumps(contents), + content_type='text/json') + + except Exception as e: + raise Http500(e) + def api_papersize(request): """API handler to get the compatible paper sizes for the provided layout diff --git a/www/static/img/place-node.png b/www/static/img/place-node.png new file mode 100644 index 0000000000000000000000000000000000000000..b70092f74ce6c338be72f1769385630f77143c7e GIT binary patch literal 214 zcmeAS@N?(olHy`uVBq!ia0vp^A|TAc1|)ksWqE-VTavfC3&Vd9T(EcfWS~g5r;B5V zMeo}y8~GRm1zI2Wi!NFiu&P4Xe^YPzl?v`om#w{RlP(C(lD^6DF0W|uF7=r{Mw36E zU)g4#-OJUztj#YaKCzAWn?zd^lWJM6kle=~f#E){+u~R%xc@c;dzf-;whK$V9&vj@ z%1<+WgI_5xwk^C~a@`2^&iCy1~Xvl$26RO8x|Y zfemG0Y?LHBThWMQ!HOm&FGb$fC^O9{%)Q+)7U$f0db&^dO8M5)^ZT97`Tov3{|WsV z$n$o5!8Ps2Rl)07yvKFitMal6 zeb|jL%%wLMa0qL$uv?-U&fpGS;zts;zTYDBw^-SUY#lxojHmG~&u=h_r}&5oobN<9 zA}TtDtJsA7NqD26H-I&r$coBE>}x|26={mhhj@+eS8ogV>81EKB)L+!e#n zOxa?r5XCy(BJ35d@j+DhJ6_;&Mz9Tg@JzJR0#Ur5X*Y@|MGu=ej1xG3EhUd9O816n zonefNsd-cm`2lX?c$*ZIF^5ZeuHj_fH>2K~<%a0Lk$i2kK(Hj0U*lnkdA+lFZT=QxG&s(lDYa5NvN pi`>oNc1ALoPTa@0E+hGS`~iRyX!gWboW}qF002ovPDHLkV1i{g>3IME literal 0 HcmV?d00001 diff --git a/www/urls.py b/www/urls.py index 15c6f974..f3ed3938 100644 --- a/www/urls.py +++ b/www/urls.py @@ -72,7 +72,8 @@ urlpatterns = [ name='donate-thanks'), # API calls used by the web frontend - url(r'^apis/nominatim/$', views.api_nominatim), + # url(r'^apis/nominatim/$', views.api_nominatim), + url(r'^apis/nominatim/$', views.api_geosearch), url(r'^apis/reversegeo/([^/]*)/([^/]*)/$', views.api_postgis_reverse), url(r'^apis/papersize', views.api_papersize), url(r'^apis/boundingbox/([^/]*)/$', views.api_bbox),