From 6539f83cafb77609c7ab15ab81ba9ba6976dcc6a Mon Sep 17 00:00:00 2001 From: Sam <78538841+spwoodcock@users.noreply.github.com> Date: Mon, 11 Sep 2023 15:37:13 +0200 Subject: [PATCH 1/3] fix: json2osm handle polygons and points (#192) --- osm_fieldwork/json2osm.py | 62 +++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/osm_fieldwork/json2osm.py b/osm_fieldwork/json2osm.py index 3ae9b673..1ce2a619 100755 --- a/osm_fieldwork/json2osm.py +++ b/osm_fieldwork/json2osm.py @@ -25,6 +25,7 @@ import sys import json import geojson +import shapely from sys import argv from osm_fieldwork.convert import Convert, escape from osm_fieldwork.osmfile import OsmFile @@ -203,9 +204,9 @@ def parse(self, else: log.error("Need to specify a JSON or GeoJson file!") return all_tags - elif type(data) == str: + elif isinstance(data, str): reader = geojson.loads(data) - elif type(data) == list: + elif isinstance(data, list): reader = data total = list() @@ -221,6 +222,12 @@ def parse(self, # log.info(f"ROW: {row}") tags = dict() if 'geometry' in row: + # If geom not point, convert to centroid + if row['geometry']['type'] != 'Point': + log.debug(f"Converting {row['geometry']['type']} geometry to centroid point") + geom = shapely.from_geojson(str(row)) + centroid = shapely.to_geojson(geom.centroid) + row['geometry'] = centroid tags['geometry'] = row['geometry'] else: pat = re.compile("[-0-9.]*, [0-9.-]*, [0-9.]*") @@ -253,13 +260,15 @@ def parse(self, # a JSON file from ODK Central always uses coordinates as # the keyword if key == 'coordinates': - if type(v) == list: + if isinstance(v, list): lat = v[1] lon = v[0] tags['geometry'] = f"{lat} {lon}" continue tags[key] = v total.append(tags) + + log.debug(f"Finsished parsing JSON file {filespec}") return total def createEntry(self, @@ -298,31 +307,30 @@ def createEntry(self, # Otherwise use the GPS coordinates where you are. lat = None lon = None - if type(value) == float: + if isinstance(value, float): continue # log.debug(f"FIXME: {key} = {value} {type(value)}") if key == "xid" and value is not None: attrs['id'] = int(value) if key == "geometry": - # The GeoJson file has the geometry field. Usually it's a dict + # The GeoJson file has the geometry field. Usually it's a list # but on occasion it's a string instead, so turn it into a list - if type(value) == str: - if value[0] == '[': - coords = eval(value) - lat = coords[1] - lon = coords[0] - else: - coords = value.split(' ') - lat = coords[0] - lon = coords[1] - # log.debug(f"VALUE STRING: {coords}") - elif type(value) == geojson.geometry.Point: - lat = value['coordinates'][1] - lon = value['coordinates'][0] - # log.debug(f"VALUE POINT: {lat}/{lon}") - elif type(value) == list: - lat = float(value[1]) - lon = float(value[0]) + if isinstance(value, str) and len(coords:=value.split(' ')) >= 2: + lat = coords[0] + lon = coords[1] + + # Parse as geojson + else: + geom = shapely.from_geojson(str(value)) + + if geom.geom_type != 'Point': + # Use centroid if polygon + geom = geom.centroid + + # Get coords from point + lat = geom.y + lon = geom.x + attrs["lat"] = lat attrs["lon"] = lon # log.debug(f"ATTRS: {attrs}") @@ -342,7 +350,9 @@ def createEntry(self, # tags[entry] = "yes" # continue - if value is not None and value != "no" and value != "unknown": + if isinstance(value, str) and (value == "no" or value == "unknown"): + pass + elif value is not None: if key == "track" or key == "geoline": refs.append(tag) log.debug("Adding reference %s" % tag) @@ -351,9 +361,9 @@ def createEntry(self, priv[key] = value else: item = self.convertEntry(key, value) - if item is not None and type(item) == dict: + if item is not None and isinstance(item, dict): tags.update(item) - elif type(item) == list: + elif isinstance(item, list): for entry in item: tags.update(entry) @@ -408,7 +418,7 @@ def json2osm(input_file, yaml_file=None): if len(feature) > 0: if "lat" not in feature["attrs"]: if 'geometry' in feature['tags']: - if type(feature['tags']['geometry']) == str: + if isinstance(feature["tags"]["geometry"], str): coords = list(feature['tags']['geometry']) # del feature['tags']['geometry'] elif 'coordinates' in feature['tags']: From ba896e87026bebeca82c61ba4664f2d55eee14d4 Mon Sep 17 00:00:00 2001 From: Sam <78538841+spwoodcock@users.noreply.github.com> Date: Mon, 11 Sep 2023 15:41:15 +0200 Subject: [PATCH 2/3] =?UTF-8?q?bump:=20version=200.3.6rc1=20=E2=86=92=200.?= =?UTF-8?q?3.6rc2=20(#193)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 16 ++++++++++++++++ Makefile | 2 +- osm_fieldwork/__version__.py | 2 +- pyproject.toml | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d53d232..2afe3453 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +## 0.3.6rc2 (2023-09-11) + +### Fix + +- json2osm handle polygons and points (#192) +- Update raw-osmdata to 0.1.1 +- add osm-rawdata dependency +- Major refactoring to use the new osm-rawdata module +- Add join_or tag which is now required in the enhanced YAML format +- Use optional env variable for the raw data API +- initialize variable +- Arg, change the raw data URI again +- json2osm calling via api +- The URL for raw data has changed +- Add config file for waterways + ## 0.3.6rc1 (2023-09-07) ### Fix diff --git a/Makefile b/Makefile index fbc04beb..a3197701 100755 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ PACKAGE := org.osm_fieldwork.py NAME := osm-fieldwork -VERSION := 0.3.6rc1 +VERSION := 0.3.6rc2 # All python source files FILES := $(wildcard ./osm_fieldwork/*.py) diff --git a/osm_fieldwork/__version__.py b/osm_fieldwork/__version__.py index 90dd0228..46450c62 100644 --- a/osm_fieldwork/__version__.py +++ b/osm_fieldwork/__version__.py @@ -1 +1 @@ -__version__ = "0.3.6rc1" +__version__ = "0.3.6rc2" diff --git a/pyproject.toml b/pyproject.toml index a17d32ec..a106ae84 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,7 +60,7 @@ pythonpath = "osm_fieldwork" [tool.commitizen] name = "cz_conventional_commits" -version = "0.3.6rc1" +version = "0.3.6rc2" version_files = [ "pyproject.toml:version", "osm_fieldwork/__version__.py", From 8e56479de5869ff90099c0c1d33fd46f11dba607 Mon Sep 17 00:00:00 2001 From: spwoodcock Date: Mon, 11 Sep 2023 14:47:28 +0100 Subject: [PATCH 3/3] ci: remove environment from pytest run --- .github/workflows/pytest.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index beb5c3e7..adb22353 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -15,8 +15,6 @@ on: jobs: test: runs-on: ubuntu-latest - environment: - name: ${{ github.ref_name }} steps: - uses: actions/checkout@v3