Skip to content

Commit

Permalink
update GeoJSON tests for picogeojson 0.5
Browse files Browse the repository at this point in the history
crs member requires overriding a default, and is now placed at the top
level
  • Loading branch information
njwilson23 committed Jun 30, 2017
1 parent 4cf6917 commit c3ab4fa
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
5 changes: 3 additions & 2 deletions karta/vector/_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class GeoJSONOutMixin(object):
"""

_serializer = picogeojson.Serializer(antimeridian_cutting=False,
enforce_poly_winding=False)
enforce_poly_winding=False,
write_crs=True)

@staticmethod
def _as_named_tuple(geom, **kwargs):
Expand Down Expand Up @@ -69,7 +70,7 @@ def _as_named_tuple(geom, **kwargs):

properties = geom.properties
properties.update(data)
return picogeojson.Feature(g, properties)
return picogeojson.Feature(g, properties, None, g.crs)

def as_geojson(self, indent=None, urn=None, force_wgs84=True):
""" Output representation of internal data as a GeoJSON string.
Expand Down
2 changes: 1 addition & 1 deletion karta/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.9.0"
__version__ = "0.9.1"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
setuptools >= 17.0
numpy >= 1.10
picogeojson >= 0.2
picogeojson >= 0.5.0
pyproj >= 1.9
gdal >= 1.10
blosc >= 1.2.8
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def finalize_options(self):
"pyproj>=1.9",
"gdal>=1.10",
"blosc>=1.2.8",
"picogeojson>=0.4.2"],
"picogeojson>=0.5.0"],
author = "Nat Wilson",
author_email = "natw@fortyninemaps.com",
packages = ["karta", "karta.vector", "karta.raster"],
Expand Down
54 changes: 35 additions & 19 deletions tests/geojson_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,79 +102,94 @@ class TestGeoJSONOutput(unittest.TestCase):

maxDiff = None

def verifyJson(self, json1, json2):
def verifyDict(self, d1, d2):
for key, v in d1.items():
try:
self.assertTrue(key in d2)
except AssertionError as e:
raise AssertionError("key '{}' not in dict 2".format(key))
if isinstance(v, dict):
self.verifyDict(v, d2[key])
else:
self.assertEqual(v, d2[key])
return

def verifyJSON(self, json1, json2):
""" Verify that two JSON strings are equivalent """
obj1 = json.loads(json1)
obj2 = json.loads(json2)
self.assertEqual(obj1, obj2)
self.verifyDict(obj1, obj2)
return

def test_point_write_cartesian(self):
p = Point((100.0, 0.0), crs=Cartesian)
s = p.as_geojson(urn="urn:ogc:def:crs:EPSG::5806", force_wgs84=False)
ans = """{"properties": {},"bbox": [100.0, 0.0, 100.0, 0.0], "geometry": {"coordinates": [100.0, 0.0], "crs": {"properties": {"name": "urn:ogc:def:crs:EPSG::5806"}, "type": "name"}, "type": "Point"}, "type": "Feature"}"""
self.verifyJson(s, ans)
ans = """{"properties": {},"bbox": [100.0, 0.0, 100.0, 0.0],
"geometry": {"coordinates": [100.0, 0.0], "type": "Point"},
"type": "Feature",
"crs": {"properties": {"name": "urn:ogc:def:crs:EPSG::5806"}, "type": "name"} }"""
self.verifyJSON(s, ans)
return

def test_point_write(self):
p = Point((100.0, 0.0), crs=LonLatWGS84)
s = p.as_geojson(urn="urn:ogc:def:crs:EPSG::5806")
ans = """{"properties": {}, "bbox": [100.0, 0.0, 100.0, 0.0], "geometry": {"coordinates": [100.0, 0.0], "crs": {"properties": {"name": "urn:ogc:def:crs:EPSG::5806"}, "type": "name"}, "type": "Point"}, "type": "Feature"}"""
self.verifyJson(s, ans)
ans = """{"properties": {}, "bbox": [100.0, 0.0, 100.0, 0.0], "geometry": {"coordinates": [100.0, 0.0], "type": "Point"}, "type": "Feature", "crs": {"properties": {"name": "urn:ogc:def:crs:EPSG::5806"}, "type": "name"}}"""
self.verifyJSON(s, ans)
return

def test_line_write(self):
p = Line([(100.0, 0.0), (101.0, 1.0)], crs=LonLatWGS84)
s = p.as_geojson(urn="urn:ogc:def:crs:EPSG::5806")
ans = """{"type": "Feature", "geometry": {"coordinates": [[100.0, 0.0], [101.0, 1.0]], "type": "LineString", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::5806"}}}, "properties": {}, "bbox": [100.0, 0.0, 101.0, 1.0]}"""
self.verifyJson(s, ans)
ans = """{"type": "Feature", "geometry": {"coordinates": [[100.0, 0.0], [101.0, 1.0]], "type": "LineString"}, "properties": {}, "bbox": [100.0, 0.0, 101.0, 1.0], "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::5806"}}}"""
self.verifyJSON(s, ans)
return

