add bounding box detection for Umap and Poi files (Issue #70, WiP)

fossgis-logo
Hartmut Holzgraefe 2023-03-21 01:32:42 +00:00
rodzic 8ec8908cfa
commit c7d71fd8ee
3 zmienionych plików z 79 dodań i 9 usunięć

Wyświetl plik

@ -23,11 +23,34 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from . import Stylesheet
from coords import BoundingBox
import codecs
import json
import logging
LOG = logging.getLogger('ocitysmap')
def PoiBounds(poi_file):
fp = codecs.open(poi_file, "r", "utf-8-sig")
poi = json.load(fp)
fp.close()
min_lat = float(poi['center_lat'])
max_lat = float(poi['center_lat'])
min_lon = float(poi['center_lon'])
max_lon = float(poi['center_lon'])
for group in poi['nodes']:
for node in group['nodes']:
min_lat = min(min_lat, node['lat'])
min_lon = min(min_lon, node['lon'])
max_lat = max(max_lat, node['lat'])
max_lon = max(max_lon, node['lon'])
return BoundingBox(min_lat, min_lon, max_lat, max_lon)
class PoiStylesheet(Stylesheet):
def __init__(self, poi_file, tmpdir):

Wyświetl plik

@ -23,6 +23,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from . import Stylesheet
from coords import BoundingBox
import os
import json
@ -88,6 +89,40 @@ def get_default_properties(json, umap_defaults, create_copy=True):
if create_copy:
return umap_defaults
def UmapBounds(umap_file):
fp = codecs.open(umap_file, 'r', 'utf-8-sig')
umap = json.load(fp)
fp.close()
lon_vals = []
lat_vals = []
# UMAP files have one or more layers with extended GeoJSON within
# general GeoJson files do not have that, but by treating the whole
# file as a layer we can render both general GeoJSON and UMAP files
if 'layers' in umap:
layers = umap['layers']
else:
layers = [ umap ]
for layer in layers:
for feature in layer['features']:
geom = feature['geometry']
for coord in geom['coordinates']:
if type(coord) == float: # then its a point feature
lon_vals.append(geom['coordinates'][0])
lat_vals.append(geom['coordinates'][1])
elif type(coord) == list:
for c in coord:
if type(c) == float: # then its a linestring feature
lon_vals.append(coord[0])
lat_vals.append(coord[1])
elif type(c) == list: # then its a polygon feature
lon_vals.append(c[0])
lat_vals.append(c[1])
return BoundingBox(min(lat_vals), min(lon_vals), max(lat_vals), max(lon_vals))
class UmapStylesheet(Stylesheet):
def __init__(self, umap_file, tmpdir):
super().__init__()

Wyświetl plik

@ -33,7 +33,10 @@ import re
import ocitysmap
import ocitysmap.layoutlib.renderers
from coords import BoundingBox
from stylelib.Gpx import GpxBounds
from stylelib.Gpx import GpxBounds
from stylelib.Umap import UmapBounds
from stylelib.Poi import PoiBounds
from pprint import pprint
@ -280,19 +283,28 @@ def main():
% ( options.paper_format,
', '.join(paper_format_names)))
# add actual import files
# get bounding box information from import files
# TODO: support legacy options?
# TODO: also extract title information
if options.import_file:
for import_file in options.import_file:
import_file = os.path.realpath(import_file)
file_type = ocitysmap.guess_filetype(import_file)
print("%s - %s" % (import_file, file_type))
if file_type == 'gpx':
gpx_bbox = GpxBounds(import_file).create_padded(0.1)
if bbox:
bbox.merge(gpx_bbox)
else:
bbox = gpx_bbox
file_bbox = None
if file_type == 'gpx':
file_bbox = GpxBounds(import_file)
if file_type == 'umap':
file_bbox = UmapBounds(import_file)
if file_type == 'poi':
file_bbox = PoiBounds(import_file)
if file_bbox:
file_bbox = file_bbox.create_padded(0.1)
if bbox:
bbox.merge(file_bbox)
else:
bbox = file_bbox
# Parse bounding box arguments when given
# This overrides any implicit bounding box settings