Skip to content

Commit

Permalink
Merge pull request #250 from perrygeo/pre-0-16
Browse files Browse the repository at this point in the history
fix shapely deprecation warnings
  • Loading branch information
perrygeo authored Oct 29, 2021
2 parents f41f5f3 + ade5936 commit 75d6ef0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
0.16.0
- Fix deprecation warning with shapely 1.8+ #250

0.15.0
- Fix deprecation warning with Affine #211
- Avoid unnecessary memory copy operation #213
Expand Down
2 changes: 1 addition & 1 deletion src/rasterstats/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.15.0"
__version__ = "0.16.0"
20 changes: 8 additions & 12 deletions src/rasterstats/io.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
import sys
import json
import math
import fiona
Expand All @@ -12,29 +11,27 @@
from rasterio.enums import MaskFlags
from affine import Affine
import numpy as np
from shapely import wkt, wkb

try:
from shapely.errors import ReadingError
except:
except ImportError: # pragma: no cover
from shapely.geos import ReadingError

try:
from json.decoder import JSONDecodeError
except ImportError:
except ImportError: # pragma: no cover
JSONDecodeError = ValueError
from shapely import wkt, wkb

try:
from collections.abc import Iterable, Mapping
except:
except ImportError: # pragma: no cover
from collections import Iterable, Mapping


geom_types = ["Point", "LineString", "Polygon",
"MultiPoint", "MultiLineString", "MultiPolygon"]

PY3 = sys.version_info[0] >= 3
if PY3:
string_types = str, # pragma: no cover
else:
string_types = basestring, # pragma: no cover

def wrap_geom(geom):
""" Wraps a geometry dict in an GeoJSON Feature
Expand Down Expand Up @@ -85,8 +82,7 @@ def parse_feature(obj):

def read_features(obj, layer=0):
features_iter = None

if isinstance(obj, string_types):
if isinstance(obj, str):
try:
# test it as fiona data source
with fiona.open(obj, 'r', layer=layer) as src:
Expand Down
10 changes: 7 additions & 3 deletions src/rasterstats/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ def geom_xys(geom):
geoms = [geom]

for g in geoms:
arr = g.array_interface_base['data']
for pair in zip(arr[::2], arr[1::2]):
yield pair
if hasattr(g, "exterior"):
yield from geom_xys(g.exterior)
for interior in g.interiors:
yield from geom_xys(interior)
else:
for pair in g.coords:
yield pair


def point_query(*args, **kwargs):
Expand Down
12 changes: 10 additions & 2 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
[1, 1, 1],
[1, 1, 1]]])

eps = 1e-6

with fiona.open(polygons, 'r') as src:
target_features = [f for f in src]

Expand All @@ -31,7 +33,7 @@

def _compare_geomlists(aa, bb):
for a, b in zip(aa, bb):
assert a.almost_equals(b)
assert a.equals_exact(b, eps)


def _test_read_features(indata):
Expand All @@ -44,7 +46,7 @@ def _test_read_features(indata):
def _test_read_features_single(indata):
# single (first target geom)
geom = shape(list(read_features(indata))[0]['geometry'])
assert geom.almost_equals(target_geoms[0])
assert geom.equals_exact(target_geoms[0], eps)


def test_fiona_path():
Expand Down Expand Up @@ -375,3 +377,9 @@ def test_geodataframe():
except ImportError:
pytest.skip("Can't import geopands")
assert list(read_features(df))


# TODO # io.parse_features on a feature-only geo_interface
# TODO # io.parse_features on a feature-only geojson-like object
# TODO # io.read_features on a feature-only
# TODO # io.Raster.read() on an open rasterio dataset
14 changes: 13 additions & 1 deletion tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,30 @@ def test_geom_xys():
Polygon, MultiPolygon)
pt = Point(0, 0)
assert list(geom_xys(pt)) == [(0, 0)]

mpt = MultiPoint([(0, 0), (1, 1)])
assert list(geom_xys(mpt)) == [(0, 0), (1, 1)]

line = LineString([(0, 0), (1, 1)])
assert list(geom_xys(line)) == [(0, 0), (1, 1)]

mline = MultiLineString([((0, 0), (1, 1)), ((-1, 0), (1, 0))])
assert list(geom_xys(mline)) == [(0, 0), (1, 1), (-1, 0), (1, 0)]
poly = Polygon([(0, 0), (1, 1), (1, 0)])

poly = Polygon([(0, 0), (1, 1), (1, 0), (0, 0)])
assert list(geom_xys(poly)) == [(0, 0), (1, 1), (1, 0), (0, 0)]

ring = poly.exterior
assert list(geom_xys(ring)) == [(0, 0), (1, 1), (1, 0), (0, 0)]

mpoly = MultiPolygon([poly, Polygon([(2, 2), (3, 3), (3, 2)])])
assert list(geom_xys(mpoly)) == [(0, 0), (1, 1), (1, 0), (0, 0),
(2, 2), (3, 3), (3, 2), (2, 2)]

mpt3d = MultiPoint([(0, 0, 1), (1, 1, 2)])
assert list(geom_xys(mpt3d)) == [(0, 0), (1, 1)]


# TODO # gen_point_query(interpolation="fake")
# TODO # gen_point_query(interpolation="bilinear")
# TODO # gen_point_query(<features_without_props>)
4 changes: 4 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def test_get_percentile():
assert get_percentile('percentile_100') == 100.0
assert get_percentile('percentile_13.2') == 13.2


def test_get_bad_percentile():
with pytest.raises(ValueError):
get_percentile('foo')
Expand Down Expand Up @@ -63,3 +64,6 @@ def test_boxify_non_point():
line = LineString([(0, 0), (1, 1)])
with pytest.raises(ValueError):
boxify_points(line, None)

# TODO # def test_boxify_multi_point
# TODO # def test_boxify_point
4 changes: 4 additions & 0 deletions tests/test_zonal.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,3 +567,7 @@ def test_geodataframe_zonal():
expected = zonal_stats(polygons, raster)
assert zonal_stats(df, raster) == expected

# TODO # gen_zonal_stats(<features_without_props>)
# TODO # gen_zonal_stats(stats=nodata)
# TODO # gen_zonal_stats(<raster with non-integer dtype>)
# TODO # gen_zonal_stats(transform AND affine>)

0 comments on commit 75d6ef0

Please sign in to comment.