Skip to content

Commit

Permalink
implement fromdict and todict functions
Browse files Browse the repository at this point in the history
  • Loading branch information
njwilson23 committed Jun 29, 2017
1 parent 115cc97 commit 47fad9e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion picogeojson/__init__.py
Expand Up @@ -7,7 +7,7 @@

from .geojson import (Serializer, Deserializer,
DEFAULTCRS,
fromfile, fromstring, tofile, tostring,
fromfile, fromstring, fromdict, tofile, tostring, todict,
load, dump, loads, dumps,
result_fromstring, result_fromfile)

Expand Down
20 changes: 17 additions & 3 deletions picogeojson/geojson.py
Expand Up @@ -127,7 +127,7 @@ def _parseMultiLineString(self, d):

def _parsePolygon(self, d):
crs = d.get("crs", self.defaultcrs)
geom = Polygon(d["coordinates"], crs)
geom = Polygon(copy.copy(d["coordinates"]), crs)
if self.enforce_poly_winding:
for i, ring in enumerate(geom.coordinates):
if bool(i) is is_counterclockwise(ring):
Expand All @@ -136,7 +136,7 @@ def _parsePolygon(self, d):

def _parseMultiPolygon(self, d):
crs = d.get("crs", self.defaultcrs)
geom = MultiPolygon(d["coordinates"], crs)
geom = MultiPolygon(copy.deepcopy(d["coordinates"]), crs)
if self.enforce_poly_winding:
for j, coords in enumerate(geom.coordinates):
for i, ring in enumerate(coords):
Expand Down Expand Up @@ -255,7 +255,7 @@ def geojson_asdict(self, geom, root=True):

if self.enforce_poly_winding:
if type(geom).__name__ == "Polygon":
d["coordinates"] = copy.deepcopy(d["coordinates"])
d["coordinates"] = copy.copy(d["coordinates"])
cx = d["coordinates"]
for i, ring in enumerate(cx):
if bool(i) is is_counterclockwise(ring):
Expand Down Expand Up @@ -320,6 +320,13 @@ def fixed_precision(A, prec=6):
else:
return round(A, prec)

@docstring_insert(deserializer_args)
def fromdict(dct, **kw):
""" Read a dictionary and return the GeoJSON object.
{} """
d = Deserializer(**kw)
return d.deserialize(dct)

@docstring_insert(deserializer_args)
def fromfile(f, **kw):
""" Read a JSON file and return the GeoJSON object.
Expand Down Expand Up @@ -348,6 +355,13 @@ def result_fromstring(s, **kw):
d = Deserializer(**kw)
return GeoJSONResult(d.fromstring(s))

@docstring_insert(serializer_args)
def todict(geom, **kw):
""" Serialize *geom* to a dictionary.
{} """
s = Serializer(**kw)
return s.geojson_asdict(geom)

@docstring_insert(serializer_args)
def tostring(geom, **kw):
""" Serialize *geom* to a JSON string.
Expand Down
29 changes: 29 additions & 0 deletions tests/tests.py
Expand Up @@ -29,6 +29,23 @@ def test_shorthand(self):
self.assertEqual(res.coordinates, [100.0, 0.0])
return

def test_fromdict(self):
d = {
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]]
},
"properties": {
"cover": "water",
"color": "blue"
}
}
geom = picogeojson.fromdict(d)
self.assertEqual(d["geometry"]["coordinates"], geom.geometry.coordinates)
self.assertEqual(d["properties"], geom.properties)
return

def test_shorthand_result(self):
res = picogeojson.result_fromfile(os.path.join(TESTDATA, 'point.json'))
self.assertEqual(type(res), GeoJSONResult)
Expand Down Expand Up @@ -174,6 +191,18 @@ def test_shorthand_compat(self):
d = json.loads(picogeojson.dumps(pt))
self.assertEqual(tuple(pt.coordinates), tuple(d["coordinates"]))

def test_todict(self):
geom = picogeojson.Feature(picogeojson.Polygon([[(0, 0), (1, 0),
(1, 1), (0, 1),
(0, 0)]]),
{"cover": "water", "color": "blue"})

d = picogeojson.todict(geom)
self.assertEqual(d["geometry"]["coordinates"], geom.geometry.coordinates)
self.assertEqual(d["properties"], geom.properties)
return


def test_serialize_point(self):
pt = picogeojson.Point((44.0, 17.0), DEFAULTCRS)
s = self.serializer(pt)
Expand Down

0 comments on commit 47fad9e

Please sign in to comment.