def makedata(ifile, zfield, *args): # if csv: if ifile[-4:] == '.csv': data = pd.read_csv(ifile, header=0) # Filter data data = data[(data[zfield] != -9999.0) & (data[lat] != -9999.0) & (pd.isnull(data[lat]) == False)] # Get points and Buffer to create polygon type coords = GeoSeries([Point(x, y) for x, y in zip(data[lon], data[lat])]) data['geometry'] = coords.buffer(0.0001) # Filter out null types data = data[pd.isnull(data.geometry) == False] #Recreate index data.index = [i for i in range(len(data))] return GeoDataFrame(data)# elif ifile[-4:] == '.shp': data = GeoDataFrame.from_file(ifile) # Polygon types if isinstance(data.geometry[1], Polygon) == True: data = data[pd.isnull(data.geometry) == False] return data # Point types elif isinstance(data.geometry[1], Point) == True: coords = data.geometry.buffer(0.0001) data['geometry'] = coords # Filter out null types data = data[pd.isnull(data.geometry) == False] # Recreate index data.index = [i for i in range(len(data))] return GeoDataFrame(data) else: return "Filetype not supported - Only points or polygons!"
def setUp(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.g3.crs = {"init": "epsg:4326", "no_defs": True} self.g4 = GeoSeries([self.t2, self.t1]) self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, self.t2, None]) self.a1 = self.g1.copy() self.a1.index = ["A", "B"] self.a2 = self.g2.copy() self.a2.index = ["B", "C"] self.esb = Point(-73.9847, 40.7484) self.sol = Point(-74.0446, 40.6893) self.landmarks = GeoSeries([self.esb, self.sol], crs={"init": "epsg:4326", "no_defs": True}) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) # Placeholder for testing, will just drop in different geometries # when needed self.gdf1 = GeoDataFrame({"geometry": self.g1, "col0": [1.0, 2.0], "col1": ["geo", "pandas"]}) self.gdf2 = GeoDataFrame({"geometry": self.g1, "col3": [4, 5], "col4": ["rand", "string"]})
class TestNonuniformGeometryPlotting(unittest.TestCase): def setUp(self): poly = Polygon([(1, 0), (2, 0), (2, 1)]) line = LineString([(0.5, 0.5), (1, 1), (1, 0.5), (1.5, 1)]) point = Point(0.75, 0.25) self.series = GeoSeries([poly, line, point]) self.df = GeoDataFrame({'geometry': self.series, 'values': [1, 2, 3]}) return def test_colormap(self): ax = self.series.plot(cmap='RdYlGn') cmap = get_cmap('RdYlGn', 3) # polygon gets extra alpha. See #266 _check_colors(1, ax.collections[0], [cmap(0)], alpha=0.5) # N.B. ax.collections[1] contains the edges of the polygon _check_colors(1, ax.collections[2], [cmap(1)], alpha=1) # line _check_colors(1, ax.collections[3], [cmap(2)], alpha=1) # point def test_style_kwargs(self): # markersize -> only the Point gets it ax = self.series.plot(markersize=10) assert ax.collections[3].get_sizes() == [10] ax = self.df.plot(markersize=10) assert ax.collections[3].get_sizes() == [10]
class TestLineStringPlotting(unittest.TestCase): def setUp(self): self.N = 10 values = np.arange(self.N) self.lines = GeoSeries([LineString([(0, i), (9, i)]) for i in xrange(self.N)]) self.df = GeoDataFrame({'geometry': self.lines, 'values': values}) def test_single_color(self): ax = self.lines.plot(color='green') _check_colors(ax.get_lines(), ['green']*self.N) ax = self.df.plot(color='green') _check_colors(ax.get_lines(), ['green']*self.N) ax = self.df.plot(column='values', color='green') _check_colors(ax.get_lines(), ['green']*self.N) def test_style_kwargs(self): # linestyle ax = self.lines.plot(linestyle='dashed') ls = [l.get_linestyle() for l in ax.get_lines()] assert ls == ['--'] * self.N ax = self.df.plot(linestyle='dashed') ls = [l.get_linestyle() for l in ax.get_lines()] assert ls == ['--'] * self.N ax = self.df.plot(column='values', linestyle='dashed') ls = [l.get_linestyle() for l in ax.get_lines()] assert ls == ['--'] * self.N
class TestSeries(unittest.TestCase): def setUp(self): self.p1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.p2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g1 = GeoSeries([self.p1, self.p2]) self.g2 = GeoSeries([self.p2, self.p1]) def test_area(self): assert np.allclose(self.g1.area.values, np.array([0.5, 1.0])) def test_bounds(self): assert np.allclose(self.g1.bounds.values, np.array([[0, 0, 1, 1], [0, 0, 1, 1]])) def test_contains(self): assert np.alltrue(self.g1.contains(self.p1)) assert not np.alltrue(self.g1.contains(Point([5, 5]))) def test_length(self): l = np.array([2 + np.sqrt(2), 4]) assert np.allclose(self.g1.length.values, l) def test_is_valid(self): assert np.alltrue(self.g1.is_valid) def test_is_empty(self): assert np.alltrue(np.logical_not(self.g1.is_empty)) def test_is_ring(self): assert np.alltrue(self.g1.is_ring) def test_is_simple(self): assert np.alltrue(self.g1.is_simple)
class TestNonuniformGeometryPlotting: def setup_method(self): pytest.importorskip('matplotlib', '1.5.0') poly = Polygon([(1, 0), (2, 0), (2, 1)]) line = LineString([(0.5, 0.5), (1, 1), (1, 0.5), (1.5, 1)]) point = Point(0.75, 0.25) self.series = GeoSeries([poly, line, point]) self.df = GeoDataFrame({'geometry': self.series, 'values': [1, 2, 3]}) def test_colors(self): # default uniform color ax = self.series.plot() _check_colors(1, ax.collections[0].get_facecolors(), [MPL_DFT_COLOR]) _check_colors(1, ax.collections[1].get_edgecolors(), [MPL_DFT_COLOR]) _check_colors(1, ax.collections[2].get_facecolors(), [MPL_DFT_COLOR]) # colormap: different colors ax = self.series.plot(cmap='RdYlGn') cmap = plt.get_cmap('RdYlGn') exp_colors = cmap(np.arange(3) / (3 - 1)) _check_colors(1, ax.collections[0].get_facecolors(), [exp_colors[0]]) _check_colors(1, ax.collections[1].get_edgecolors(), [exp_colors[1]]) _check_colors(1, ax.collections[2].get_facecolors(), [exp_colors[2]]) def test_style_kwargs(self): ax = self.series.plot(markersize=10) assert ax.collections[2].get_sizes() == [10] ax = self.df.plot(markersize=10) assert ax.collections[2].get_sizes() == [10]
def test_point_plot(self): """ Test plotting a simple series of points """ clf() filename = 'points_plot.png' N = 10 points = GeoSeries(Point(i, i) for i in xrange(N)) ax = points.plot() self._compare_images(ax=ax, filename=filename)
def test_line_plot(self): """ Test plotting a simple series of lines """ clf() filename = 'lines_plot.png' N = 10 lines = GeoSeries([LineString([(0, i), (9, i)]) for i in xrange(N)]) ax = lines.plot() self._compare_images(ax=ax, filename=filename)
def test_buffer_distance_array(self): original = GeoSeries([self.p0, self.p0]) expected = GeoSeries( [Polygon(((6, 5), (5, 4), (4, 5), (5, 6), (6, 5))), Polygon(((10, 5), (5, 0), (0, 5), (5, 10), (10, 5))), ]) calculated = original.buffer(np.array([1, 5]), resolution=1) assert_geoseries_equal(calculated, expected, check_less_precise=True)
def test_facecolor(self): t1 = Polygon([(0, 0), (1, 0), (1, 1)]) t2 = Polygon([(1, 0), (2, 0), (2, 1)]) polys = GeoSeries([t1, t2]) df = GeoDataFrame({'geometry': polys, 'values': [0, 1]}) ax = polys.plot(facecolor='k') _check_colors(ax.patches, ['k']*2, alpha=0.5)
def test_polygons_append(self): t1 = Polygon([(0, 0), (1, 0), (1, 1)]) t2 = Polygon([(0, 0), (1, 1), (0, 1)]) sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) s = GeoSeries([t1, t2, sq]) t = GeoSeries([t1, t2, sq], [3, 4, 5]) s = s.append(t) assert len(s) == 6 assert s.sindex.size == 6
def test_polygons_append(self): t1 = Polygon([(0, 0), (1, 0), (1, 1)]) t2 = Polygon([(0, 0), (1, 1), (0, 1)]) sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) s = GeoSeries([t1, t2, sq]) t = GeoSeries([t1, t2, sq], [3, 4, 5]) s = s.append(t) self.assertEqual(len(s), 6) self.assertEqual(s.sindex.size, 6)
def test_poly_plot(self): """ Test plotting a simple series of polygons """ clf() filename = 'poly_plot.png' t1 = Polygon([(0, 0), (1, 0), (1, 1)]) t2 = Polygon([(1, 0), (2, 0), (2, 1)]) polys = GeoSeries([t1, t2]) ax = polys.plot() self._compare_images(ax=ax, filename=filename)
def test_empty_plot(self): s = GeoSeries([]) with pytest.warns(UserWarning): ax = s.plot() assert len(ax.collections) == 0 df = GeoDataFrame([]) with pytest.warns(UserWarning): ax = df.plot() assert len(ax.collections) == 0
def test_isna(NA): s2 = GeoSeries([Point(0, 0), NA, Point(2, 2)]) exp = pd.Series([False, True, False]) res = s2.isnull() assert_series_equal(res, exp) res = s2.isna() assert_series_equal(res, exp) res = s2.notnull() assert_series_equal(res, ~exp) res = s2.notna() assert_series_equal(res, ~exp)
def test_explode_geoseries(self): s = GeoSeries([MultiPoint([(0, 0), (1, 1)]), MultiPoint([(2, 2), (3, 3), (4, 4)])]) s.index.name = 'test_index_name' expected_index_name = ['test_index_name', None] index = [(0, 0), (0, 1), (1, 0), (1, 1), (1, 2)] expected = GeoSeries([Point(0, 0), Point(1, 1), Point(2, 2), Point(3, 3), Point(4, 4)], index=MultiIndex.from_tuples( index, names=expected_index_name)) assert_geoseries_equal(expected, s.explode())
def test_explode(self): s = GeoSeries([MultiPoint([(0,0), (1,1)]), MultiPoint([(2,2), (3,3), (4,4)])]) index = [(0, 0), (0, 1), (1, 0), (1, 1), (1, 2)] expected = GeoSeries([Point(0,0), Point(1,1), Point(2,2), Point(3,3), Point(4,4)], index=MultiIndex.from_tuples(index)) assert_geoseries_equal(expected, s.explode()) df = self.gdf1[:2].set_geometry(s) assert_geoseries_equal(expected, df.explode())
def setUp(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.g4 = GeoSeries([self.t2, self.t1]) self.a1 = self.g1.copy() self.a1.index = ['A', 'B'] self.a2 = self.g2.copy() self.a2.index = ['B', 'C']
def __getitem__(self, key): """ The geometry column is not stored as a GeoSeries, so need to convert it back """ col = super(GeoDataFrame, self).__getitem__(key) if key == 'geometry': g = GeoSeries(col) # TODO: set crs in GeoSeries constructor rather than here g.crs = self.crs return g else: return col
def test_isna(NA): s2 = GeoSeries([Point(0, 0), NA, Point(2, 2)], index=[2, 4, 5], name='tt') exp = pd.Series([False, True, False], index=[2, 4, 5], name='tt') res = s2.isnull() assert type(res) == pd.Series assert_series_equal(res, exp) res = s2.isna() assert_series_equal(res, exp) res = s2.notnull() assert_series_equal(res, ~exp) res = s2.notna() assert_series_equal(res, ~exp)
class TestLineStringPlotting(unittest.TestCase): def setUp(self): self.N = 10 values = np.arange(self.N) self.lines = GeoSeries([LineString([(0, i), (4, i+0.5), (9, i)]) for i in xrange(self.N)], index=list('ABCDEFGHIJ')) self.df = GeoDataFrame({'geometry': self.lines, 'values': values}) def test_single_color(self): ax = self.lines.plot(color='green') _check_colors(self.N, ax.collections[0], ['green']*self.N) ax = self.df.plot(color='green') _check_colors(self.N, ax.collections[0], ['green']*self.N) with warnings.catch_warnings(record=True) as _: # don't print warning # 'color' overrides 'column' ax = self.df.plot(column='values', color='green') _check_colors(self.N, ax.collections[0], ['green']*self.N) def test_style_kwargs(self): def linestyle_tuple_to_string(tup): """ Converts a linestyle of the form `(offset, onoffseq)`, as documented in `Collections.set_linestyle`, to a string representation, namely one of: { 'dashed', 'dotted', 'dashdot', 'solid' }. """ from matplotlib.backend_bases import GraphicsContextBase reverse_idx = dict((v, k) for k, v in GraphicsContextBase.dashd.items()) return reverse_idx[tup] # linestyle ax = self.lines.plot(linestyle='dashed') ls = [linestyle_tuple_to_string(l) for l in ax.collections[0].get_linestyles()] assert ls == ['dashed'] ax = self.df.plot(linestyle='dashed') ls = [linestyle_tuple_to_string(l) for l in ax.collections[0].get_linestyles()] assert ls == ['dashed'] ax = self.df.plot(column='values', linestyle='dashed') ls = [linestyle_tuple_to_string(l) for l in ax.collections[0].get_linestyles()] assert ls == ['dashed']
def setup_method(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.t3 = Polygon([(2, 0), (3, 0), (3, 1)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.inner_sq = Polygon([(0.25, 0.25), (0.75, 0.25), (0.75, 0.75), (0.25, 0.75)]) self.nested_squares = Polygon(self.sq.boundary, [self.inner_sq.boundary]) self.p0 = Point(5, 5) self.p3d = Point(5, 5, 5) self.g0 = GeoSeries([self.t1, self.t2, self.sq, self.inner_sq, self.nested_squares, self.p0]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.g3.crs = {'init': 'epsg:4326', 'no_defs': True} self.g4 = GeoSeries([self.t2, self.t1]) self.g4.crs = {'init': 'epsg:4326', 'no_defs': True} self.g_3d = GeoSeries([self.p0, self.p3d]) self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, None]) self.a1 = self.g1.copy() self.a1.index = ['A', 'B'] self.a2 = self.g2.copy() self.a2.index = ['B', 'C'] self.esb = Point(-73.9847, 40.7484) self.sol = Point(-74.0446, 40.6893) self.landmarks = GeoSeries([self.esb, self.sol], crs={'init': 'epsg:4326', 'no_defs': True}) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) self.g6 = GeoSeries([self.p0, self.t3]) self.empty = GeoSeries([]) self.empty.crs = {'init': 'epsg:4326', 'no_defs': True} self.empty_poly = Polygon() # Crossed lines self.l3 = LineString([(0, 0), (1, 1)]) self.l4 = LineString([(0, 1), (1, 0)]) self.crossed_lines = GeoSeries([self.l3, self.l4]) # Placeholder for testing, will just drop in different geometries # when needed self.gdf1 = GeoDataFrame({'geometry': self.g1, 'col0': [1.0, 2.0], 'col1': ['geo', 'pandas']}) self.gdf2 = GeoDataFrame({'geometry': self.g1, 'col3': [4, 5], 'col4': ['rand', 'string']})
class Bench: # extensions for different file types to test params = [".shp", ".json", ".gpkg"] param_names = ["ext"] def setup(self, ext): self.driver_dict = {".shp": "ESRI Shapefile", ".json": "GeoJSON", ".gpkg": "GPKG"} driver = self.driver_dict[ext] num_points = 20000 xs = np.random.rand(num_points) ys = np.random.rand(num_points) self.points = GeoSeries([Point(x, y) for (x, y) in zip(xs, ys)]) self.df = GeoDataFrame({"geometry": self.points, "x": xs, "y": ys, "s": np.zeros(num_points, dtype="object")}) self.tmpdir = tempfile.mkdtemp() self.series_filename = os.path.join(self.tmpdir, "series" + ext) self.frame_filename = os.path.join(self.tmpdir, "frame" + ext) self.points.to_file(self.series_filename, driver=driver) self.df.to_file(self.frame_filename, driver=driver) def teardown(self, ext): shutil.rmtree(self.tmpdir) def time_write_frame(self, ext): driver = self.driver_dict[ext] with tempfile.TemporaryDirectory() as tmpdir: out_filename = os.path.join(tmpdir, "frame" + ext) self.df.to_file(out_filename, driver=driver) def time_write_series(self, ext): driver = self.driver_dict[ext] with tempfile.TemporaryDirectory() as tmpdir: out_filename = os.path.join(tmpdir, "series" + ext) self.points.to_file(out_filename, driver=driver) def time_read_frame(self, ext): frame = GeoDataFrame.from_file(self.frame_filename) def time_read_series(self, ext): points = GeoSeries.from_file(self.series_filename) def time_read_series_from_frame(self, ext): points = GeoSeries.from_file(self.frame_filename)
def setup(self, *args): self.points = GeoSeries([Point(i, i) for i in range(100000)]) triangles = GeoSeries([Polygon([(random.random(), random.random()) for _ in range(3)]) for _ in range(1000)]) triangles2 = triangles.copy().iloc[np.random.choice(1000, 1000)] triangles3 = GeoSeries([Polygon([(random.random(), random.random()) for _ in range(3)]) for _ in range(10000)]) triangle = Polygon([(random.random(), random.random()) for _ in range(3)]) self.triangles, self.triangles2 = triangles, triangles2 self.triangles_big = triangles3 self.triangle = triangle
def test_single_color(self): t1 = Polygon([(0, 0), (1, 0), (1, 1)]) t2 = Polygon([(1, 0), (2, 0), (2, 1)]) polys = GeoSeries([t1, t2]) df = GeoDataFrame({'geometry': polys, 'values': [0, 1]}) ax = polys.plot(color='green') _check_colors(ax.patches, ['green']*2, alpha=0.5) ax = df.plot(color='green') _check_colors(ax.patches, ['green']*2, alpha=0.5) ax = df.plot(column='values', color='green') _check_colors(ax.patches, ['green']*2, alpha=0.5)
def setUp(self): t1 = Polygon([(0, 0), (1, 0), (1, 1)]) t2 = Polygon([(1, 0), (2, 0), (2, 1)]) self.polys = GeoSeries([t1, t2]) self.df = GeoDataFrame({'geometry': self.polys, 'values': [0, 1]}) return
def test_to_file(self): """ Test to_file and from_file """ tempfilename = os.path.join(self.tempdir, 'test.shp') self.g3.to_file(tempfilename) # Read layer back in? s = GeoSeries.from_file(tempfilename) self.assertTrue(all(self.g3.equals(s)))
class TestPolygonPlotting(unittest.TestCase): def setUp(self): t1 = Polygon([(0, 0), (1, 0), (1, 1)]) t2 = Polygon([(1, 0), (2, 0), (2, 1)]) self.polys = GeoSeries([t1, t2]) self.df = GeoDataFrame({'geometry': self.polys, 'values': [0, 1]}) return def test_single_color(self): ax = self.polys.plot(color='green') _check_colors(ax.patches, ['green']*2, alpha=0.5) ax = self.df.plot(color='green') _check_colors(ax.patches, ['green']*2, alpha=0.5) ax = self.df.plot(column='values', color='green') _check_colors(ax.patches, ['green']*2, alpha=0.5) def test_vmin_vmax(self): # when vmin == vmax, all polygons should be the same color ax = self.df.plot(column='values', categorical=True, vmin=0, vmax=0) cmap = get_cmap('Set1', 2) self.assertEqual(ax.patches[0].get_facecolor(), ax.patches[1].get_facecolor())
def setup_method(self): self.N = 10 values = np.arange(self.N) self.lines = GeoSeries([LineString([(0, i), (4, i+0.5), (9, i)]) for i in range(self.N)], index=list('ABCDEFGHIJ')) self.df = GeoDataFrame({'geometry': self.lines, 'values': values})
def setup_method(self): pytest.importorskip('matplotlib', '1.5.0') poly = Polygon([(1, 0), (2, 0), (2, 1)]) line = LineString([(0.5, 0.5), (1, 1), (1, 0.5), (1.5, 1)]) point = Point(0.75, 0.25) self.series = GeoSeries([poly, line, point]) self.df = GeoDataFrame({'geometry': self.series, 'values': [1, 2, 3]})
def test_equal_comp_op(self): s = GeoSeries([Point(x, x) for x in range(3)]) res = s == Point(1, 1) exp = pd.Series([False, True, False]) assert_series_equal(res, exp)
def test_difference_series2(self): expected = GeoSeries([GeometryCollection(), self.t2]) with pytest.warns(DeprecationWarning): self._test_binary_operator("__sub__", expected, self.g1, self.g2) with pytest.warns(DeprecationWarning): self._test_binary_operator("__sub__", expected, self.gdf1, self.g2)
#!/bin/env python # Elena C Reinisch 20180912 # setup script to load data and put into friendly database # load libraries from __future__ import (absolute_import, division, print_function) import os import matplotlib as mpl import matplotlib.pyplot as plt from shapely.geometry import Point import pandas as pd import geopandas as gpd from geopandas import GeoSeries, GeoDataFrame # load data co2_data_table = pd.read_excel("../data/co2.xlsx", skiprows=range(1, 2)) # set up geometry geometry = [ Point(xy) for xy in zip(co2_data_table['Longitude'], co2_data_table['Latitude']) ] geometry = GeoSeries(geometry) geometry.crs = {'init': 'epsg:4326'} geometry.crs co2_geo_data_table = GeoDataFrame(co2_data_table, geometry=geometry)
def setUp(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.g3.crs = {'init': 'epsg:4326', 'no_defs': True} self.g4 = GeoSeries([self.t2, self.t1]) self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, self.t2, None]) self.a1 = self.g1.copy() self.a1.index = ['A', 'B'] self.a2 = self.g2.copy() self.a2.index = ['B', 'C'] self.esb = Point(-73.9847, 40.7484) self.sol = Point(-74.0446, 40.6893) self.landmarks = GeoSeries([self.esb, self.sol], crs={'init': 'epsg:4326', 'no_defs': True}) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) # Placeholder for testing, will just drop in different geometries # when needed self.gdf1 = GeoDataFrame({'geometry' : self.g1, 'col0' : [1.0, 2.0], 'col1' : ['geo', 'pandas']}) self.gdf2 = GeoDataFrame({'geometry' : self.g1, 'col3' : [4, 5], 'col4' : ['rand', 'string']})
def test_symmetric_difference_poly(self): expected = GeoSeries([GeometryCollection(), self.sq], crs=self.g3.crs) self._test_binary_topological('symmetric_difference', expected, self.g3, self.t1)
def add_nuts(s: gpd.GeoSeries) -> gpd.GeoSeries: rv = nuts_gdf[nuts_gdf.contains(s.geometry)][['LEVL_CODE', 'db_record']] rv.set_index('LEVL_CODE', drop=True, inplace=True) rv.rename(index={0: 'NUTS_0', 1: 'NUTS_1', 2: 'NUTS_2', 3: 'NUTS_3'}, inplace=True) return s.append(rv.T.iloc[0])
class TestGeomMethods(unittest.TestCase): def setUp(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.g3.crs = {'init': 'epsg:4326', 'no_defs': True} self.g4 = GeoSeries([self.t2, self.t1]) self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, self.t2, None]) self.a1 = self.g1.copy() self.a1.index = ['A', 'B'] self.a2 = self.g2.copy() self.a2.index = ['B', 'C'] self.esb = Point(-73.9847, 40.7484) self.sol = Point(-74.0446, 40.6893) self.landmarks = GeoSeries([self.esb, self.sol], crs={'init': 'epsg:4326', 'no_defs': True}) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) # Placeholder for testing, will just drop in different geometries # when needed self.gdf1 = GeoDataFrame({'geometry' : self.g1, 'col0' : [1.0, 2.0], 'col1' : ['geo', 'pandas']}) self.gdf2 = GeoDataFrame({'geometry' : self.g1, 'col3' : [4, 5], 'col4' : ['rand', 'string']}) def _test_unary_real(self, op, expected, a): """ Tests for 'area', 'length', 'is_valid', etc. """ fcmp = assert_series_equal self._test_unary(op, expected, a, fcmp) def _test_unary_topological(self, op, expected, a): if isinstance(expected, GeoPandasBase): fcmp = assert_geoseries_equal else: fcmp = lambda a, b: self.assert_(geom_equals(a, b)) self._test_unary(op, expected, a, fcmp) def _test_binary_topological(self, op, expected, a, b, *args, **kwargs): """ Tests for 'intersection', 'union', 'symmetric_difference', etc. """ if isinstance(expected, GeoPandasBase): fcmp = assert_geoseries_equal else: fcmp = lambda a, b: self.assert_(geom_equals(a, b)) if isinstance(b, GeoPandasBase): right_df = True else: right_df = False self._binary_op_test(op, expected, a, b, fcmp, True, right_df, *args, **kwargs) def _test_binary_real(self, op, expected, a, b, *args, **kwargs): fcmp = assert_series_equal self._binary_op_test(op, expected, a, b, fcmp, True, False, *args, **kwargs) def _test_binary_operator(self, op, expected, a, b): """ The operators only have GeoSeries on the left, but can have GeoSeries or GeoDataFrame on the right. """ if isinstance(expected, GeoPandasBase): fcmp = assert_geoseries_equal else: fcmp = lambda a, b: self.assert_(geom_equals(a, b)) if isinstance(b, GeoPandasBase): right_df = True else: right_df = False self._binary_op_test(op, expected, a, b, fcmp, False, right_df) def _binary_op_test(self, op, expected, left, right, fcmp, left_df, right_df, *args, **kwargs): """ This is a helper to call a function on GeoSeries and GeoDataFrame arguments. For example, 'intersection' is a member of both GeoSeries and GeoDataFrame and can take either GeoSeries or GeoDataFrame inputs. This function has the ability to test all four combinations of input types. Parameters ---------- expected : str The operation to be tested. e.g., 'intersection' left: GeoSeries right: GeoSeries fcmp: function Called with the result of the operation and expected. It should assert if the result is incorrect left_df: bool If the left input should also be called with a GeoDataFrame right_df: bool Indicates whether the right input should be called with a GeoDataFrame """ def _make_gdf(s): n = len(s) col1 = string.lowercase[:n] col2 = range(n) return GeoDataFrame({'geometry': s.values, 'col1' : col1, 'col2' : col2}, index=s.index, crs=s.crs) # Test GeoSeries.op(GeoSeries) result = getattr(left, op)(right, *args, **kwargs) fcmp(result, expected) if left_df: # Test GeoDataFrame.op(GeoSeries) gdf_left = _make_gdf(left) result = getattr(gdf_left, op)(right, *args, **kwargs) fcmp(result, expected) if right_df: # Test GeoSeries.op(GeoDataFrame) gdf_right = _make_gdf(right) result = getattr(left, op)(gdf_right, *args, **kwargs) fcmp(result, expected) if left_df: # Test GeoDataFrame.op(GeoDataFrame) result = getattr(gdf_left, op)(gdf_right, *args, **kwargs) fcmp(result, expected) def _test_unary(self, op, expected, a, fcmp): # GeoSeries, (GeoSeries or geometry) result = getattr(a, op) fcmp(result, expected) # GeoDataFrame, (GeoSeries or geometry) gdf = self.gdf1.set_geometry(a) result = getattr(gdf, op) fcmp(result, expected) def test_intersection(self): self._test_binary_topological('intersection', self.t1, self.g1, self.g2) def test_union_series(self): self._test_binary_topological('union', self.sq, self.g1, self.g2) def test_union_polygon(self): self._test_binary_topological('union', self.sq, self.g1, self.t2) def test_symmetric_difference_series(self): self._test_binary_topological('symmetric_difference', self.sq, self.g3, self.g4) def test_symmetric_difference_poly(self): expected = GeoSeries([GeometryCollection(), self.sq], crs=self.g3.crs) self._test_binary_topological('symmetric_difference', expected, self.g3, self.t1) def test_difference_series(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_topological('difference', expected, self.g1, self.g2) def test_difference_poly(self): expected = GeoSeries([self.t1, self.t1]) self._test_binary_topological('difference', expected, self.g1, self.t2) def test_boundary(self): l1 = LineString([(0, 0), (1, 0), (1, 1), (0, 0)]) l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) expected = GeoSeries([l1, l2], index=self.g1.index, crs=self.g1.crs) self._test_unary_topological('boundary', expected, self.g1) def test_area(self): expected = Series(np.array([0.5, 1.0]), index=self.g1.index) self._test_unary_real('area', expected, self.g1) def test_bounds(self): # Set columns to get the order right expected = DataFrame({'minx': [0.0, 0.0], 'miny': [0.0, 0.0], 'maxx': [1.0, 1.0], 'maxy': [1.0, 1.0]}, index=self.g1.index, columns=['minx', 'miny', 'maxx', 'maxy']) result = self.g1.bounds assert_frame_equal(expected, result) gdf = self.gdf1.set_geometry(self.g1) result = gdf.bounds assert_frame_equal(expected, result) def test_contains(self): expected = np.array([True] * len(self.g1)) assert_array_equal(expected, self.g1.contains(self.t1)) expected = np.array([False] * len(self.g1)) assert_array_equal(expected, self.g1.contains(Point(5,5))) def test_length(self): expected = Series(np.array([2 + np.sqrt(2), 4]), index=self.g1.index) self._test_unary_real('length', expected, self.g1) @unittest.skip('TODO') def test_crosses(self): # TODO pass @unittest.skip('TODO') def test_disjoint(self): # TODO pass @unittest.skip('TODO') def test_intersects(self): # TODO pass @unittest.skip('TODO') def test_overlaps(self): # TODO pass @unittest.skip('TODO') def test_touches(self): # TODO pass @unittest.skip('TODO') def test_within(self): # TODO pass def test_is_valid(self): expected = Series(np.array([True] * len(self.g1)), self.g1.index) self._test_unary_real('is_valid', expected, self.g1) def test_is_empty(self): expected = Series(np.array([False] * len(self.g1)), self.g1.index) self._test_unary_real('is_empty', expected, self.g1) def test_is_ring(self): expected = Series(np.array([True] * len(self.g1)), self.g1.index) self._test_unary_real('is_ring', expected, self.g1) def test_is_simple(self): expected = Series(np.array([True] * len(self.g1)), self.g1.index) self._test_unary_real('is_simple', expected, self.g1) @unittest.skip('TODO') def test_exterior(self): # TODO pass @unittest.skip('TODO') def test_interiors(self): # TODO pass def test_interpolate(self): expected = GeoSeries([Point(0.5, 1.0), Point(0.75, 1.0)]) self._test_binary_topological('interpolate', expected, self.g5, 0.75, normalized=True) expected = GeoSeries([Point(0.5, 1.0), Point(1.0, 0.5)]) self._test_binary_topological('interpolate', expected, self.g5, 1.5) def test_project(self): expected = Series([2.0, 1.5], index=self.g5.index) p = Point(1.0, 0.5) self._test_binary_real('project', expected, self.g5, p) expected = Series([1.0, 0.5], index=self.g5.index) self._test_binary_real('project', expected, self.g5, p, normalized=True) def test_translate_tuple(self): trans = self.sol.x - self.esb.x, self.sol.y - self.esb.y self.assert_(self.landmarks.translate(*trans)[0].equals(self.sol)) res = self.gdf1.set_geometry(self.landmarks).translate(*trans)[0] self.assert_(res.equals(self.sol)) def test_rotate(self): angle = 98 expected = self.g4 o = Point(0,0) res = self.g4.rotate(angle, origin=o).rotate(-angle, origin=o) self.assert_(geom_almost_equals(self.g4, res)) res = self.gdf1.set_geometry(self.g4).rotate(angle, origin=Point(0,0)) self.assert_(geom_almost_equals(expected, res.rotate(-angle, origin=o))) def test_scale(self): expected = self.g4 scale = 2., 1. inv = tuple(1./i for i in scale) o = Point(0,0) res = self.g4.scale(*scale, origin=o).scale(*inv, origin=o) self.assertTrue(geom_almost_equals(expected, res)) res = self.gdf1.set_geometry(self.g4).scale(*scale, origin=o) res = res.scale(*inv, origin=o) self.assert_(geom_almost_equals(expected, res)) def test_skew(self): expected = self.g4 skew = 45. o = Point(0,0) # Test xs res = self.g4.skew(xs=skew, origin=o).skew(xs=-skew, origin=o) self.assert_(geom_almost_equals(expected, res)) res = self.gdf1.set_geometry(self.g4).skew(xs=skew, origin=o) res = res.skew(xs=-skew, origin=o) self.assert_(geom_almost_equals(expected, res)) # Test ys res = self.g4.skew(ys=skew, origin=o).skew(ys=-skew, origin=o) self.assert_(geom_almost_equals(expected, res)) res = self.gdf1.set_geometry(self.g4).skew(ys=skew, origin=o) res = res.skew(ys=-skew, origin=o) self.assert_(geom_almost_equals(expected, res)) def test_envelope(self): e = self.g3.envelope self.assertTrue(np.alltrue(e.geom_equals(self.sq))) self.assertIsInstance(e, GeoSeries) self.assertEqual(self.g3.crs, e.crs) def test_total_bounds(self): bbox = self.sol.x, self.sol.y, self.esb.x, self.esb.y self.assert_(self.landmarks.total_bounds, bbox) df = GeoDataFrame({'geometry': self.landmarks, 'col1': range(len(self.landmarks))}) self.assert_(df.total_bounds, bbox) # # Test '&', '|', '^', and '-' # The left can only be a GeoSeries. The right hand side can be a # GeoSeries, GeoDataFrame or Shapely geometry # def test_intersection_operator(self): self._test_binary_operator('__and__', self.t1, self.g1, self.g2) def test_union_operator(self): self._test_binary_operator('__or__', self.sq, self.g1, self.g2) def test_union_operator_polygon(self): self._test_binary_operator('__or__', self.sq, self.g1, self.t2) def test_symmetric_difference_operator(self): self._test_binary_operator('__xor__', self.sq, self.g3, self.g4) def test_difference_series(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_operator('__sub__', expected, self.g1, self.g2) def test_difference_poly(self): expected = GeoSeries([self.t1, self.t1]) self._test_binary_operator('__sub__', expected, self.g1, self.t2)
def test_centroid(self): polygon = Polygon([(-1, -1), (1, -1), (1, 1), (-1, 1)]) point = Point(0, 0) polygons = GeoSeries([polygon for i in range(3)]) points = GeoSeries([point for i in range(3)]) assert_geoseries_equal(polygons.centroid, points)
def setUp(self): self.tempdir = tempfile.mkdtemp() self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.g3.crs = {'init': 'epsg:4326', 'no_defs': True} self.g4 = GeoSeries([self.t2, self.t1]) self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, self.t2, None]) self.a1 = self.g1.copy() self.a1.index = ['A', 'B'] self.a2 = self.g2.copy() self.a2.index = ['B', 'C'] self.esb = Point(-73.9847, 40.7484) self.sol = Point(-74.0446, 40.6893) self.landmarks = GeoSeries([self.esb, self.sol], crs={ 'init': 'epsg:4326', 'no_defs': True }) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2])
class TestSeries(unittest.TestCase): def setUp(self): self.tempdir = tempfile.mkdtemp() self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.g3.crs = {'init': 'epsg:4326', 'no_defs': True} self.g4 = GeoSeries([self.t2, self.t1]) self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, self.t2, None]) self.a1 = self.g1.copy() self.a1.index = ['A', 'B'] self.a2 = self.g2.copy() self.a2.index = ['B', 'C'] self.esb = Point(-73.9847, 40.7484) self.sol = Point(-74.0446, 40.6893) self.landmarks = GeoSeries([self.esb, self.sol], crs={ 'init': 'epsg:4326', 'no_defs': True }) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) def tearDown(self): shutil.rmtree(self.tempdir) def test_single_geom_constructor(self): p = Point(1, 2) line = LineString([(2, 3), (4, 5), (5, 6)]) poly = Polygon([(0, 0), (1, 0), (1, 1)], [[(.1, .1), (.9, .1), (.9, .9)]]) mp = MultiPoint([(1, 2), (3, 4), (5, 6)]) mline = MultiLineString([[(1, 2), (3, 4), (5, 6)], [(7, 8), (9, 10)]]) poly2 = Polygon([(1, 1), (1, -1), (-1, -1), (-1, 1)], [[(.5, .5), (.5, -.5), (-.5, -.5), (-.5, .5)]]) mpoly = MultiPolygon([poly, poly2]) geoms = [p, line, poly, mp, mline, mpoly] index = ['a', 'b', 'c', 'd'] for g in geoms: gs = GeoSeries(g) self.assert_(len(gs) == 1) self.assert_(gs.iloc[0] is g) gs = GeoSeries(g, index=index) self.assert_(len(gs) == len(index)) for x in gs: self.assert_(x is g) def test_copy(self): gc = self.g3.copy() self.assertTrue(type(gc) is GeoSeries) self.assertEqual(self.g3.name, gc.name) self.assertEqual(self.g3.crs, gc.crs) def test_in(self): self.assertTrue(self.t1 in self.g1) self.assertTrue(self.sq in self.g1) self.assertTrue(self.t1 in self.a1) self.assertTrue(self.t2 in self.g3) self.assertTrue(self.sq not in self.g3) self.assertTrue(5 not in self.g3) def test_geom_equals(self): self.assertTrue(np.alltrue(self.g1.geom_equals(self.g1))) assert_array_equal(self.g1.geom_equals(self.sq), [False, True]) def test_geom_equals_align(self): a = self.a1.geom_equals(self.a2) self.assertFalse(a['A']) self.assertTrue(a['B']) self.assertFalse(a['C']) def test_align(self): a1, a2 = self.a1.align(self.a2) self.assertTrue(a2['A'].is_empty) self.assertTrue(a1['B'].equals(a2['B'])) self.assertTrue(a1['C'].is_empty) def test_geom_almost_equals(self): # TODO: test decimal parameter self.assertTrue(np.alltrue(self.g1.geom_almost_equals(self.g1))) assert_array_equal(self.g1.geom_almost_equals(self.sq), [False, True]) def test_geom_equals_exact(self): # TODO: test tolerance parameter self.assertTrue(np.alltrue(self.g1.geom_equals_exact(self.g1, 0.001))) assert_array_equal(self.g1.geom_equals_exact(self.sq, 0.001), [False, True]) def test_equal_comp_op(self): s = GeoSeries([Point(x, x) for x in range(3)]) res = s == Point(1, 1) exp = pd.Series([False, True, False]) assert_series_equal(res, exp) def test_to_file(self): """ Test to_file and from_file """ tempfilename = os.path.join(self.tempdir, 'test.shp') self.g3.to_file(tempfilename) # Read layer back in? s = GeoSeries.from_file(tempfilename) self.assertTrue(all(self.g3.geom_equals(s))) # TODO: compare crs def test_to_json(self): """Test whether GeoSeries.to_json works and returns an actual json file.""" json_str = self.g3.to_json() json_dict = json.loads(json_str) # TODO : verify the output is a valid GeoJSON. def test_representative_point(self): self.assertTrue( np.alltrue(self.g1.contains(self.g1.representative_point()))) self.assertTrue( np.alltrue(self.g2.contains(self.g2.representative_point()))) self.assertTrue( np.alltrue(self.g3.contains(self.g3.representative_point()))) self.assertTrue( np.alltrue(self.g4.contains(self.g4.representative_point()))) def test_transform(self): utm18n = self.landmarks.to_crs(epsg=26918) lonlat = utm18n.to_crs(epsg=4326) self.assertTrue(np.alltrue(self.landmarks.geom_almost_equals(lonlat))) with self.assertRaises(ValueError): self.g1.to_crs(epsg=4326) with self.assertRaises(TypeError): self.landmarks.to_crs(crs=None, epsg=None) def test_fillna(self): na = self.na_none.fillna(Point()) self.assertTrue(isinstance(na[2], BaseGeometry)) self.assertTrue(na[2].is_empty) self.assertTrue(geom_equals(self.na_none[:2], na[:2])) # XXX: method works inconsistently for different pandas versions #self.na_none.fillna(method='backfill') def test_coord_slice(self): """ Test CoordinateSlicer """ # need some better test cases self.assertTrue(geom_equals(self.g3, self.g3.cx[:, :])) self.assertTrue( geom_equals(self.g3[[True, False]], self.g3.cx[0.9:, :0.1])) self.assertTrue( geom_equals(self.g3[[False, True]], self.g3.cx[0:0.1, 0.9:1.0])) def test_coord_slice_with_zero(self): # Test that CoordinateSlice correctly handles zero slice (#GH477). gs = GeoSeries([Point(x, x) for x in range(-3, 4)]) self.assertTrue(geom_equals(gs.cx[:0, :0], gs.loc[:3])) self.assertTrue(geom_equals(gs.cx[:, :0], gs.loc[:3])) self.assertTrue(geom_equals(gs.cx[:0, :], gs.loc[:3])) self.assertTrue(geom_equals(gs.cx[0:, 0:], gs.loc[3:])) self.assertTrue(geom_equals(gs.cx[0:, :], gs.loc[3:])) self.assertTrue(geom_equals(gs.cx[:, 0:], gs.loc[3:])) def test_geoseries_geointerface(self): self.assertEqual(self.g1.__geo_interface__['type'], 'FeatureCollection') self.assertEqual(len(self.g1.__geo_interface__['features']), self.g1.shape[0]) def test_proj4strings(self): # As string reprojected = self.g3.to_crs('+proj=utm +zone=30N') reprojected_back = reprojected.to_crs(epsg=4326) self.assertTrue( np.alltrue(self.g3.geom_almost_equals(reprojected_back))) # As dict reprojected = self.g3.to_crs({'proj': 'utm', 'zone': '30N'}) reprojected_back = reprojected.to_crs(epsg=4326) self.assertTrue( np.alltrue(self.g3.geom_almost_equals(reprojected_back))) # Set to equivalent string, convert, compare to original copy = self.g3.copy() copy.crs = '+init=epsg:4326' reprojected = copy.to_crs({'proj': 'utm', 'zone': '30N'}) reprojected_back = reprojected.to_crs(epsg=4326) self.assertTrue( np.alltrue(self.g3.geom_almost_equals(reprojected_back))) # Conversions by different format reprojected_string = self.g3.to_crs('+proj=utm +zone=30N') reprojected_dict = self.g3.to_crs({'proj': 'utm', 'zone': '30N'}) self.assertTrue( np.alltrue( reprojected_string.geom_almost_equals(reprojected_dict)))
class TestGeomMethods: def setup_method(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.t3 = Polygon([(2, 0), (3, 0), (3, 1)]) self.tz = Polygon([(1, 1, 1), (2, 2, 2), (3, 3, 3)]) self.tz1 = Polygon([(2, 2, 2), (1, 1, 1), (3, 3, 3)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.sqz = Polygon([(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4)]) self.t4 = Polygon([(0, 0), (3, 0), (3, 3), (0, 2)]) self.t5 = Polygon([(2, 0), (3, 0), (3, 3), (2, 3)]) self.inner_sq = Polygon([(0.25, 0.25), (0.75, 0.25), (0.75, 0.75), (0.25, 0.75)]) self.nested_squares = Polygon(self.sq.boundary, [self.inner_sq.boundary]) self.p0 = Point(5, 5) self.p3d = Point(5, 5, 5) self.g0 = GeoSeries([ self.t1, self.t2, self.sq, self.inner_sq, self.nested_squares, self.p0, None, ]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.gz = GeoSeries([self.tz, self.sqz, self.tz1]) self.g3.crs = "epsg:4326" self.g4 = GeoSeries([self.t2, self.t1]) self.g4.crs = "epsg:4326" self.g_3d = GeoSeries([self.p0, self.p3d]) self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, None]) self.a1 = self.g1.copy() self.a1.index = ["A", "B"] self.a2 = self.g2.copy() self.a2.index = ["B", "C"] self.esb = Point(-73.9847, 40.7484, 30.3244) self.sol = Point(-74.0446, 40.6893, 31.2344) self.landmarks = GeoSeries([self.esb, self.sol], crs="epsg:4326") self.pt2d = Point(-73.9847, 40.7484) self.landmarks_mixed = GeoSeries([self.esb, self.sol, self.pt2d], crs=4326) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) self.g6 = GeoSeries([self.p0, self.t3]) self.g7 = GeoSeries([self.sq, self.t4]) self.g8 = GeoSeries([self.t1, self.t5]) self.empty = GeoSeries([]) self.all_none = GeoSeries([None, None]) self.empty_poly = Polygon() self.g9 = GeoSeries(self.g0, index=range(1, 8)) # Crossed lines self.l3 = LineString([(0, 0), (1, 1)]) self.l4 = LineString([(0, 1), (1, 0)]) self.crossed_lines = GeoSeries([self.l3, self.l4]) # Placeholder for testing, will just drop in different geometries # when needed self.gdf1 = GeoDataFrame({ "geometry": self.g1, "col0": [1.0, 2.0], "col1": ["geo", "pandas"] }) self.gdf2 = GeoDataFrame({ "geometry": self.g1, "col3": [4, 5], "col4": ["rand", "string"] }) self.gdf3 = GeoDataFrame({ "geometry": self.g3, "col3": [4, 5], "col4": ["rand", "string"] }) self.gdfz = GeoDataFrame({ "geometry": self.gz, "col3": [4, 5, 6], "col4": ["rand", "string", "geo"] }) def _test_unary_real(self, op, expected, a): """Tests for 'area', 'length', 'is_valid', etc.""" fcmp = assert_series_equal self._test_unary(op, expected, a, fcmp) def _test_unary_topological(self, op, expected, a): if isinstance(expected, GeoPandasBase): fcmp = assert_geoseries_equal else: def fcmp(a, b): assert a.equals(b) self._test_unary(op, expected, a, fcmp) def _test_binary_topological(self, op, expected, a, b, *args, **kwargs): """Tests for 'intersection', 'union', 'symmetric_difference', etc.""" if isinstance(expected, GeoPandasBase): fcmp = assert_geoseries_equal else: def fcmp(a, b): assert geom_equals(a, b) if isinstance(b, GeoPandasBase): right_df = True else: right_df = False self._binary_op_test(op, expected, a, b, fcmp, True, right_df, *args, **kwargs) def _test_binary_real(self, op, expected, a, b, *args, **kwargs): fcmp = assert_series_equal self._binary_op_test(op, expected, a, b, fcmp, True, False, *args, **kwargs) def _test_binary_operator(self, op, expected, a, b): """ The operators only have GeoSeries on the left, but can have GeoSeries or GeoDataFrame on the right. If GeoDataFrame is on the left, geometry column is used. """ if isinstance(expected, GeoPandasBase): fcmp = assert_geoseries_equal else: def fcmp(a, b): assert geom_equals(a, b) if isinstance(b, GeoPandasBase): right_df = True else: right_df = False self._binary_op_test(op, expected, a, b, fcmp, False, right_df) def _binary_op_test(self, op, expected, left, right, fcmp, left_df, right_df, *args, **kwargs): """ This is a helper to call a function on GeoSeries and GeoDataFrame arguments. For example, 'intersection' is a member of both GeoSeries and GeoDataFrame and can take either GeoSeries or GeoDataFrame inputs. This function has the ability to test all four combinations of input types. Parameters ---------- expected : str The operation to be tested. e.g., 'intersection' left: GeoSeries right: GeoSeries fcmp: function Called with the result of the operation and expected. It should assert if the result is incorrect left_df: bool If the left input should also be called with a GeoDataFrame right_df: bool Indicates whether the right input should be called with a GeoDataFrame """ def _make_gdf(s): n = len(s) col1 = string.ascii_lowercase[:n] col2 = range(n) return GeoDataFrame( { "geometry": s.values, "col1": col1, "col2": col2 }, index=s.index, crs=s.crs, ) # Test GeoSeries.op(GeoSeries) result = getattr(left, op)(right, *args, **kwargs) fcmp(result, expected) if left_df: # Test GeoDataFrame.op(GeoSeries) gdf_left = _make_gdf(left) result = getattr(gdf_left, op)(right, *args, **kwargs) fcmp(result, expected) if right_df: # Test GeoSeries.op(GeoDataFrame) gdf_right = _make_gdf(right) result = getattr(left, op)(gdf_right, *args, **kwargs) fcmp(result, expected) if left_df: # Test GeoDataFrame.op(GeoDataFrame) result = getattr(gdf_left, op)(gdf_right, *args, **kwargs) fcmp(result, expected) def _test_unary(self, op, expected, a, fcmp): # GeoSeries, (GeoSeries or geometry) result = getattr(a, op) fcmp(result, expected) # GeoDataFrame, (GeoSeries or geometry) gdf = self.gdf1.set_geometry(a) result = getattr(gdf, op) fcmp(result, expected) # TODO re-enable for all operations once we use pyproj > 2 # def test_crs_warning(self): # # operations on geometries should warn for different CRS # no_crs_g3 = self.g3.copy() # no_crs_g3.crs = None # with pytest.warns(UserWarning): # self._test_binary_topological('intersection', self.g3, # self.g3, no_crs_g3) def test_intersection(self): self._test_binary_topological("intersection", self.t1, self.g1, self.g2) with pytest.warns(UserWarning, match="The indices .+ different"): self._test_binary_topological("intersection", self.all_none, self.g1, self.empty) with pytest.warns(UserWarning, match="The indices .+ different"): assert len(self.g0.intersection(self.g9, align=True) == 8) assert len(self.g0.intersection(self.g9, align=False) == 7) def test_union_series(self): self._test_binary_topological("union", self.sq, self.g1, self.g2) with pytest.warns(UserWarning, match="The indices .+ different"): assert len(self.g0.union(self.g9, align=True) == 8) assert len(self.g0.union(self.g9, align=False) == 7) def test_union_polygon(self): self._test_binary_topological("union", self.sq, self.g1, self.t2) def test_symmetric_difference_series(self): self._test_binary_topological("symmetric_difference", self.sq, self.g3, self.g4) with pytest.warns(UserWarning, match="The indices .+ different"): assert len(self.g0.symmetric_difference(self.g9, align=True) == 8) assert len(self.g0.symmetric_difference(self.g9, align=False) == 7) def test_symmetric_difference_poly(self): expected = GeoSeries([GeometryCollection(), self.sq], crs=self.g3.crs) self._test_binary_topological("symmetric_difference", expected, self.g3, self.t1) def test_difference_series(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_topological("difference", expected, self.g1, self.g2) with pytest.warns(UserWarning, match="The indices .+ different"): assert len(self.g0.difference(self.g9, align=True) == 8) assert len(self.g0.difference(self.g9, align=False) == 7) def test_difference_poly(self): expected = GeoSeries([self.t1, self.t1]) self._test_binary_topological("difference", expected, self.g1, self.t2) def test_geo_op_empty_result(self): l1 = LineString([(0, 0), (1, 1)]) l2 = LineString([(2, 2), (3, 3)]) expected = GeoSeries([GeometryCollection()]) # binary geo resulting in empty geometry result = GeoSeries([l1]).intersection(l2) assert_geoseries_equal(result, expected) # binary geo empty result with right GeoSeries result = GeoSeries([l1]).intersection(GeoSeries([l2])) assert_geoseries_equal(result, expected) # unary geo resulting in empty geometry result = GeoSeries([GeometryCollection()]).convex_hull assert_geoseries_equal(result, expected) def test_boundary(self): l1 = LineString([(0, 0), (1, 0), (1, 1), (0, 0)]) l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) expected = GeoSeries([l1, l2], index=self.g1.index, crs=self.g1.crs) self._test_unary_topological("boundary", expected, self.g1) def test_area(self): expected = Series(np.array([0.5, 1.0]), index=self.g1.index) self._test_unary_real("area", expected, self.g1) expected = Series(np.array([0.5, np.nan]), index=self.na_none.index) self._test_unary_real("area", expected, self.na_none) def test_area_crs_warn(self): with pytest.warns(UserWarning, match="Geometry is in a geographic CRS"): self.g4.area def test_bounds(self): # Set columns to get the order right expected = DataFrame( { "minx": [0.0, 0.0], "miny": [0.0, 0.0], "maxx": [1.0, 1.0], "maxy": [1.0, 1.0], }, index=self.g1.index, columns=["minx", "miny", "maxx", "maxy"], ) result = self.g1.bounds assert_frame_equal(expected, result) gdf = self.gdf1.set_geometry(self.g1) result = gdf.bounds assert_frame_equal(expected, result) def test_bounds_empty(self): # test bounds of empty GeoSeries # https://github.com/geopandas/geopandas/issues/1195 s = GeoSeries([]) result = s.bounds expected = DataFrame(columns=["minx", "miny", "maxx", "maxy"], index=s.index, dtype="float64") assert_frame_equal(result, expected) def test_unary_union(self): p1 = self.t1 p2 = Polygon([(2, 0), (3, 0), (3, 1)]) expected = unary_union([p1, p2]) g = GeoSeries([p1, p2]) self._test_unary_topological("unary_union", expected, g) g2 = GeoSeries([p1, None]) self._test_unary_topological("unary_union", p1, g2) g3 = GeoSeries([None, None]) assert g3.unary_union is None def test_cascaded_union_deprecated(self): p1 = self.t1 p2 = Polygon([(2, 0), (3, 0), (3, 1)]) g = GeoSeries([p1, p2]) with pytest.warns( FutureWarning, match="The 'cascaded_union' attribute is deprecated"): result = g.cascaded_union assert result == g.unary_union def test_contains(self): expected = [True, False, True, False, False, False, False] assert_array_dtype_equal(expected, self.g0.contains(self.t1)) expected = [False, True, True, True, True, True, False, False] with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.contains(self.g9, align=True)) expected = [False, False, True, False, False, False, False] assert_array_dtype_equal(expected, self.g0.contains(self.g9, align=False)) def test_length(self): expected = Series(np.array([2 + np.sqrt(2), 4]), index=self.g1.index) self._test_unary_real("length", expected, self.g1) expected = Series(np.array([2 + np.sqrt(2), np.nan]), index=self.na_none.index) self._test_unary_real("length", expected, self.na_none) def test_length_crs_warn(self): with pytest.warns(UserWarning, match="Geometry is in a geographic CRS"): self.g4.length def test_crosses(self): expected = [False, False, False, False, False, False, False] assert_array_dtype_equal(expected, self.g0.crosses(self.t1)) expected = [False, True] assert_array_dtype_equal(expected, self.crossed_lines.crosses(self.l3)) expected = [False] * 8 with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.crosses(self.g9, align=True)) expected = [False] * 7 assert_array_dtype_equal(expected, self.g0.crosses(self.g9, align=False)) def test_disjoint(self): expected = [False, False, False, False, False, True, False] assert_array_dtype_equal(expected, self.g0.disjoint(self.t1)) expected = [False] * 8 with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.disjoint(self.g9, align=True)) expected = [False, False, False, False, True, False, False] assert_array_dtype_equal(expected, self.g0.disjoint(self.g9, align=False)) def test_relate(self): expected = Series( [ "212101212", "212101212", "212FF1FF2", "2FFF1FFF2", "FF2F112F2", "FF0FFF212", None, ], index=self.g0.index, ) assert_array_dtype_equal(expected, self.g0.relate(self.inner_sq)) expected = Series(["FF0FFF212", None], index=self.g6.index) assert_array_dtype_equal(expected, self.g6.relate(self.na_none)) expected = Series( [ None, "2FFF1FFF2", "2FFF1FFF2", "2FFF1FFF2", "2FFF1FFF2", "0FFFFFFF2", None, None, ], index=range(8), ) with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.relate(self.g9, align=True)) expected = Series( [ "FF2F11212", "2FF11F212", "212FF1FF2", "FF2F1F212", "FF2FF10F2", None, None, ], index=self.g0.index, ) assert_array_dtype_equal(expected, self.g0.relate(self.g9, align=False)) def test_distance(self): expected = Series(np.array([np.sqrt((5 - 1)**2 + (5 - 1)**2), np.nan]), self.na_none.index) assert_array_dtype_equal(expected, self.na_none.distance(self.p0)) expected = Series(np.array([np.sqrt(4**2 + 4**2), np.nan]), self.g6.index) assert_array_dtype_equal(expected, self.g6.distance(self.na_none)) expected = Series(np.array([np.nan, 0, 0, 0, 0, 0, np.nan, np.nan]), range(8)) with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.distance(self.g9, align=True)) val = self.g0.iloc[4].distance(self.g9.iloc[4]) expected = Series(np.array([0, 0, 0, 0, val, np.nan, np.nan]), self.g0.index) assert_array_dtype_equal(expected, self.g0.distance(self.g9, align=False)) def test_distance_crs_warning(self): with pytest.warns(UserWarning, match="Geometry is in a geographic CRS"): self.g4.distance(self.p0) def test_intersects(self): expected = [True, True, True, True, True, False, False] assert_array_dtype_equal(expected, self.g0.intersects(self.t1)) expected = [True, False] assert_array_dtype_equal(expected, self.na_none.intersects(self.t2)) expected = np.array([], dtype=bool) assert_array_dtype_equal(expected, self.empty.intersects(self.t1)) expected = np.array([], dtype=bool) assert_array_dtype_equal(expected, self.empty.intersects(self.empty_poly)) expected = [False] * 7 assert_array_dtype_equal(expected, self.g0.intersects(self.empty_poly)) expected = [False, True, True, True, True, True, False, False] with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.intersects(self.g9, align=True)) expected = [True, True, True, True, False, False, False] assert_array_dtype_equal(expected, self.g0.intersects(self.g9, align=False)) def test_overlaps(self): expected = [True, True, False, False, False, False, False] assert_array_dtype_equal(expected, self.g0.overlaps(self.inner_sq)) expected = [False, False] assert_array_dtype_equal(expected, self.g4.overlaps(self.t1)) expected = [False] * 8 with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.overlaps(self.g9, align=True)) expected = [False] * 7 assert_array_dtype_equal(expected, self.g0.overlaps(self.g9, align=False)) def test_touches(self): expected = [False, True, False, False, False, False, False] assert_array_dtype_equal(expected, self.g0.touches(self.t1)) expected = [False] * 8 with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.touches(self.g9, align=True)) expected = [True, False, False, True, False, False, False] assert_array_dtype_equal(expected, self.g0.touches(self.g9, align=False)) def test_within(self): expected = [True, False, False, False, False, False, False] assert_array_dtype_equal(expected, self.g0.within(self.t1)) expected = [True, True, True, True, True, False, False] assert_array_dtype_equal(expected, self.g0.within(self.sq)) expected = [False, True, True, True, True, True, False, False] with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.within(self.g9, align=True)) expected = [False, True, False, False, False, False, False] assert_array_dtype_equal(expected, self.g0.within(self.g9, align=False)) def test_covers_itself(self): # Each polygon in a Series covers itself res = self.g1.covers(self.g1) exp = Series([True, True]) assert_series_equal(res, exp) def test_covers(self): res = self.g7.covers(self.g8) exp = Series([True, False]) assert_series_equal(res, exp) expected = [False, True, True, True, True, True, False, False] with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.covers(self.g9, align=True)) expected = [False, False, True, False, False, False, False] assert_array_dtype_equal(expected, self.g0.covers(self.g9, align=False)) def test_covers_inverse(self): res = self.g8.covers(self.g7) exp = Series([False, False]) assert_series_equal(res, exp) @pytest.mark.skipif( not compat.USE_PYGEOS, reason="covered_by is only implemented for pygeos, not shapely", ) def test_covered_by(self): res = self.g1.covered_by(self.g1) exp = Series([True, True]) assert_series_equal(res, exp) expected = [False, True, True, True, True, True, False, False] with pytest.warns(UserWarning, match="The indices .+ different"): assert_array_dtype_equal(expected, self.g0.covered_by(self.g9, align=True)) expected = [False, True, False, False, False, False, False] assert_array_dtype_equal(expected, self.g0.covered_by(self.g9, align=False)) def test_is_valid(self): expected = Series(np.array([True] * len(self.g1)), self.g1.index) self._test_unary_real("is_valid", expected, self.g1) def test_is_empty(self): expected = Series(np.array([False] * len(self.g1)), self.g1.index) self._test_unary_real("is_empty", expected, self.g1) # for is_ring we raise a warning about the value for Polygon changing @pytest.mark.filterwarnings("ignore:is_ring:FutureWarning") def test_is_ring(self): expected = Series(np.array([True] * len(self.g1)), self.g1.index) self._test_unary_real("is_ring", expected, self.g1) def test_is_simple(self): expected = Series(np.array([True] * len(self.g1)), self.g1.index) self._test_unary_real("is_simple", expected, self.g1) def test_has_z(self): expected = Series([False, True], self.g_3d.index) self._test_unary_real("has_z", expected, self.g_3d) def test_xyz_points(self): expected_x = [-73.9847, -74.0446] expected_y = [40.7484, 40.6893] expected_z = [30.3244, 31.2344] assert_array_dtype_equal(expected_x, self.landmarks.geometry.x) assert_array_dtype_equal(expected_y, self.landmarks.geometry.y) assert_array_dtype_equal(expected_z, self.landmarks.geometry.z) # mixed dimensions expected_z = [30.3244, 31.2344, np.nan] assert_array_dtype_equal(expected_z, self.landmarks_mixed.geometry.z) def test_xyz_polygons(self): # accessing x attribute in polygon geoseries should raise an error with pytest.raises(ValueError): _ = self.gdf1.geometry.x # and same for accessing y attribute in polygon geoseries with pytest.raises(ValueError): _ = self.gdf1.geometry.y # and same for accessing z attribute in polygon geoseries with pytest.raises(ValueError): _ = self.gdfz.geometry.z def test_centroid(self): polygon = Polygon([(-1, -1), (1, -1), (1, 1), (-1, 1)]) point = Point(0, 0) polygons = GeoSeries([polygon for i in range(3)]) points = GeoSeries([point for i in range(3)]) assert_geoseries_equal(polygons.centroid, points) def test_centroid_crs_warn(self): with pytest.warns(UserWarning, match="Geometry is in a geographic CRS"): self.g4.centroid def test_convex_hull(self): # the convex hull of a square should be the same as the square squares = GeoSeries([self.sq for i in range(3)]) assert_geoseries_equal(squares, squares.convex_hull) def test_exterior(self): exp_exterior = GeoSeries([LinearRing(p.boundary) for p in self.g3]) for expected, computed in zip(exp_exterior, self.g3.exterior): assert computed.equals(expected) def test_interiors(self): original = GeoSeries([self.t1, self.nested_squares]) # This is a polygon with no interior. expected = [] assert original.interiors[0] == expected # This is a polygon with an interior. expected = LinearRing(self.inner_sq.boundary) assert original.interiors[1][0].equals(expected) def test_interpolate(self): expected = GeoSeries([Point(0.5, 1.0), Point(0.75, 1.0)]) self._test_binary_topological("interpolate", expected, self.g5, 0.75, normalized=True) expected = GeoSeries([Point(0.5, 1.0), Point(1.0, 0.5)]) self._test_binary_topological("interpolate", expected, self.g5, 1.5) def test_interpolate_distance_array(self): expected = GeoSeries([Point(0.0, 0.75), Point(1.0, 0.5)]) self._test_binary_topological("interpolate", expected, self.g5, np.array([0.75, 1.5])) expected = GeoSeries([Point(0.5, 1.0), Point(0.0, 1.0)]) self._test_binary_topological("interpolate", expected, self.g5, np.array([0.75, 1.5]), normalized=True) def test_interpolate_distance_wrong_length(self): distances = np.array([1, 2, 3]) with pytest.raises(ValueError): self.g5.interpolate(distances) def test_interpolate_distance_wrong_index(self): distances = Series([1, 2], index=[99, 98]) with pytest.raises(ValueError): self.g5.interpolate(distances) def test_interpolate_crs_warning(self): g5_crs = self.g5.copy() g5_crs.crs = 4326 with pytest.warns(UserWarning, match="Geometry is in a geographic CRS"): g5_crs.interpolate(1) def test_project(self): expected = Series([2.0, 1.5], index=self.g5.index) p = Point(1.0, 0.5) self._test_binary_real("project", expected, self.g5, p) expected = Series([1.0, 0.5], index=self.g5.index) self._test_binary_real("project", expected, self.g5, p, normalized=True) s = GeoSeries([Point(2, 2), Point(0.5, 0.5)], index=[1, 2]) expected = Series([np.nan, 2.0, np.nan]) with pytest.warns(UserWarning, match="The indices .+ different"): assert_series_equal(self.g5.project(s), expected) expected = Series([2.0, 0.5], index=self.g5.index) assert_series_equal(self.g5.project(s, align=False), expected) def test_affine_transform(self): # 45 degree reflection matrix matrix = [0, 1, 1, 0, 0, 0] expected = self.g4 res = self.g3.affine_transform(matrix) assert_geoseries_equal(expected, res) def test_translate_tuple(self): trans = self.sol.x - self.esb.x, self.sol.y - self.esb.y assert self.landmarks.translate(*trans)[0].equals(self.sol) res = self.gdf1.set_geometry(self.landmarks).translate(*trans)[0] assert res.equals(self.sol) def test_rotate(self): angle = 98 expected = self.g4 o = Point(0, 0) res = self.g4.rotate(angle, origin=o).rotate(-angle, origin=o) assert geom_almost_equals(self.g4, res) res = self.gdf1.set_geometry(self.g4).rotate(angle, origin=Point(0, 0)) assert geom_almost_equals(expected, res.rotate(-angle, origin=o)) def test_scale(self): expected = self.g4 scale = 2.0, 1.0 inv = tuple(1.0 / i for i in scale) o = Point(0, 0) res = self.g4.scale(*scale, origin=o).scale(*inv, origin=o) assert geom_almost_equals(expected, res) res = self.gdf1.set_geometry(self.g4).scale(*scale, origin=o) res = res.scale(*inv, origin=o) assert geom_almost_equals(expected, res) def test_skew(self): expected = self.g4 skew = 45.0 o = Point(0, 0) # Test xs res = self.g4.skew(xs=skew, origin=o).skew(xs=-skew, origin=o) assert geom_almost_equals(expected, res) res = self.gdf1.set_geometry(self.g4).skew(xs=skew, origin=o) res = res.skew(xs=-skew, origin=o) assert geom_almost_equals(expected, res) # Test ys res = self.g4.skew(ys=skew, origin=o).skew(ys=-skew, origin=o) assert geom_almost_equals(expected, res) res = self.gdf1.set_geometry(self.g4).skew(ys=skew, origin=o) res = res.skew(ys=-skew, origin=o) assert geom_almost_equals(expected, res) def test_buffer(self): original = GeoSeries([Point(0, 0)]) expected = GeoSeries( [Polygon(((5, 0), (0, -5), (-5, 0), (0, 5), (5, 0)))]) calculated = original.buffer(5, resolution=1) assert geom_almost_equals(expected, calculated) def test_buffer_args(self): args = dict(cap_style=3, join_style=2, mitre_limit=2.5) calculated_series = self.g0.buffer(10, **args) for original, calculated in zip(self.g0, calculated_series): if original is None: assert calculated is None else: expected = original.buffer(10, **args) assert calculated.equals(expected) def test_buffer_distance_array(self): original = GeoSeries([self.p0, self.p0]) expected = GeoSeries([ Polygon(((6, 5), (5, 4), (4, 5), (5, 6), (6, 5))), Polygon(((10, 5), (5, 0), (0, 5), (5, 10), (10, 5))), ]) calculated = original.buffer(np.array([1, 5]), resolution=1) assert_geoseries_equal(calculated, expected, check_less_precise=True) def test_buffer_distance_wrong_length(self): original = GeoSeries([self.p0, self.p0]) distances = np.array([1, 2, 3]) with pytest.raises(ValueError): original.buffer(distances) def test_buffer_distance_wrong_index(self): original = GeoSeries([self.p0, self.p0], index=[0, 1]) distances = Series(data=[1, 2], index=[99, 98]) with pytest.raises(ValueError): original.buffer(distances) def test_buffer_empty_none(self): p = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)]) s = GeoSeries([p, GeometryCollection(), None]) result = s.buffer(0) assert_geoseries_equal(result, s) result = s.buffer(np.array([0, 0, 0])) assert_geoseries_equal(result, s) def test_buffer_crs_warn(self): with pytest.warns(UserWarning, match="Geometry is in a geographic CRS"): self.g4.buffer(1) with pytest.warns(None) as record: # do not warn for 0 self.g4.buffer(0) for r in record: assert "Geometry is in a geographic CRS." not in str(r.message) def test_envelope(self): e = self.g3.envelope assert np.all(e.geom_equals(self.sq)) assert isinstance(e, GeoSeries) assert self.g3.crs == e.crs def test_total_bounds(self): bbox = self.sol.x, self.sol.y, self.esb.x, self.esb.y assert isinstance(self.landmarks.total_bounds, np.ndarray) assert tuple(self.landmarks.total_bounds) == bbox df = GeoDataFrame({ "geometry": self.landmarks, "col1": range(len(self.landmarks)) }) assert tuple(df.total_bounds) == bbox def test_explode_geoseries(self): s = GeoSeries( [ MultiPoint([(0, 0), (1, 1)]), MultiPoint([(2, 2), (3, 3), (4, 4)]) ], crs=4326, ) s.index.name = "test_index_name" expected_index_name = ["test_index_name", None] index = [(0, 0), (0, 1), (1, 0), (1, 1), (1, 2)] expected = GeoSeries( [Point(0, 0), Point(1, 1), Point(2, 2), Point(3, 3), Point(4, 4)], index=MultiIndex.from_tuples(index, names=expected_index_name), crs=4326, ) with pytest.warns(FutureWarning, match="Currently, index_parts defaults"): assert_geoseries_equal(expected, s.explode()) @pytest.mark.parametrize("index_name", [None, "test"]) def test_explode_geodataframe(self, index_name): s = GeoSeries([MultiPoint([Point(1, 2), Point(2, 3)]), Point(5, 5)]) df = GeoDataFrame({"col": [1, 2], "geometry": s}) df.index.name = index_name with pytest.warns(FutureWarning, match="Currently, index_parts defaults"): test_df = df.explode() expected_s = GeoSeries([Point(1, 2), Point(2, 3), Point(5, 5)]) expected_df = GeoDataFrame({"col": [1, 1, 2], "geometry": expected_s}) expected_index = MultiIndex( [[0, 1], [0, 1]], # levels [[0, 0, 1], [0, 1, 0]], # labels/codes names=[index_name, None], ) expected_df = expected_df.set_index(expected_index) assert_frame_equal(test_df, expected_df) @pytest.mark.parametrize("index_name", [None, "test"]) def test_explode_geodataframe_level_1(self, index_name): # GH1393 s = GeoSeries([MultiPoint([Point(1, 2), Point(2, 3)]), Point(5, 5)]) df = GeoDataFrame({"level_1": [1, 2], "geometry": s}) df.index.name = index_name test_df = df.explode(index_parts=True) expected_s = GeoSeries([Point(1, 2), Point(2, 3), Point(5, 5)]) expected_df = GeoDataFrame({ "level_1": [1, 1, 2], "geometry": expected_s }) expected_index = MultiIndex( [[0, 1], [0, 1]], # levels [[0, 0, 1], [0, 1, 0]], # labels/codes names=[index_name, None], ) expected_df = expected_df.set_index(expected_index) assert_frame_equal(test_df, expected_df) @pytest.mark.parametrize("index_name", [None, "test"]) def test_explode_geodataframe_no_multiindex(self, index_name): # GH1393 s = GeoSeries([MultiPoint([Point(1, 2), Point(2, 3)]), Point(5, 5)]) df = GeoDataFrame({"level_1": [1, 2], "geometry": s}) df.index.name = index_name test_df = df.explode(index_parts=False) expected_s = GeoSeries([Point(1, 2), Point(2, 3), Point(5, 5)]) expected_df = GeoDataFrame({ "level_1": [1, 1, 2], "geometry": expected_s }) expected_index = Index([0, 0, 1], name=index_name) expected_df = expected_df.set_index(expected_index) assert_frame_equal(test_df, expected_df) def test_explode_pandas_fallback(self): d = { "col1": [["name1", "name2"], ["name3", "name4"]], "geometry": [MultiPoint([(1, 2), (3, 4)]), MultiPoint([(2, 1), (0, 0)])], } gdf = GeoDataFrame(d, crs=4326) expected_df = GeoDataFrame( { "col1": ["name1", "name2", "name3", "name4"], "geometry": [ MultiPoint([(1, 2), (3, 4)]), MultiPoint([(1, 2), (3, 4)]), MultiPoint([(2, 1), (0, 0)]), MultiPoint([(2, 1), (0, 0)]), ], }, index=[0, 0, 1, 1], crs=4326, ) # Test with column provided as arg exploded_df = gdf.explode("col1") assert_geodataframe_equal(exploded_df, expected_df) # Test with column provided as kwarg exploded_df = gdf.explode(column="col1") assert_geodataframe_equal(exploded_df, expected_df) @pytest.mark.skipif( not compat.PANDAS_GE_11, reason="ignore_index keyword introduced in pandas 1.1.0", ) def test_explode_pandas_fallback_ignore_index(self): d = { "col1": [["name1", "name2"], ["name3", "name4"]], "geometry": [MultiPoint([(1, 2), (3, 4)]), MultiPoint([(2, 1), (0, 0)])], } gdf = GeoDataFrame(d, crs=4326) expected_df = GeoDataFrame( { "col1": ["name1", "name2", "name3", "name4"], "geometry": [ MultiPoint([(1, 2), (3, 4)]), MultiPoint([(1, 2), (3, 4)]), MultiPoint([(2, 1), (0, 0)]), MultiPoint([(2, 1), (0, 0)]), ], }, crs=4326, ) # Test with column provided as arg exploded_df = gdf.explode("col1", ignore_index=True) assert_geodataframe_equal(exploded_df, expected_df) # Test with column provided as kwarg exploded_df = gdf.explode(column="col1", ignore_index=True) assert_geodataframe_equal(exploded_df, expected_df) @pytest.mark.parametrize("outer_index", [1, (1, 2), "1"]) def test_explode_pandas_multi_index(self, outer_index): index = MultiIndex.from_arrays( [[outer_index, outer_index, outer_index], [1, 2, 3]], names=("first", "second"), ) df = GeoDataFrame( {"vals": [1, 2, 3]}, geometry=[MultiPoint([(x, x), (x, 0)]) for x in range(3)], index=index, ) test_df = df.explode(index_parts=True) expected_s = GeoSeries([ Point(0, 0), Point(0, 0), Point(1, 1), Point(1, 0), Point(2, 2), Point(2, 0), ]) expected_df = GeoDataFrame({ "vals": [1, 1, 2, 2, 3, 3], "geometry": expected_s }) expected_index = MultiIndex.from_tuples( [(outer_index, *pair) for pair in [(1, 0), (1, 1), (2, 0), (2, 1), (3, 0), (3, 1)]], names=["first", "second", None], ) expected_df = expected_df.set_index(expected_index) assert_frame_equal(test_df, expected_df) @pytest.mark.parametrize("outer_index", [1, (1, 2), "1"]) def test_explode_pandas_multi_index_false(self, outer_index): index = MultiIndex.from_arrays( [[outer_index, outer_index, outer_index], [1, 2, 3]], names=("first", "second"), ) df = GeoDataFrame( {"vals": [1, 2, 3]}, geometry=[MultiPoint([(x, x), (x, 0)]) for x in range(3)], index=index, ) test_df = df.explode(index_parts=False) expected_s = GeoSeries([ Point(0, 0), Point(0, 0), Point(1, 1), Point(1, 0), Point(2, 2), Point(2, 0), ]) expected_df = GeoDataFrame({ "vals": [1, 1, 2, 2, 3, 3], "geometry": expected_s }) expected_index = MultiIndex.from_tuples( [ (outer_index, 1), (outer_index, 1), (outer_index, 2), (outer_index, 2), (outer_index, 3), (outer_index, 3), ], names=["first", "second"], ) expected_df = expected_df.set_index(expected_index) assert_frame_equal(test_df, expected_df) @pytest.mark.parametrize("outer_index", [1, (1, 2), "1"]) def test_explode_pandas_multi_index_ignore_index(self, outer_index): index = MultiIndex.from_arrays( [[outer_index, outer_index, outer_index], [1, 2, 3]], names=("first", "second"), ) df = GeoDataFrame( {"vals": [1, 2, 3]}, geometry=[MultiPoint([(x, x), (x, 0)]) for x in range(3)], index=index, ) test_df = df.explode(ignore_index=True) expected_s = GeoSeries([ Point(0, 0), Point(0, 0), Point(1, 1), Point(1, 0), Point(2, 2), Point(2, 0), ]) expected_df = GeoDataFrame({ "vals": [1, 1, 2, 2, 3, 3], "geometry": expected_s }) expected_index = Index(range(len(expected_df))) expected_df = expected_df.set_index(expected_index) assert_frame_equal(test_df, expected_df) # index_parts is ignored if ignore_index=True test_df = df.explode(ignore_index=True, index_parts=True) assert_frame_equal(test_df, expected_df) # # Test '&', '|', '^', and '-' # def test_intersection_operator(self): with pytest.warns(DeprecationWarning): self._test_binary_operator("__and__", self.t1, self.g1, self.g2) with pytest.warns(DeprecationWarning): self._test_binary_operator("__and__", self.t1, self.gdf1, self.g2) def test_union_operator(self): with pytest.warns(DeprecationWarning): self._test_binary_operator("__or__", self.sq, self.g1, self.g2) with pytest.warns(DeprecationWarning): self._test_binary_operator("__or__", self.sq, self.gdf1, self.g2) def test_union_operator_polygon(self): with pytest.warns(DeprecationWarning): self._test_binary_operator("__or__", self.sq, self.g1, self.t2) with pytest.warns(DeprecationWarning): self._test_binary_operator("__or__", self.sq, self.gdf1, self.t2) def test_symmetric_difference_operator(self): with pytest.warns(DeprecationWarning): self._test_binary_operator("__xor__", self.sq, self.g3, self.g4) with pytest.warns(DeprecationWarning): self._test_binary_operator("__xor__", self.sq, self.gdf3, self.g4) def test_difference_series2(self): expected = GeoSeries([GeometryCollection(), self.t2]) with pytest.warns(DeprecationWarning): self._test_binary_operator("__sub__", expected, self.g1, self.g2) with pytest.warns(DeprecationWarning): self._test_binary_operator("__sub__", expected, self.gdf1, self.g2) def test_difference_poly2(self): expected = GeoSeries([self.t1, self.t1]) with pytest.warns(DeprecationWarning): self._test_binary_operator("__sub__", expected, self.g1, self.t2) with pytest.warns(DeprecationWarning): self._test_binary_operator("__sub__", expected, self.gdf1, self.t2)
def test_difference_series(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_topological('difference', expected, self.g1, self.g2)
def test_difference_poly2(self): expected = GeoSeries([self.t1, self.t1]) with pytest.warns(DeprecationWarning): self._test_binary_operator("__sub__", expected, self.g1, self.t2) with pytest.warns(DeprecationWarning): self._test_binary_operator("__sub__", expected, self.gdf1, self.t2)
def test_buffer_distance_wrong_length(self): original = GeoSeries([self.p0, self.p0]) distances = np.array([1, 2, 3]) with pytest.raises(ValueError): original.buffer(distances)
def multi_poly_gdf(donut_geometry): """Create a multi-polygon GeoDataFrame.""" multi_poly = donut_geometry.unary_union out_df = GeoDataFrame(geometry=GeoSeries(multi_poly), crs="EPSG:4326") out_df["attr"] = ["pool"] return out_df
def test_boundary(self): l1 = LineString([(0, 0), (1, 0), (1, 1), (0, 0)]) l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) expected = GeoSeries([l1, l2], index=self.g1.index, crs=self.g1.crs) self._test_unary_topological("boundary", expected, self.g1)
def test_difference_poly2(self): expected = GeoSeries([self.t1, self.t1]) self._test_binary_operator('__sub__', expected, self.g1, self.t2)
def test_buffer(self): original = GeoSeries([Point(0, 0)]) expected = GeoSeries( [Polygon(((5, 0), (0, -5), (-5, 0), (0, 5), (5, 0)))]) calculated = original.buffer(5, resolution=1) assert geom_almost_equals(expected, calculated)
def test_difference_series2(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_operator('__sub__', expected, self.g1, self.g2)
class TestGeomMethods: def setup_method(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.t3 = Polygon([(2, 0), (3, 0), (3, 1)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.inner_sq = Polygon([(0.25, 0.25), (0.75, 0.25), (0.75, 0.75), (0.25, 0.75)]) self.nested_squares = Polygon(self.sq.boundary, [self.inner_sq.boundary]) self.p0 = Point(5, 5) self.g0 = GeoSeries([ self.t1, self.t2, self.sq, self.inner_sq, self.nested_squares, self.p0 ]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.g3.crs = {'init': 'epsg:4326', 'no_defs': True} self.g4 = GeoSeries([self.t2, self.t1]) self.g4.crs = {'init': 'epsg:4326', 'no_defs': True} self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, None]) self.a1 = self.g1.copy() self.a1.index = ['A', 'B'] self.a2 = self.g2.copy() self.a2.index = ['B', 'C'] self.esb = Point(-73.9847, 40.7484) self.sol = Point(-74.0446, 40.6893) self.landmarks = GeoSeries([self.esb, self.sol], crs={ 'init': 'epsg:4326', 'no_defs': True }) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) self.g6 = GeoSeries([self.p0, self.t3]) # Crossed lines self.l3 = LineString([(0, 0), (1, 1)]) self.l4 = LineString([(0, 1), (1, 0)]) self.crossed_lines = GeoSeries([self.l3, self.l4]) # Placeholder for testing, will just drop in different geometries # when needed self.gdf1 = GeoDataFrame({ 'geometry': self.g1, 'col0': [1.0, 2.0], 'col1': ['geo', 'pandas'] }) self.gdf2 = GeoDataFrame({ 'geometry': self.g1, 'col3': [4, 5], 'col4': ['rand', 'string'] }) def _test_unary_real(self, op, expected, a): """ Tests for 'area', 'length', 'is_valid', etc. """ fcmp = assert_series_equal self._test_unary(op, expected, a, fcmp) def _test_unary_topological(self, op, expected, a): if isinstance(expected, GeoPandasBase): fcmp = assert_geoseries_equal else: def fcmp(a, b): assert a.equals(b) self._test_unary(op, expected, a, fcmp) def _test_binary_topological(self, op, expected, a, b, *args, **kwargs): """ Tests for 'intersection', 'union', 'symmetric_difference', etc. """ if isinstance(expected, GeoPandasBase): fcmp = assert_geoseries_equal else: def fcmp(a, b): assert geom_equals(a, b) if isinstance(b, GeoPandasBase): right_df = True else: right_df = False self._binary_op_test(op, expected, a, b, fcmp, True, right_df, *args, **kwargs) def _test_binary_real(self, op, expected, a, b, *args, **kwargs): fcmp = assert_series_equal self._binary_op_test(op, expected, a, b, fcmp, True, False, *args, **kwargs) def _test_binary_operator(self, op, expected, a, b): """ The operators only have GeoSeries on the left, but can have GeoSeries or GeoDataFrame on the right. """ if isinstance(expected, GeoPandasBase): fcmp = assert_geoseries_equal else: def fcmp(a, b): assert geom_equals(a, b) if isinstance(b, GeoPandasBase): right_df = True else: right_df = False self._binary_op_test(op, expected, a, b, fcmp, False, right_df) def _binary_op_test(self, op, expected, left, right, fcmp, left_df, right_df, *args, **kwargs): """ This is a helper to call a function on GeoSeries and GeoDataFrame arguments. For example, 'intersection' is a member of both GeoSeries and GeoDataFrame and can take either GeoSeries or GeoDataFrame inputs. This function has the ability to test all four combinations of input types. Parameters ---------- expected : str The operation to be tested. e.g., 'intersection' left: GeoSeries right: GeoSeries fcmp: function Called with the result of the operation and expected. It should assert if the result is incorrect left_df: bool If the left input should also be called with a GeoDataFrame right_df: bool Indicates whether the right input should be called with a GeoDataFrame """ def _make_gdf(s): n = len(s) col1 = string.ascii_lowercase[:n] col2 = range(n) return GeoDataFrame( { 'geometry': s.values, 'col1': col1, 'col2': col2 }, index=s.index, crs=s.crs) # Test GeoSeries.op(GeoSeries) result = getattr(left, op)(right, *args, **kwargs) fcmp(result, expected) if left_df: # Test GeoDataFrame.op(GeoSeries) gdf_left = _make_gdf(left) result = getattr(gdf_left, op)(right, *args, **kwargs) fcmp(result, expected) if right_df: # Test GeoSeries.op(GeoDataFrame) gdf_right = _make_gdf(right) result = getattr(left, op)(gdf_right, *args, **kwargs) fcmp(result, expected) if left_df: # Test GeoDataFrame.op(GeoDataFrame) result = getattr(gdf_left, op)(gdf_right, *args, **kwargs) fcmp(result, expected) def _test_unary(self, op, expected, a, fcmp): # GeoSeries, (GeoSeries or geometry) result = getattr(a, op) fcmp(result, expected) # GeoDataFrame, (GeoSeries or geometry) gdf = self.gdf1.set_geometry(a) result = getattr(gdf, op) fcmp(result, expected) def test_crs_warning(self): # operations on geometries should warn for different CRS no_crs_g3 = self.g3.copy() no_crs_g3.crs = None with pytest.warns(UserWarning): self._test_binary_topological('intersection', self.g3, self.g3, no_crs_g3) def test_intersection(self): self._test_binary_topological('intersection', self.t1, self.g1, self.g2) def test_union_series(self): self._test_binary_topological('union', self.sq, self.g1, self.g2) def test_union_polygon(self): self._test_binary_topological('union', self.sq, self.g1, self.t2) def test_symmetric_difference_series(self): self._test_binary_topological('symmetric_difference', self.sq, self.g3, self.g4) def test_symmetric_difference_poly(self): expected = GeoSeries([GeometryCollection(), self.sq], crs=self.g3.crs) self._test_binary_topological('symmetric_difference', expected, self.g3, self.t1) def test_difference_series(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_topological('difference', expected, self.g1, self.g2) def test_difference_poly(self): expected = GeoSeries([self.t1, self.t1]) self._test_binary_topological('difference', expected, self.g1, self.t2) def test_boundary(self): l1 = LineString([(0, 0), (1, 0), (1, 1), (0, 0)]) l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]) expected = GeoSeries([l1, l2], index=self.g1.index, crs=self.g1.crs) self._test_unary_topological('boundary', expected, self.g1) def test_area(self): expected = Series(np.array([0.5, 1.0]), index=self.g1.index) self._test_unary_real('area', expected, self.g1) expected = Series(np.array([0.5, np.nan]), index=self.na_none.index) self._test_unary_real('area', expected, self.na_none) def test_bounds(self): # Set columns to get the order right expected = DataFrame( { 'minx': [0.0, 0.0], 'miny': [0.0, 0.0], 'maxx': [1.0, 1.0], 'maxy': [1.0, 1.0] }, index=self.g1.index, columns=['minx', 'miny', 'maxx', 'maxy']) result = self.g1.bounds assert_frame_equal(expected, result) gdf = self.gdf1.set_geometry(self.g1) result = gdf.bounds assert_frame_equal(expected, result) def test_unary_union(self): p1 = self.t1 p2 = Polygon([(2, 0), (3, 0), (3, 1)]) expected = unary_union([p1, p2]) g = GeoSeries([p1, p2]) self._test_unary_topological('unary_union', expected, g) def test_contains(self): expected = [True, False, True, False, False, False] assert_array_equal(expected, self.g0.contains(self.t1)) def test_length(self): expected = Series(np.array([2 + np.sqrt(2), 4]), index=self.g1.index) self._test_unary_real('length', expected, self.g1) expected = Series(np.array([2 + np.sqrt(2), np.nan]), index=self.na_none.index) self._test_unary_real('length', expected, self.na_none) def test_crosses(self): expected = [False, False, False, False, False, False] assert_array_equal(expected, self.g0.crosses(self.t1)) expected = [False, True] assert_array_equal(expected, self.crossed_lines.crosses(self.l3)) def test_disjoint(self): expected = [False, False, False, False, False, True] assert_array_equal(expected, self.g0.disjoint(self.t1)) def test_distance(self): expected = Series(np.array([np.sqrt((5 - 1)**2 + (5 - 1)**2), np.nan]), self.na_none.index) assert_array_equal(expected, self.na_none.distance(self.p0)) expected = Series(np.array([np.sqrt(4**2 + 4**2), np.nan]), self.g6.index) assert_array_equal(expected, self.g6.distance(self.na_none)) def test_intersects(self): expected = [True, True, True, True, True, False] assert_array_equal(expected, self.g0.intersects(self.t1)) expected = [True, False] assert_array_equal(expected, self.na_none.intersects(self.t2)) def test_overlaps(self): expected = [True, True, False, False, False, False] assert_array_equal(expected, self.g0.overlaps(self.inner_sq)) expected = [False, False] assert_array_equal(expected, self.g4.overlaps(self.t1)) def test_touches(self): expected = [False, True, False, False, False, False] assert_array_equal(expected, self.g0.touches(self.t1)) def test_within(self): expected = [True, False, False, False, False, False] assert_array_equal(expected, self.g0.within(self.t1)) expected = [True, True, True, True, True, False] assert_array_equal(expected, self.g0.within(self.sq)) def test_is_valid(self): expected = Series(np.array([True] * len(self.g1)), self.g1.index) self._test_unary_real('is_valid', expected, self.g1) def test_is_empty(self): expected = Series(np.array([False] * len(self.g1)), self.g1.index) self._test_unary_real('is_empty', expected, self.g1) def test_is_ring(self): expected = Series(np.array([True] * len(self.g1)), self.g1.index) self._test_unary_real('is_ring', expected, self.g1) def test_is_simple(self): expected = Series(np.array([True] * len(self.g1)), self.g1.index) self._test_unary_real('is_simple', expected, self.g1) def test_xy_points(self): expected_x = [-73.9847, -74.0446] expected_y = [40.7484, 40.6893] assert_array_equal(expected_x, self.landmarks.geometry.x) assert_array_equal(expected_y, self.landmarks.geometry.y) def test_xy_polygons(self): # accessing x attribute in polygon geoseries should raise an error with pytest.raises(ValueError): _ = self.gdf1.geometry.x # and same for accessing y attribute in polygon geoseries with pytest.raises(ValueError): _ = self.gdf1.geometry.y def test_centroid(self): polygon = Polygon([(-1, -1), (1, -1), (1, 1), (-1, 1)]) point = Point(0, 0) polygons = GeoSeries([polygon for i in range(3)]) points = GeoSeries([point for i in range(3)]) assert_geoseries_equal(polygons.centroid, points) def test_convex_hull(self): # the convex hull of a square should be the same as the square squares = GeoSeries([self.sq for i in range(3)]) assert_geoseries_equal(squares, squares.convex_hull) def test_exterior(self): exp_exterior = GeoSeries([LinearRing(p.boundary) for p in self.g3]) for expected, computed in zip(exp_exterior, self.g3.exterior): assert computed.equals(expected) def test_interiors(self): square_series = GeoSeries(self.nested_squares) exp_interiors = GeoSeries([LinearRing(self.inner_sq.boundary)]) for expected, computed in zip(exp_interiors, square_series.interiors): assert computed[0].equals(expected) def test_interpolate(self): expected = GeoSeries([Point(0.5, 1.0), Point(0.75, 1.0)]) self._test_binary_topological('interpolate', expected, self.g5, 0.75, normalized=True) expected = GeoSeries([Point(0.5, 1.0), Point(1.0, 0.5)]) self._test_binary_topological('interpolate', expected, self.g5, 1.5) def test_project(self): expected = Series([2.0, 1.5], index=self.g5.index) p = Point(1.0, 0.5) self._test_binary_real('project', expected, self.g5, p) expected = Series([1.0, 0.5], index=self.g5.index) self._test_binary_real('project', expected, self.g5, p, normalized=True) def test_translate_tuple(self): trans = self.sol.x - self.esb.x, self.sol.y - self.esb.y assert self.landmarks.translate(*trans)[0].equals(self.sol) res = self.gdf1.set_geometry(self.landmarks).translate(*trans)[0] assert res.equals(self.sol) def test_rotate(self): angle = 98 expected = self.g4 o = Point(0, 0) res = self.g4.rotate(angle, origin=o).rotate(-angle, origin=o) assert geom_almost_equals(self.g4, res) res = self.gdf1.set_geometry(self.g4).rotate(angle, origin=Point(0, 0)) assert geom_almost_equals(expected, res.rotate(-angle, origin=o)) def test_scale(self): expected = self.g4 scale = 2., 1. inv = tuple(1. / i for i in scale) o = Point(0, 0) res = self.g4.scale(*scale, origin=o).scale(*inv, origin=o) assert geom_almost_equals(expected, res) res = self.gdf1.set_geometry(self.g4).scale(*scale, origin=o) res = res.scale(*inv, origin=o) assert geom_almost_equals(expected, res) def test_skew(self): expected = self.g4 skew = 45. o = Point(0, 0) # Test xs res = self.g4.skew(xs=skew, origin=o).skew(xs=-skew, origin=o) assert geom_almost_equals(expected, res) res = self.gdf1.set_geometry(self.g4).skew(xs=skew, origin=o) res = res.skew(xs=-skew, origin=o) assert geom_almost_equals(expected, res) # Test ys res = self.g4.skew(ys=skew, origin=o).skew(ys=-skew, origin=o) assert geom_almost_equals(expected, res) res = self.gdf1.set_geometry(self.g4).skew(ys=skew, origin=o) res = res.skew(ys=-skew, origin=o) assert geom_almost_equals(expected, res) def test_buffer(self): original = GeoSeries([Point(0, 0)]) expected = GeoSeries( [Polygon(((5, 0), (0, -5), (-5, 0), (0, 5), (5, 0)))]) calculated = original.buffer(5, resolution=1) assert geom_almost_equals(expected, calculated) def test_buffer_args(self): args = dict(cap_style=3, join_style=2, mitre_limit=2.5) calculated_series = self.g0.buffer(10, **args) for original, calculated in zip(self.g0, calculated_series): expected = original.buffer(10, **args) assert calculated.equals(expected) def test_envelope(self): e = self.g3.envelope assert np.all(e.geom_equals(self.sq)) assert isinstance(e, GeoSeries) assert self.g3.crs == e.crs def test_total_bounds(self): bbox = self.sol.x, self.sol.y, self.esb.x, self.esb.y assert isinstance(self.landmarks.total_bounds, np.ndarray) assert tuple(self.landmarks.total_bounds) == bbox df = GeoDataFrame({ 'geometry': self.landmarks, 'col1': range(len(self.landmarks)) }) assert tuple(df.total_bounds) == bbox def test_explode(self): s = GeoSeries([ MultiPoint([(0, 0), (1, 1)]), MultiPoint([(2, 2), (3, 3), (4, 4)]) ]) index = [(0, 0), (0, 1), (1, 0), (1, 1), (1, 2)] expected = GeoSeries( [Point(0, 0), Point(1, 1), Point(2, 2), Point(3, 3), Point(4, 4)], index=MultiIndex.from_tuples(index)) assert_geoseries_equal(expected, s.explode()) df = self.gdf1[:2].set_geometry(s) assert_geoseries_equal(expected, df.explode()) # # Test '&', '|', '^', and '-' # The left can only be a GeoSeries. The right hand side can be a # GeoSeries, GeoDataFrame or Shapely geometry # def test_intersection_operator(self): self._test_binary_operator('__and__', self.t1, self.g1, self.g2) def test_union_operator(self): self._test_binary_operator('__or__', self.sq, self.g1, self.g2) def test_union_operator_polygon(self): self._test_binary_operator('__or__', self.sq, self.g1, self.t2) def test_symmetric_difference_operator(self): self._test_binary_operator('__xor__', self.sq, self.g3, self.g4) def test_difference_series2(self): expected = GeoSeries([GeometryCollection(), self.t2]) self._test_binary_operator('__sub__', expected, self.g1, self.g2) def test_difference_poly2(self): expected = GeoSeries([self.t1, self.t1]) self._test_binary_operator('__sub__', expected, self.g1, self.t2)
def test_interiors(self): square_series = GeoSeries(self.nested_squares) exp_interiors = GeoSeries([LinearRing(self.inner_sq.boundary)]) for expected, computed in zip(exp_interiors, square_series.interiors): assert computed[0].equals(expected)
def test_collect_GeoSeries(self): s = GeoSeries([self.p1, self.p2, self.p3]) result = collect(s) assert self.mpc.equals(result)
def setup_method(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.t3 = Polygon([(2, 0), (3, 0), (3, 1)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.inner_sq = Polygon([(0.25, 0.25), (0.75, 0.25), (0.75, 0.75), (0.25, 0.75)]) self.nested_squares = Polygon(self.sq.boundary, [self.inner_sq.boundary]) self.p0 = Point(5, 5) self.g0 = GeoSeries([ self.t1, self.t2, self.sq, self.inner_sq, self.nested_squares, self.p0 ]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.g3.crs = {'init': 'epsg:4326', 'no_defs': True} self.g4 = GeoSeries([self.t2, self.t1]) self.g4.crs = {'init': 'epsg:4326', 'no_defs': True} self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, None]) self.a1 = self.g1.copy() self.a1.index = ['A', 'B'] self.a2 = self.g2.copy() self.a2.index = ['B', 'C'] self.esb = Point(-73.9847, 40.7484) self.sol = Point(-74.0446, 40.6893) self.landmarks = GeoSeries([self.esb, self.sol], crs={ 'init': 'epsg:4326', 'no_defs': True }) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) self.g6 = GeoSeries([self.p0, self.t3]) # Crossed lines self.l3 = LineString([(0, 0), (1, 1)]) self.l4 = LineString([(0, 1), (1, 0)]) self.crossed_lines = GeoSeries([self.l3, self.l4]) # Placeholder for testing, will just drop in different geometries # when needed self.gdf1 = GeoDataFrame({ 'geometry': self.g1, 'col0': [1.0, 2.0], 'col1': ['geo', 'pandas'] }) self.gdf2 = GeoDataFrame({ 'geometry': self.g1, 'col3': [4, 5], 'col4': ['rand', 'string'] })
def test_exterior(self): exp_exterior = GeoSeries([LinearRing(p.boundary) for p in self.g3]) for expected, computed in zip(exp_exterior, self.g3.exterior): assert computed.equals(expected)
def test_difference_poly(self): expected = GeoSeries([self.t1, self.t1]) self._test_binary_topological("difference", expected, self.g1, self.t2)
def test_buffer_distance_wrong_index(self): original = GeoSeries([self.p0, self.p0], index=[0, 1]) distances = Series(data=[1, 2], index=[99, 98]) with pytest.raises(ValueError): original.buffer(distances)
def iloc(cls, dataset, index): from geopandas import GeoSeries from shapely.geometry import MultiPoint rows, cols = index geom_dims = cls.geom_dims(dataset) geom_col = cls.geo_column(dataset.data) scalar = False columns = list(dataset.data.columns) if isinstance(cols, slice): cols = [d.name for d in dataset.dimensions()][cols] elif np.isscalar(cols): scalar = np.isscalar(rows) cols = [dataset.get_dimension(cols).name] else: cols = [dataset.get_dimension(d).name for d in index[1]] if not all(d in cols for d in geom_dims): raise DataError( "Cannot index a dimension which is part of the " "geometry column of a spatialpandas DataFrame.", cls) cols = list( unique_iterator([ columns.index(geom_col) if c in geom_dims else columns.index(c) for c in cols ])) geom_type = dataset.data[geom_col].geom_type.iloc[0] if geom_type != 'MultiPoint': if scalar: return dataset.data.iloc[rows[0], cols[0]] elif isscalar(rows): rows = [rows] return dataset.data.iloc[rows, cols] geoms = dataset.data[geom_col] count = 0 new_geoms, indexes = [], [] for i, geom in enumerate(geoms): length = len(geom.geoms) if np.isscalar(rows): if count <= rows < (count + length): new_geoms.append(geom.geoms[rows - count]) indexes.append(i) break elif isinstance(rows, slice): if rows.start is not None and rows.start > (count + length): continue elif rows.stop is not None and rows.stop < count: break start = None if rows.start is None else max( rows.start - count, 0) stop = None if rows.stop is None else min( rows.stop - count, length) if rows.step is not None: dataset.param.warning( ".iloc step slicing currently not supported for" "the multi-tabular data format.") indexes.append(i) new_geoms.append(geom.geoms[start:stop]) elif isinstance(rows, (list, set)): sub_rows = [(r - count) for r in rows if count <= r < (count + length)] if not sub_rows: continue indexes.append(i) new_geoms.append(MultiPoint([geom.geoms[r] for r in sub_rows])) count += length new = dataset.data.iloc[indexes].copy() new[geom_col] = GeoSeries(new_geoms) return new
def setup_method(self): self.t1 = Polygon([(0, 0), (1, 0), (1, 1)]) self.t2 = Polygon([(0, 0), (1, 1), (0, 1)]) self.t3 = Polygon([(2, 0), (3, 0), (3, 1)]) self.tz = Polygon([(1, 1, 1), (2, 2, 2), (3, 3, 3)]) self.tz1 = Polygon([(2, 2, 2), (1, 1, 1), (3, 3, 3)]) self.sq = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]) self.sqz = Polygon([(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4)]) self.t4 = Polygon([(0, 0), (3, 0), (3, 3), (0, 2)]) self.t5 = Polygon([(2, 0), (3, 0), (3, 3), (2, 3)]) self.inner_sq = Polygon([(0.25, 0.25), (0.75, 0.25), (0.75, 0.75), (0.25, 0.75)]) self.nested_squares = Polygon(self.sq.boundary, [self.inner_sq.boundary]) self.p0 = Point(5, 5) self.p3d = Point(5, 5, 5) self.g0 = GeoSeries([ self.t1, self.t2, self.sq, self.inner_sq, self.nested_squares, self.p0, None, ]) self.g1 = GeoSeries([self.t1, self.sq]) self.g2 = GeoSeries([self.sq, self.t1]) self.g3 = GeoSeries([self.t1, self.t2]) self.gz = GeoSeries([self.tz, self.sqz, self.tz1]) self.g3.crs = "epsg:4326" self.g4 = GeoSeries([self.t2, self.t1]) self.g4.crs = "epsg:4326" self.g_3d = GeoSeries([self.p0, self.p3d]) self.na = GeoSeries([self.t1, self.t2, Polygon()]) self.na_none = GeoSeries([self.t1, None]) self.a1 = self.g1.copy() self.a1.index = ["A", "B"] self.a2 = self.g2.copy() self.a2.index = ["B", "C"] self.esb = Point(-73.9847, 40.7484, 30.3244) self.sol = Point(-74.0446, 40.6893, 31.2344) self.landmarks = GeoSeries([self.esb, self.sol], crs="epsg:4326") self.pt2d = Point(-73.9847, 40.7484) self.landmarks_mixed = GeoSeries([self.esb, self.sol, self.pt2d], crs=4326) self.l1 = LineString([(0, 0), (0, 1), (1, 1)]) self.l2 = LineString([(0, 0), (1, 0), (1, 1), (0, 1)]) self.g5 = GeoSeries([self.l1, self.l2]) self.g6 = GeoSeries([self.p0, self.t3]) self.g7 = GeoSeries([self.sq, self.t4]) self.g8 = GeoSeries([self.t1, self.t5]) self.empty = GeoSeries([]) self.all_none = GeoSeries([None, None]) self.empty_poly = Polygon() self.g9 = GeoSeries(self.g0, index=range(1, 8)) # Crossed lines self.l3 = LineString([(0, 0), (1, 1)]) self.l4 = LineString([(0, 1), (1, 0)]) self.crossed_lines = GeoSeries([self.l3, self.l4]) # Placeholder for testing, will just drop in different geometries # when needed self.gdf1 = GeoDataFrame({ "geometry": self.g1, "col0": [1.0, 2.0], "col1": ["geo", "pandas"] }) self.gdf2 = GeoDataFrame({ "geometry": self.g1, "col3": [4, 5], "col4": ["rand", "string"] }) self.gdf3 = GeoDataFrame({ "geometry": self.g3, "col3": [4, 5], "col4": ["rand", "string"] }) self.gdfz = GeoDataFrame({ "geometry": self.gz, "col3": [4, 5, 6], "col4": ["rand", "string", "geo"] })
power_location_gdf = power_location_gdf.set_crs(epsg=4326) # print(power_location_gdf.crs) # %%create power transmission lines geodataframe lst = [] from busdata import * branch_list = list(zip(branch[:, 0], branch[:, 1])) for x in branch_list: a = int(x[0]) b = int(x[1]) transline = LineString( power_location_gdf.loc[a, 'geometry'].coords[:] + power_location_gdf.loc[b, 'geometry'].coords[:]) lst.append(transline) power_geoseries = GeoSeries(lst) print(power_geoseries) # power_geoseries.plot() # %%pipeline geodataframe import json belg = {} with open('belgian.json', 'r') as f: belg = json.load(f) # create the list of pipes as well as a dictionary with keys of numberings and values of pipe names c = 1 pipe_dict = {} pipe_list = [] for idx, component in belg['pipe'].items():
def test_convex_hull(self): # the convex hull of a square should be the same as the square squares = GeoSeries([self.sq for i in range(3)]) assert_geoseries_equal(squares, squares.convex_hull)