def test_polygon_write(self):
p = Polygon([[100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
[100.0, 1.0]], crs=LonLatWGS84)
s = p.as_geojson(urn="urn:ogc:def:crs:EPSG::5806")
ans = """{ "properties": {}, "bbox": [100.0, 0.0, 101.0, 1.0], "geometry": { "type": "Polygon", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "coordinates": [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] }, "type": "Feature" }"""
ans = """{ "properties": {}, "bbox": [100.0, 0.0, 101.0, 1.0], "geometry": { "type": "Polygon", "coordinates": [ [ [ 100.0, 0.0 ], [ 101.0, 0.0 ], [ 101.0, 1.0 ], [ 100.0, 1.0 ], [ 100.0, 0.0 ] ] ] }, "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "type": "Feature" }"""

self.verifyJson(s, ans)
self.verifyJSON(s, ans)
return

def test_multiline_write(self):
p = Multiline([[(100, 0), (101, 1)], [(102, 2), (103, 3)]], crs=LonLatWGS84)
s = p.as_geojson(urn="urn:ogc:def:crs:EPSG::5806")
ans = """{"type": "Feature", "properties": {}, "bbox": [100.0, 0.0, 103.0, 3.0], "geometry" : { "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] } }"""
ans = """{"type": "Feature", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "properties": {}, "bbox": [100.0, 0.0, 103.0, 3.0], "geometry" : { "type": "MultiLineString", "coordinates": [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] } }"""

self.verifyJson(s, ans)
self.verifyJSON(s, ans)

def test_multipolygon_write(self):
p = Multipolygon([[[(102, 2), (103, 2), (103, 3), (102, 3)]],
[[(100, 0), (101, 0), (101, 1), (100, 1)],
[(100.2, 0.2), (100.8, 0.2), (100.8, 0.8), (100.2, 0.8)]]],
crs=LonLatWGS84)
s = p.as_geojson(urn="urn:ogc:def:crs:EPSG::5806")
ans = """{"type": "Feature", "properties": {},"bbox": [100.0, 0.0, 103.0, 3.0], "geometry" : { "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "type": "MultiPolygon",
ans = """{"type": "Feature", "properties": {},"bbox": [100.0, 0.0, 103.0, 3.0], "geometry" : { "type": "MultiPolygon",
"coordinates": [
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
]
} }"""
}, "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } } }"""

self.verifyJson(s, ans)
self.verifyJSON(s, ans)

def test_write_reproject(self):
# tests whether coordinates are correctly reprojected to WGS84 lon/lat
p = Line([(1e6, 1e6), (1.2e6, 1.4e6)], crs=WebMercator)
s = p.as_geojson()
ans = """{ "type": "Feature", "properties": {},
"bbox": [8.983152841195214, 8.946573850543412, 10.779783409434257, 12.476624651238847],
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"geometry": {
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"coordinates": [[8.983152841195214, 8.946573850543412],
[10.779783409434257, 12.476624651238847]],
"type": "LineString" } }"""

self.verifyJson(s, ans)
self.verifyJSON(s, ans)
return

def test_write_string_data(self):
Expand All @@ -192,8 +207,9 @@ def test_write_string_data(self):
"Lincoln, Nebraska"]},
crs=LonLatWGS84)
s = capitols.as_geojson(urn="urn:ogc:def:crs:EPSG::5806")
ans = """{"bbox": [-121.5, 33.57, -71.02, 46.6], "properties": { "n": [ "Phoenix, Arizona", "Sacramento, California", "Atlanta, Georgia", "Indianapolis, Indiana", "Helena, Montana", "Columbus, Ohio", "Richmond, Virginia", "Topeka, Kansas", "Boston, Massachusetts", "Lincoln, Nebraska" ] }, "type": "Feature", "geometry": {"type": "MultiPoint", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } }, "coordinates": [ [ -112.1, 33.57 ], [ -121.5, 38.57 ], [ -84.42, 33.76 ], [ -86.15, 39.78 ], [ -112.0, 46.6 ], [ -82.99, 39.98 ], [ -77.48, 37.53 ], [ -95.69, 39.04 ], [ -71.02, 42.33 ], [ -96.68, 40.81 ] ] } } """
self.verifyJson(s, ans)
d = json.loads(s)
ans = """{"bbox": [-121.5, 33.57, -71.02, 46.6], "properties": { "n": [ "Phoenix, Arizona", "Sacramento, California", "Atlanta, Georgia", "Indianapolis, Indiana", "Helena, Montana", "Columbus, Ohio", "Richmond, Virginia", "Topeka, Kansas", "Boston, Massachusetts", "Lincoln, Nebraska" ] }, "type": "Feature", "geometry": {"type": "MultiPoint", "coordinates": [ [ -112.1, 33.57 ], [ -121.5, 38.57 ], [ -84.42, 33.76 ], [ -86.15, 39.78 ], [ -112.0, 46.6 ], [ -82.99, 39.98 ], [ -77.48, 37.53 ], [ -95.69, 39.04 ], [ -71.02, 42.33 ], [ -96.68, 40.81 ] ] }, "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::5806" } } } """
self.verifyJSON(s, ans)
return

def test_write_data_crs(self):
Expand Down

0 comments on commit c3ab4fa

Please sign in to comment.