diff --git a/openaddr/conform.py b/openaddr/conform.py index 80f73e7a..cc24f95b 100644 --- a/openaddr/conform.py +++ b/openaddr/conform.py @@ -550,6 +550,10 @@ def ogr_source_to_csv(source_config, source_path, dest_path): _L.debug("SRS tag found specifying %s", srs) inSpatialRef = osr.SpatialReference() inSpatialRef.ImportFromEPSG(int(srs[5:])) + + if int(osgeo.__version__[0]) >= 3: + # GDAL 3 changes axis order: https://github.com/OSGeo/gdal/issues/1546 + inSpatialRef.SetAxisMappingStrategy(osgeo.osr.OAMS_TRADITIONAL_GIS_ORDER) else: # OGR is capable of doing more than EPSG, but so far we don't need it. raise Exception("Bad SRS. Can only handle EPSG, the SRS tag is %s", srs) diff --git a/openaddr/tests/conform.py b/openaddr/tests/conform.py index bec50520..2cf9a474 100644 --- a/openaddr/tests/conform.py +++ b/openaddr/tests/conform.py @@ -1894,6 +1894,24 @@ def test_lake_man_shp_noprj_epsg26943(self): self.assertAlmostEqual(-122.2592497, rows[0]['geometry']['coordinates'][0], places=4) self.assertAlmostEqual(37.8026126, rows[0]['geometry']['coordinates'][1], places=4) + def test_lake_man_shp_epsg4269_axis_order(self): + # Regression test for https://github.com/openaddresses/batch-machine/issues/26 + # A shapefile with an explicit geographic "srs" tag (here EPSG:4269, NAD83) + # must not have its coordinates flipped by GDAL 3's authority-compliant + # (lat, lon) axis order. The underlying shapefile geometry is stored in + # ordinary (lon, lat) order, same coordinates as the plain lake-man.shp + # fixture used by test_lake_man, so the expected output here matches that + # test's expected values. Before the fix, this source's coordinates come + # out as (lat, lon) instead of (lon, lat). + rc, dest_path = self._run_conform_on_source('lake-man-epsg4269', 'shp') + self.assertEqual(0, rc) + + with open(dest_path) as fp: + rows = list(map(json.loads, list(fp))) + self.assertEqual('Point', rows[0]['geometry']['type']) + self.assertAlmostEqual(-122.2592497, rows[0]['geometry']['coordinates'][0], places=4) + self.assertAlmostEqual(37.8026126, rows[0]['geometry']['coordinates'][1], places=4) + # TODO: add tests for non-ESRI GeoJSON sources def test_lake_man_split2(self): @@ -1973,8 +1991,11 @@ def test_lake_man_gml(self): with open(dest_path) as fp: rows = list(map(json.loads, list(fp))) self.assertEqual(6, len(rows)) - self.assertAlmostEqual(37.8026126, rows[0]['geometry']['coordinates'][0], places=4) - self.assertAlmostEqual(-122.2592497, rows[0]['geometry']['coordinates'][1], places=4) + # GeoJSON coordinates are always [lon, lat]. This source has an + # explicit "srs": "EPSG:4326" tag; see + # https://github.com/openaddresses/batch-machine/issues/26. + self.assertAlmostEqual(-122.2592497, rows[0]['geometry']['coordinates'][0], places=4) + self.assertAlmostEqual(37.8026126, rows[0]['geometry']['coordinates'][1], places=4) self.assertEqual(rows[0]['properties']['number'], '5115') self.assertEqual(rows[0]['properties']['street'], 'FRUITED PLAINS LN') diff --git a/openaddr/tests/conforms/lake-man-epsg4269.dbf b/openaddr/tests/conforms/lake-man-epsg4269.dbf new file mode 100644 index 00000000..a710e002 Binary files /dev/null and b/openaddr/tests/conforms/lake-man-epsg4269.dbf differ diff --git a/openaddr/tests/conforms/lake-man-epsg4269.json b/openaddr/tests/conforms/lake-man-epsg4269.json new file mode 100644 index 00000000..4e591ef6 --- /dev/null +++ b/openaddr/tests/conforms/lake-man-epsg4269.json @@ -0,0 +1,20 @@ +{ + "schema": 2, + "layers": { + "addresses": [{ + "name": "default", + "data": "http://fake-web/lake-man-epsg4269.zip", + "cache": "http://fake-cache/lake-man-epsg4269.zip", + "protocol": "http", + "compression": "zip", + "conform": { + "lon": "X", + "lat": "Y", + "number": "NUMBER", + "street": "STRNAME", + "format": "shapefile", + "srs": "EPSG:4269" + } + }] + } +} diff --git a/openaddr/tests/conforms/lake-man-epsg4269.shp b/openaddr/tests/conforms/lake-man-epsg4269.shp new file mode 100644 index 00000000..6963b4fb Binary files /dev/null and b/openaddr/tests/conforms/lake-man-epsg4269.shp differ diff --git a/openaddr/tests/conforms/lake-man-epsg4269.shx b/openaddr/tests/conforms/lake-man-epsg4269.shx new file mode 100644 index 00000000..f7808d66 Binary files /dev/null and b/openaddr/tests/conforms/lake-man-epsg4269.shx differ