Places index: drop buildings and land use, search without apostrophes and by brand
Named buildings and land-use areas are skipped unless they're also a shop/amenity/etc. A second, unshown FTS column holds the name without apostrophes and the brand tag, so "mcdonalds" finds McDonald's and "lidl" finds "Lidl Petržalka". The index records a schema version; rake places:index rebuilds older ones by itself.
This commit is contained in:
1 parent
8e4c466829
commit
f3dd37dd4e
5 files changed
+67
-12
No files matched your search
+35
-7
@@ -13,10 +13,21 @@ module Maptool
|
||||
module Places
|
||||
class ExtractError < StandardError; end
|
||||
|
||||
# Bumped whenever the table layout or what goes into it changes —
|
||||
# PmtilesBuilder rebuilds an index whose user_version is older, so a
|
||||
# plain `rake places:index` picks the change up.
|
||||
SCHEMA_VERSION = 2
|
||||
|
||||
# Most to least important; the first key a feature carries decides
|
||||
# its kind and rank. Within "place", the value refines it further.
|
||||
KIND_KEYS = %w[place natural tourism amenity historic leisure shop aeroway railway highway waterway
|
||||
boundary landuse man_made office building].freeze
|
||||
# Named buildings and land-use areas (a farm's fields, an industrial
|
||||
# estate) are a large share of every country's index and nobody looks
|
||||
# for them by name — dropped, but only when that's what the object
|
||||
# *is*: a supermarket drawn as a building is still shop=supermarket.
|
||||
SKIPPED_KEYS = %w[landuse building].freeze
|
||||
APOSTROPHES = "'’ʼ`"
|
||||
PLACE_RANK = { "city" => 0, "town" => 1, "village" => 2, "suburb" => 3, "hamlet" => 3 }.freeze
|
||||
# How much closer a settlement counts as when results are ordered by
|
||||
# distance from the map center (Search) — so typing a city's name finds
|
||||
@@ -49,20 +60,21 @@ module Maptool
|
||||
db = SQLite3::Database.new(db_path)
|
||||
db.execute(<<~SQL)
|
||||
CREATE VIRTUAL TABLE places USING fts5(
|
||||
name, kind UNINDEXED, importance UNINDEXED, lon UNINDEXED, lat UNINDEXED, bbox UNINDEXED,
|
||||
name, extra, kind UNINDEXED, importance UNINDEXED, lon UNINDEXED, lat UNINDEXED, bbox UNINDEXED,
|
||||
tokenize = 'unicode61 remove_diacritics 2', prefix = '2 3'
|
||||
)
|
||||
SQL
|
||||
db.execute("PRAGMA user_version = #{SCHEMA_VERSION}")
|
||||
# A street is many way segments, all named the same — one row per
|
||||
# name/kind per ~2 km cell keeps a town's "Hlavná" from filling the
|
||||
# whole result list. ponytail: grid dedupe, a long road still shows
|
||||
# once per cell it crosses.
|
||||
seen = Set.new
|
||||
insert = db.prepare("INSERT INTO places VALUES (?, ?, ?, ?, ?, ?)")
|
||||
insert = db.prepare("INSERT INTO places VALUES (?, ?, ?, ?, ?, ?, ?)")
|
||||
db.transaction do
|
||||
io.each_line do |line|
|
||||
row = row(JSON.parse(line))
|
||||
next unless row && seen.add?([row[0], row[1], (row[4] * 50).round, (row[3] * 50).round])
|
||||
next unless row && seen.add?([row[0], row[2], (row[5] * 50).round, (row[4] * 50).round])
|
||||
|
||||
insert.execute(row)
|
||||
rescue JSON::ParserError
|
||||
@@ -74,13 +86,29 @@ module Maptool
|
||||
db.close
|
||||
end
|
||||
|
||||
# [name, kind, importance, lon, lat, bbox json] or nil
|
||||
def self.current?(db_path)
|
||||
return false unless File.exist?(db_path)
|
||||
|
||||
db = SQLite3::Database.new(db_path, readonly: true)
|
||||
db.get_first_value("PRAGMA user_version") >= SCHEMA_VERSION
|
||||
rescue SQLite3::Exception
|
||||
false
|
||||
ensure
|
||||
db&.close
|
||||
end
|
||||
|
||||
# [name, extra, kind, importance, lon, lat, bbox json] or nil. extra is
|
||||
# searched too but never shown: the name with its apostrophes dropped
|
||||
# (FTS5 splits "McDonald's" into "mcdonald" + "s", so "mcdonalds" would
|
||||
# otherwise find nothing — Search.normalize drops them from the query
|
||||
# the same way) and the brand tag ("Lidl" for "Lidl Petržalka").
|
||||
def self.row(feature)
|
||||
tags = feature["properties"] || {}
|
||||
name = tags["name"].to_s.strip
|
||||
geometry = feature["geometry"] || {}
|
||||
coords = geometry["coordinates"]
|
||||
return nil if name.empty? || !coords.is_a?(Array) || coords.empty?
|
||||
key = KIND_KEYS.find { |k| tags[k] }
|
||||
return nil if name.empty? || !coords.is_a?(Array) || coords.empty? || SKIPPED_KEYS.include?(key)
|
||||
|
||||
points = positions(coords)
|
||||
lngs, lats = points.map { |p| p.first(2) }.transpose
|
||||
@@ -90,14 +118,14 @@ module Maptool
|
||||
# bbox center of an area.
|
||||
lon, lat = geometry["type"] == "LineString" ? points[points.size / 2] : [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2]
|
||||
|
||||
key = KIND_KEYS.find { |k| tags[k] }
|
||||
value = tags[key].to_s
|
||||
kind = if key.nil? then ""
|
||||
elsif key == "highway" && geometry["type"] != "Point" then "street"
|
||||
else value.tr("_", " ")
|
||||
end
|
||||
importance = key == "place" ? PLACE_RANK.fetch(value, KEY_RANK["place"]) : KEY_RANK[key]
|
||||
[name, kind, importance, lon, lat, JSON.generate(bbox)]
|
||||
extra = [(name.delete(APOSTROPHES) if name.count(APOSTROPHES).positive?), tags["brand"]].compact.uniq - [name]
|
||||
[name, extra.join(" "), kind, importance, lon, lat, JSON.generate(bbox)]
|
||||
end
|
||||
|
||||
def self.positions(coords)
|
||||
|
||||
@@ -419,7 +419,7 @@ module Maptool
|
||||
FileUtils.mkdir_p(places_dir(data_dir))
|
||||
sources.each do |source|
|
||||
out_path = places_country_path(data_dir, source["code"])
|
||||
next if File.exist?(out_path) && File.mtime(out_path) >= File.mtime(source["path"])
|
||||
next if File.exist?(out_path) && File.mtime(out_path) >= File.mtime(source["path"]) && Places.current?(out_path)
|
||||
|
||||
tmp_path = "#{out_path}.tmp"
|
||||
begin
|
||||
|
||||
@@ -97,9 +97,10 @@ module Maptool
|
||||
end
|
||||
end
|
||||
|
||||
# Lowercased, diacritics stripped — "kosice" finds "Košice".
|
||||
# Lowercased, diacritics and apostrophes stripped — "kosice" finds
|
||||
# "Košice", "mcdonalds" finds "McDonald's" (see Places.row's extra).
|
||||
def self.normalize(text)
|
||||
text.to_s.unicode_normalize(:nfkd).gsub(/\p{Mn}/, "").downcase
|
||||
text.to_s.unicode_normalize(:nfkd).gsub(/\p{Mn}/, "").delete(Places::APOSTROPHES).downcase
|
||||
end
|
||||
|
||||
def self.entries(path, read)
|
||||
|
||||
Reference in new issue
Block a user