Bas Couwenberg pushed to branch upstream at Debian GIS Project / python-geojson
Commits: 1de69b33 by Bas Couwenberg at 2018-10-18T12:52:00Z New upstream version 2.4.1 - - - - - 6 changed files: - CHANGELOG.rst - README.rst - geojson/_version.py - geojson/geometry.py - geojson/utils.py - tests/test_coords.py Changes: ===================================== CHANGELOG.rst ===================================== @@ -1,6 +1,13 @@ Changes ======= +2.4.1 (2018-10-17) +------------------ + +- Allow ``FeatureCollections`` to be passed to ``coords`` + + - https://github.com/frewsxcv/python-geojson/pull/117 + 2.4.0 (2018-05-21) ------------------ ===================================== README.rst ===================================== @@ -224,7 +224,7 @@ Visualize the result of the example above `here <https://gist.github.com/frewsxc GeoJSON encoding/decoding ------------------------- -All of the GeoJSON Objects implemented in this library can be encoded and decoded into raw GeoJSON with the ``geojson.dump``, ``geojson.dumps``, ``geojson.load``, and ``geojson.loads`` functions. +All of the GeoJSON Objects implemented in this library can be encoded and decoded into raw GeoJSON with the ``geojson.dump``, ``geojson.dumps``, ``geojson.load``, and ``geojson.loads`` functions. Note that each of these functions is a wrapper around the core `json` function with the same name, and will pass through any additional arguments. This allows you to control the JSON formatting or parsing behavior with the underlying core `json` functions. .. code:: python ===================================== geojson/_version.py ===================================== @@ -1,2 +1,2 @@ -__version__ = "2.4.0" +__version__ = "2.4.1" __version_info__ = tuple(map(int, __version__.split("."))) ===================================== geojson/geometry.py ===================================== @@ -21,7 +21,7 @@ class Geometry(GeoJSON): Initialises a Geometry object. :param coordinates: Coordinates of the Geometry object. - :type coordinates: tuple + :type coordinates: tuple or list of tuple :param crs: CRS :type crs: CRS object """ ===================================== geojson/utils.py ===================================== @@ -10,19 +10,26 @@ def coords(obj): :return: A generator with coordinate tuples from the geometry or feature. :rtype: generator """ - - if isinstance(obj, (tuple, list)): - coordinates = obj - elif 'geometry' in obj: - coordinates = obj['geometry']['coordinates'] + # Handle recursive case first + if 'features' in obj: + for f in obj['features']: + # For Python 2 compatibility + # See https://www.reddit.com/r/learnpython/comments/4rc15s/yield_from_and_python_27/ # noqa: E501 + for c in coords(f): + yield c else: - coordinates = obj.get('coordinates', obj) - for e in coordinates: - if isinstance(e, (float, int)): - yield tuple(coordinates) - break - for f in coords(e): - yield f + if isinstance(obj, (tuple, list)): + coordinates = obj + elif 'geometry' in obj: + coordinates = obj['geometry']['coordinates'] + else: + coordinates = obj.get('coordinates', obj) + for e in coordinates: + if isinstance(e, (float, int)): + yield tuple(coordinates) + break + for f in coords(e): + yield f def map_coords(func, obj): ===================================== tests/test_coords.py ===================================== @@ -27,6 +27,14 @@ class CoordsTestCase(unittest.TestCase): self.assertEqual(pairs[0], (3.78, 9.28)) self.assertEqual(pairs[-1], (23.18, -34.29)) + def test_featurecollection(self): + p1 = geojson.Feature(geometry=geojson.Point((-115.11, 37.11))) + p2 = geojson.Feature(geometry=geojson.Point((-115.22, 37.22))) + itr = coords(geojson.FeatureCollection([p1, p2])) + pairs = list(itr) + self.assertEqual(pairs[0], (-115.11, 37.11)) + self.assertEqual(pairs[1], (-115.22, 37.22)) + def test_map_point(self): result = map_coords(lambda x: x, geojson.Point((-115.81, 37.24))) self.assertEqual(result['type'], 'Point') View it on GitLab: https://salsa.debian.org/debian-gis-team/python-geojson/commit/1de69b33033be9b1319660c922d3474ba83f30c4 -- View it on GitLab: https://salsa.debian.org/debian-gis-team/python-geojson/commit/1de69b33033be9b1319660c922d3474ba83f30c4 You're receiving this email because of your account on salsa.debian.org.
_______________________________________________ Pkg-grass-devel mailing list [email protected] https://alioth-lists.debian.net/cgi-bin/mailman/listinfo/pkg-grass-devel
