def test_overlay(dfs, how, use_sindex, expected_features): """ Basic overlay test with small dummy example dataframes (from docs). Results obtained using QGIS 2.16 (Vector -> Geoprocessing Tools -> Intersection / Union / ...), saved to GeoJSON and pasted here """ df1, df2 = dfs result = overlay(df1, df2, how=how, use_sindex=use_sindex) # construction of result if how == 'identity': expected = pd.concat([ GeoDataFrame.from_features(expected_features['intersection']), GeoDataFrame.from_features(expected_features['difference']) ], ignore_index=True) else: expected = GeoDataFrame.from_features(expected_features[how]) # TODO needed adaptations to result # if how == 'union': # result = result.drop(['idx1', 'idx2'], axis=1).sort_values(['col1', 'col2']).reset_index(drop=True) # elif how in ('intersection', 'identity'): # result = result.drop(['idx1', 'idx2'], axis=1) assert_geodataframe_equal(result, expected) # for difference also reversed if how == 'difference': result = overlay(df2, df1, how=how, use_sindex=use_sindex) expected = GeoDataFrame.from_features( expected_features['difference_inverse']) assert_geodataframe_equal(result, expected)
def test_to_file_int64(tmpdir, df_points): tempfilename = os.path.join(str(tmpdir), "int64.shp") geometry = df_points.geometry df = GeoDataFrame(geometry=geometry) df["data"] = pd.array([1, np.nan] * 5, dtype=pd.Int64Dtype()) df.to_file(tempfilename) df_read = GeoDataFrame.from_file(tempfilename) assert_geodataframe_equal(df_read, df, check_dtype=False, check_like=True)
def test_clip_points(point_gdf, single_rectangle_gdf): """Test clipping a points GDF with a generic polygon geometry.""" clip_pts = clip(point_gdf, single_rectangle_gdf) pts = np.array([[2, 2], [3, 4], [9, 8]]) exp = GeoDataFrame([Point(xy) for xy in pts], columns=["geometry"], crs="EPSG:3857") assert_geodataframe_equal(clip_pts, exp)
def test_tz_cols(self, example_positionfixes): """Test if columns get casted to datetimes.""" pfs = example_positionfixes.copy() pfs["tracked_at"] = [ "1971-01-01 04:00:00", "1971-01-01 05:00:00", "1971-01-02 07:00:00" ] pfs = _trackintel_model(pfs, tz_cols=["tracked_at"], tz="UTC") assert_geodataframe_equal(pfs, example_positionfixes)
def test_two_users(self, example_osna): """Test if two users are handled correctly.""" two_user = example_osna.append(example_osna) two_user.iloc[len(example_osna):, 0] = 1 # second user gets id 1 result = osna_method(two_user) two_user.loc[two_user["location_id"] == 0, "activity_label"] = "home" two_user.loc[two_user["location_id"] == 1, "activity_label"] = "work" assert_geodataframe_equal(result, two_user)
def test_default(self, example_osna): """Test with no changes to test data.""" osna = osna_method(example_osna) example_osna.loc[example_osna["location_id"] == 0, "activity_label"] = "home" example_osna.loc[example_osna["location_id"] == 1, "activity_label"] = "work" assert_geodataframe_equal(example_osna, osna)
def test_transform2(epsg4326, epsg26918): df = df_epsg26918() lonlat = df.to_crs(**epsg4326) utm = lonlat.to_crs(**epsg26918) # can't check for CRS equality, as the formats differ although representing # the same CRS assert_geodataframe_equal(df, utm, check_less_precise=True, check_crs=False)
def test_empty_intersection(dfs): df1, df2 = dfs polys3 = GeoSeries([Polygon([(-1, -1), (-3, -1), (-3, -3), (-1, -3)]), Polygon([(-3, -3), (-5, -3), (-5, -5), (-3, -5)])]) df3 = GeoDataFrame({'geometry': polys3, 'col3': [1, 2]}) expected = GeoDataFrame([], columns=['col1', 'col3', 'geometry']) result = overlay(df1, df3) assert_geodataframe_equal(result, expected, check_like=True)
def test_overlay_nybb(how): polydf = read_file(geopandas.datasets.get_path('nybb')) # construct circles dataframe N = 10 b = [int(x) for x in polydf.total_bounds] polydf2 = GeoDataFrame( [{'geometry': Point(x, y).buffer(10000), 'value1': x + y, 'value2': x - y} for x, y in zip(range(b[0], b[2], int((b[2]-b[0])/N)), range(b[1], b[3], int((b[3]-b[1])/N)))], crs=polydf.crs) result = overlay(polydf, polydf2, how=how) cols = ['BoroCode', 'BoroName', 'Shape_Leng', 'Shape_Area', 'value1', 'value2'] if how == 'difference': cols = cols[:-2] # expected result if how == 'identity': # read union one, further down below we take the appropriate subset expected = read_file(os.path.join( DATA, 'nybb_qgis', 'qgis-union.shp')) else: expected = read_file(os.path.join( DATA, 'nybb_qgis', 'qgis-{0}.shp'.format(how))) # The result of QGIS for 'union' contains incorrect geometries: # 24 is a full original circle overlapping with unioned geometries, and # 27 is a completely duplicated row) if how == 'union': expected = expected.drop([24, 27]) expected.reset_index(inplace=True, drop=True) # Eliminate observations without geometries (issue from QGIS) expected = expected[expected.is_valid] expected.reset_index(inplace=True, drop=True) if how == 'identity': expected = expected[expected.BoroCode.notnull()].copy() # Order GeoDataFrames expected = expected.sort_values(cols).reset_index(drop=True) # TODO needed adaptations to result result = result.sort_values(cols).reset_index(drop=True) if how in ('union', 'identity'): # concat < 0.23 sorts, so changes the order of the columns # but at least we ensure 'geometry' is the last column assert result.columns[-1] == 'geometry' assert len(result.columns) == len(expected.columns) result = result.reindex(columns=expected.columns) assert_geodataframe_equal(result, expected, check_crs=False, check_column_type=False,)
def test_round_trip_current(tmpdir, current_pickle_data): data = current_pickle_data for name, value in data.items(): path = str(tmpdir / "{}.pickle".format(name)) value.to_pickle(path) result = pd.read_pickle(path) assert_geodataframe_equal(result, value) assert isinstance(result.has_sindex, bool)
def test_split_out_name(self): gpd_default = self.world.rename_geometry("geom").dissolve("continent") ddf = dask_geopandas.from_geopandas(self.world.rename_geometry("geom"), npartitions=4) dd_split = ddf.dissolve("continent", split_out=4) assert dd_split.npartitions == 4 assert_geodataframe_equal(gpd_default, dd_split.compute(), check_like=True)
def test_no_intersection(): # overlapping bounds but non-overlapping geometries gs = GeoSeries([Point(x, x).buffer(0.1) for x in range(3)]) gdf1 = GeoDataFrame({"foo": ["a", "b", "c"]}, geometry=gs) gdf2 = GeoDataFrame({"bar": ["1", "3", "5"]}, geometry=gs.translate(1)) expected = GeoDataFrame(columns=["foo", "bar", "geometry"]) result = overlay(gdf1, gdf2, how="intersection") assert_geodataframe_equal(result, expected, check_index_type=False)
def test_setting_geometry(self, example_positionfixes): """Test the setting of the geometry.""" # create pfs as dataframe pfs = pd.DataFrame(example_positionfixes[["user_id", "tracked_at"]], copy=True) pfs["geom"] = example_positionfixes.geometry # check if geom column gets assigned to geometry pfs = _trackintel_model(pfs, geom_col="geom") assert_geodataframe_equal(example_positionfixes, pfs)
def test_clip_with_polygon(single_rectangle_gdf): """Test clip when using a shapely object""" polygon = Polygon([(0, 0), (5, 12), (10, 0), (0, 0)]) clipped = clip(single_rectangle_gdf, polygon) exp_poly = polygon.intersection( Polygon([(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)])) exp = GeoDataFrame([1], geometry=[exp_poly], crs="EPSG:3857") exp["attr2"] = "site-boundary" assert_geodataframe_equal(clipped, exp)
def test_extent_col(self, example_locations): """Test function with optional geom-column "extent".""" locs = example_locations.copy() del locs["extent"] coords = [[8.45, 47.6], [8.45, 47.4], [8.55, 47.4], [8.55, 47.6], [8.45, 47.6]] locs["extent_wrongname"] = Polygon(coords) locs = read_locations_gpd(locs, extent="extent_wrongname") assert_geodataframe_equal(locs, example_locations)
def test_prior_activity_label(self, example_osna): """Test that prior activity_label column does not corrupt output.""" example_osna["activity_label"] = np.arange(len(example_osna)) result = osna_method(example_osna) del example_osna["activity_label"] example_osna.loc[example_osna["location_id"] == 0, "activity_label"] = "home" example_osna.loc[example_osna["location_id"] == 1, "activity_label"] = "work" assert_geodataframe_equal(example_osna, result)
def test_custom_labels(self, example_freq): """Test method with custom label of a different length""" custom_label = "doing_nothing" freq = freq_method(example_freq, "doing_nothing") example_freq["activity_label"] = None example_freq.loc[example_freq["location_id"] == 0, "activity_label"] = custom_label assert freq["activity_label"].count( ) == example_freq["activity_label"].count() assert_geodataframe_equal(example_freq, freq)
def test_default(self): expected = self.world.set_index( _hilbert_distance(self.world, self.world.total_bounds, level=16), ).sort_index() ddf = self.ddf.spatial_shuffle() assert ddf.npartitions == self.ddf.npartitions assert isinstance(ddf.spatial_partitions, geopandas.GeoSeries) assert_geodataframe_equal(ddf.compute(), expected)
def test_to_crs_geo_column_name(): # Test to_crs() with different geometry column name (GH#339) df = df_epsg26918() df = df.rename(columns={'geometry': 'geom'}) df.set_geometry('geom', inplace=True) lonlat = df.to_crs(epsg=4326) utm = lonlat.to_crs(epsg=26918) assert lonlat.geometry.name == 'geom' assert utm.geometry.name == 'geom' assert_geodataframe_equal(df, utm, check_less_precise=True)
def test_to_crs_geo_column_name(): # Test to_crs() with different geometry column name (GH#339) df = df_epsg26918() df = df.rename(columns={"geometry": "geom"}) df.set_geometry("geom", inplace=True) lonlat = df.to_crs(epsg=4326) utm = lonlat.to_crs(epsg=26918) assert lonlat.geometry.name == "geom" assert utm.geometry.name == "geom" assert_geodataframe_equal(df, utm, check_less_precise=True)
def test_non_overlapping_geoms(): """Test that a bounding box returns error if the extents don't overlap""" unit_box = Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)]) unit_gdf = GeoDataFrame([1], geometry=[unit_box], crs="EPSG:4326") non_overlapping_gdf = unit_gdf.copy() non_overlapping_gdf = non_overlapping_gdf.geometry.apply( lambda x: shapely.affinity.translate(x, xoff=20)) out = clip(unit_gdf, non_overlapping_gdf) assert_geodataframe_equal( out, GeoDataFrame(columns=unit_gdf.columns, crs=unit_gdf.crs))
def test_transformation(self): """Check if data gets transformed.""" file = os.path.join("tests", "data", "positionfixes.csv") pfs = ti.read_positionfixes_csv(file, sep=";", crs="EPSG:4326", index_col=None) pfs_2056 = pfs.to_crs("EPSG:2056") _, pfs_4326 = check_gdf_crs(pfs_2056, transform=True) assert_geodataframe_equal(pfs, pfs_4326, check_less_precise=True)
def test_empty_intersection(dfs): df1, df2 = dfs polys3 = GeoSeries([ Polygon([(-1, -1), (-3, -1), (-3, -3), (-1, -3)]), Polygon([(-3, -3), (-5, -3), (-5, -5), (-3, -5)]), ]) df3 = GeoDataFrame({"geometry": polys3, "col3": [1, 2]}) expected = GeoDataFrame([], columns=["col1", "col3", "geometry"]) result = overlay(df1, df3) assert_geodataframe_equal(result, expected, check_dtype=False)
def test_pfs_without_sp(self, geolife_pfs_sp_long): """Delete pfs that belong to staypoints and see if they are detected.""" pfs, sp = geolife_pfs_sp_long _, tpls_case1 = pfs.as_positionfixes.generate_triplegs(sp, method="between_staypoints") # only keep pfs where staypoint id is nan pfs_no_sp = pfs[pd.isna(pfs["staypoint_id"])].drop(columns="staypoint_id") _, tpls_case2 = pfs_no_sp.as_positionfixes.generate_triplegs(sp, method="between_staypoints") assert_geodataframe_equal(tpls_case1, tpls_case2)
def test_overlay(self, dfs, how): """ Basic test for availability of the GeoDataFrame method. Other overlay tests are located in tests/test_overlay.py """ df1, df2 = dfs expected = geopandas.overlay(df1, df2, how=how) result = df1.overlay(df2, how=how) assert_geodataframe_equal(result, expected)
def test_h3_to_parent_aggregate(h3_geodataframe_with_values): result = h3_geodataframe_with_values.h3.h3_to_parent_aggregate(8) # TODO: Why does Pandas not preserve the order of groups here? index = pd.Index(["881f1d4811fffff", "881f1d4817fffff"], name="h3_08") geometry = [Polygon(h3.h3_to_geo_boundary(h, True)) for h in index] expected = gpd.GeoDataFrame( {"val": [5, 3]}, geometry=geometry, index=index, crs="epsg:4326" ) assert_geodataframe_equal(expected, result)
def test_Tessellation(self): tes = mm.Tessellation(self.df_buildings, "uID", self.limit, segment=2) tessellation = tes.tessellation assert len(tessellation) == len(self.df_tessellation) bands = mm.Tessellation(self.df_streets, "nID", mm.buffered_limit(self.df_streets, 50), segment=5).tessellation assert len(bands) == len(self.df_streets) # test_enclosed_tessellation enc1 = mm.Tessellation(self.df_buildings, "uID", enclosures=self.enclosures).tessellation assert len(enc1) == 155 assert isinstance(enc1, gpd.GeoDataFrame) enc1_loop = mm.Tessellation(self.df_buildings, "uID", enclosures=self.enclosures, use_dask=False).tessellation assert len(enc1) == 155 assert isinstance(enc1, gpd.GeoDataFrame) assert len(enc1_loop) == 155 assert isinstance(enc1_loop, gpd.GeoDataFrame) assert_geodataframe_equal(enc1, enc1_loop) with pytest.raises(ValueError): mm.Tessellation(self.df_buildings, "uID", limit=self.limit, enclosures=self.enclosures) enc1_loop = mm.Tessellation(self.df_buildings, "uID", enclosures=self.enclosures, use_dask=False).tessellation assert len(enc1) == 155 assert isinstance(enc1, gpd.GeoDataFrame) # erroneous geometry df = self.df_buildings b = df.total_bounds x = np.mean([b[0], b[2]]) y = np.mean([b[1], b[3]]) df.loc[144] = [145, Polygon([(x, y), (x, y + 1), (x + 1, y)])] df.loc[145] = [146, MultiPoint([(x, y), (x + 1, y)]).buffer(0.55)] df.loc[146] = [147, affinity.rotate(df.geometry.iloc[0], 12)] tess = mm.Tessellation(df, "uID", self.limit) assert tess.collapsed == {145} assert len(tess.multipolygons) == 3
def test_overlay_overlap(how): """ Overlay test with overlapping geometries in both dataframes. Test files are created with:: import geopandas from geopandas import GeoSeries, GeoDataFrame from shapely.geometry import Point, Polygon, LineString s1 = GeoSeries([Point(0, 0), Point(1.5, 0)]).buffer(1, resolution=2) s2 = GeoSeries([Point(1, 1), Point(2, 2)]).buffer(1, resolution=2) df1 = GeoDataFrame({'geometry': s1, 'col1':[1,2]}) df2 = GeoDataFrame({'geometry': s2, 'col2':[1, 2]}) ax = df1.plot(alpha=0.5) df2.plot(alpha=0.5, ax=ax, color='C1') df1.to_file('geopandas/geopandas/tests/data/df1_overlap.geojson', driver='GeoJSON') df2.to_file('geopandas/geopandas/tests/data/df2_overlap.geojson', driver='GeoJSON') and then overlay results are obtained from using QGIS 2.16 (Vector -> Geoprocessing Tools -> Intersection / Union / ...), saved to GeoJSON. """ df1 = read_file(os.path.join(DATA, "overlap", "df1_overlap.geojson")) df2 = read_file(os.path.join(DATA, "overlap", "df2_overlap.geojson")) result = overlay(df1, df2, how=how) if how == "identity": raise pytest.skip() expected = read_file( os.path.join(DATA, "overlap", "df1_df2_overlap-{0}.geojson".format(how))) if how == "union": # the QGIS result has the last row duplicated, so removing this expected = expected.iloc[:-1] # TODO needed adaptations to result result = result.reset_index(drop=True) if how == "union": result = result.sort_values(["col1", "col2"]).reset_index(drop=True) assert_geodataframe_equal( result, expected, normalize=True, check_column_type=False, check_less_precise=True, )
def test_transform2(epsg4326, epsg26918): # with PROJ >= 7, the transformation using EPSG code vs proj4 string is # slightly different due to use of grid files or not -> turn off network # to not use grid files at all for this test os.environ["PROJ_NETWORK"] = "OFF" df = df_epsg26918() lonlat = df.to_crs(**epsg4326) utm = lonlat.to_crs(**epsg26918) # can't check for CRS equality, as the formats differ although representing # the same CRS assert_geodataframe_equal(df, utm, check_less_precise=True, check_crs=False)
def test_transformation(self): """Check if data gets transformed.""" file = os.path.join('tests', 'data', 'positionfixes.csv') pfs = ti.read_positionfixes_csv(file, sep=';', crs='EPSG:4326', index_col=None) pfs_2056 = pfs.to_crs("EPSG:2056") pfs_4326 = ti.visualization.util.transform_gdf_to_wgs84(pfs_2056) assert_geodataframe_equal(pfs, pfs_4326, check_less_precise=True)
def test_parallel_computing(self): """The result obtained with parallel computing should be identical.""" pfs, _ = ti.io.dataset_reader.read_geolife(os.path.join("tests", "data", "geolife_long")) # without parallel computing code pfs_ori, sp_ori = pfs.as_positionfixes.generate_staypoints(n_jobs=1) # using two cores pfs_para, sp_para = pfs.as_positionfixes.generate_staypoints(n_jobs=2) # the result of parallel computing should be identical assert_geodataframe_equal(pfs_ori, pfs_para) assert_geodataframe_equal(sp_ori, sp_para)
def deep_eq(fst_obj, snd_obj): """Compares whether fst_obj and snd_obj are deeply equal. In case when both fst_obj and snd_obj are of type np.ndarray or either np.memmap, they are compared using np.array_equal(fst_obj, snd_obj). Otherwise, when they are lists or tuples, they are compared for length and then deep_eq is applied component-wise. When they are dict, they are compared for key set equality, and then deep_eq is applied value-wise. For all other data types that are not list, tuple, dict, or np.ndarray, the method falls back to the __eq__ method. Because np.ndarray is not a hashable object, it is impossible to form a set of numpy arrays, hence deep_eq works correctly. :param fst_obj: First object compared :param snd_obj: Second object compared :return: `True` if objects are deeply equal, `False` otherwise """ # pylint: disable=too-many-return-statements if not isinstance(fst_obj, type(snd_obj)): return False if isinstance(fst_obj, np.ndarray): if fst_obj.dtype != snd_obj.dtype: return False fst_nan_mask = np.isnan(fst_obj) snd_nan_mask = np.isnan(snd_obj) return np.array_equal(fst_obj[~fst_nan_mask], snd_obj[~snd_nan_mask]) and \ np.array_equal(fst_nan_mask, snd_nan_mask) if isinstance(fst_obj, gpd.GeoDataFrame): try: assert_geodataframe_equal(fst_obj, snd_obj) return True except AssertionError: return False if isinstance(fst_obj, (list, tuple)): if len(fst_obj) != len(snd_obj): return False for element_fst, element_snd in zip(fst_obj, snd_obj): if not deep_eq(element_fst, element_snd): return False return True if isinstance(fst_obj, dict): if fst_obj.keys() != snd_obj.keys(): return False for key in fst_obj: if not deep_eq(fst_obj[key], snd_obj[key]): return False return True return fst_obj == snd_obj
def test_correct_index(dfs): # GH883 - case where the index was not properly reset df1, df2 = dfs polys3 = GeoSeries([Polygon([(1, 1), (3, 1), (3, 3), (1, 3)]), Polygon([(-1, 1), (1, 1), (1, 3), (-1, 3)]), Polygon([(3, 3), (5, 3), (5, 5), (3, 5)])]) df3 = GeoDataFrame({'geometry': polys3, 'col3': [1, 2, 3]}) i1 = Polygon([(1, 1), (1, 3), (3, 3), (3, 1), (1, 1)]) i2 = Polygon([(3, 3), (3, 5), (5, 5), (5, 3), (3, 3)]) expected = GeoDataFrame([[1, 1, i1], [3, 2, i2]], columns=['col3', 'col2', 'geometry']) result = overlay(df3, df2) assert_geodataframe_equal(result, expected)
def test_overlay_overlap(how): """ Overlay test with overlapping geometries in both dataframes. Test files are created with:: import geopandas from geopandas import GeoSeries, GeoDataFrame from shapely.geometry import Point, Polygon, LineString s1 = GeoSeries([Point(0, 0), Point(1.5, 0)]).buffer(1, resolution=2) s2 = GeoSeries([Point(1, 1), Point(2, 2)]).buffer(1, resolution=2) df1 = GeoDataFrame({'geometry': s1, 'col1':[1,2]}) df2 = GeoDataFrame({'geometry': s2, 'col2':[1, 2]}) ax = df1.plot(alpha=0.5) df2.plot(alpha=0.5, ax=ax, color='C1') df1.to_file('geopandas/geopandas/tests/data/df1_overlap.geojson', driver='GeoJSON') df2.to_file('geopandas/geopandas/tests/data/df2_overlap.geojson', driver='GeoJSON') and then overlay results are obtained from using QGIS 2.16 (Vector -> Geoprocessing Tools -> Intersection / Union / ...), saved to GeoJSON. """ df1 = read_file(os.path.join(DATA, 'overlap', 'df1_overlap.geojson')) df2 = read_file(os.path.join(DATA, 'overlap', 'df2_overlap.geojson')) result = overlay(df1, df2, how=how) if how == 'identity': raise pytest.skip() expected = read_file(os.path.join( DATA, 'overlap', 'df1_df2_overlap-{0}.geojson'.format(how))) if how == 'union': # the QGIS result has the last row duplicated, so removing this expected = expected.iloc[:-1] # TODO needed adaptations to result result = result.reset_index(drop=True) if how == 'union': result = result.sort_values(['col1', 'col2']).reset_index(drop=True) assert_geodataframe_equal(result, expected, check_column_type=False, check_less_precise=True)
def test_geodataframe(): assert_geodataframe_equal(df1, df2) with pytest.raises(AssertionError): assert_geodataframe_equal(df1, df2, check_less_precise=True) with pytest.raises(AssertionError): assert_geodataframe_equal(df1, df2[['geometry', 'col1']]) assert_geodataframe_equal(df1, df2[['geometry', 'col1']], check_like=True) df3 = df2.copy() df3.loc[0, 'col1'] = 10 with pytest.raises(AssertionError): assert_geodataframe_equal(df1, df3)
def test_overlay(dfs_index, how, use_sindex): """ Basic overlay test with small dummy example dataframes (from docs). Results obtained using QGIS 2.16 (Vector -> Geoprocessing Tools -> Intersection / Union / ...), saved to GeoJSON """ df1, df2 = dfs_index result = overlay(df1, df2, how=how, use_sindex=use_sindex) # construction of result def _read(name): expected = read_file( os.path.join(DATA, 'polys', 'df1_df2-{0}.geojson'.format(name))) expected.crs = None return expected if how == 'identity': expected_intersection = _read('intersection') expected_difference = _read('difference') expected = pd.concat([ expected_intersection, expected_difference ], ignore_index=True, sort=False) expected['col1'] = expected['col1'].astype(float) else: expected = _read(how) # TODO needed adaptations to result if how == 'union': result = result.sort_values(['col1', 'col2']).reset_index(drop=True) elif how == 'difference': result = result.reset_index(drop=True) assert_geodataframe_equal(result, expected, check_column_type=False) # for difference also reversed if how == 'difference': result = overlay(df2, df1, how=how, use_sindex=use_sindex) result = result.reset_index(drop=True) expected = _read('difference-inverse') assert_geodataframe_equal(result, expected, check_column_type=False)
def test_geometry_not_named_geometry(dfs, how, other_geometry): # Issue #306 # Add points and flip names df1, df2 = dfs df3 = df1.copy() df3 = df3.rename(columns={'geometry': 'polygons'}) df3 = df3.set_geometry('polygons') if other_geometry: df3['geometry'] = df1.centroid.geometry assert df3.geometry.name == 'polygons' res1 = overlay(df1, df2, how=how) res2 = overlay(df3, df2, how=how) assert df3.geometry.name == 'polygons' if how == 'difference': # in case of 'difference', column names of left frame are preserved assert res2.geometry.name == 'polygons' if other_geometry: assert 'geometry' in res2.columns assert_geoseries_equal(res2['geometry'], df3['geometry'], check_series_type=False) res2 = res2.drop(['geometry'], axis=1) res2 = res2.rename(columns={'polygons': 'geometry'}) res2 = res2.set_geometry('geometry') # TODO if existing column is overwritten -> geometry not last column if other_geometry and how == 'intersection': res2 = res2.reindex(columns=res1.columns) assert_geodataframe_equal(res1, res2) df4 = df2.copy() df4 = df4.rename(columns={'geometry': 'geom'}) df4 = df4.set_geometry('geom') if other_geometry: df4['geometry'] = df2.centroid.geometry assert df4.geometry.name == 'geom' res1 = overlay(df1, df2, how=how) res2 = overlay(df1, df4, how=how) assert_geodataframe_equal(res1, res2)
def test_align(self): df = self.df2 res1, res2 = df.align(df) assert_geodataframe_equal(res1, df) assert_geodataframe_equal(res2, df) res1, res2 = df.align(df.copy()) assert_geodataframe_equal(res1, df) assert_geodataframe_equal(res2, df) # assert crs is / is not preserved on mixed dataframes df_nocrs = df.copy() df_nocrs.crs = None res1, res2 = df.align(df_nocrs) assert_geodataframe_equal(res1, df) assert res1.crs is not None assert_geodataframe_equal(res2, df_nocrs) assert res2.crs is None # mixed GeoDataFrame / DataFrame df_nogeom = pd.DataFrame(df.drop('geometry', axis=1)) res1, res2 = df.align(df_nogeom, axis=0) assert_geodataframe_equal(res1, df) assert type(res2) == pd.DataFrame assert_frame_equal(res2, df_nogeom) # same as above but now with actual alignment df1 = df.iloc[1:].copy() df2 = df.iloc[:-1].copy() exp1 = df.copy() exp1.iloc[0] = np.nan exp2 = df.copy() exp2.iloc[-1] = np.nan res1, res2 = df1.align(df2) assert_geodataframe_equal(res1, exp1) assert_geodataframe_equal(res2, exp2) df2_nocrs = df2.copy() df2_nocrs.crs = None exp2_nocrs = exp2.copy() exp2_nocrs.crs = None res1, res2 = df1.align(df2_nocrs) assert_geodataframe_equal(res1, exp1) assert res1.crs is not None assert_geodataframe_equal(res2, exp2_nocrs) assert res2.crs is None df2_nogeom = pd.DataFrame(df2.drop('geometry', axis=1)) exp2_nogeom = pd.DataFrame(exp2.drop('geometry', axis=1)) res1, res2 = df1.align(df2_nogeom, axis=0) assert_geodataframe_equal(res1, exp1) assert type(res2) == pd.DataFrame assert_frame_equal(res2, exp2_nogeom)
def test_no_crs(): df1 = GeoDataFrame({'col1': [1, 2], 'geometry': s1}, crs=None) df2 = GeoDataFrame({'col1': [1, 2], 'geometry': s1}, crs={}) assert_geodataframe_equal(df1, df2)
def test_to_crs_transform(): df = df_epsg26918() lonlat = df.to_crs(epsg=4326) utm = lonlat.to_crs(epsg=26918) assert_geodataframe_equal(df, utm, check_less_precise=True)
def test_to_crs_inplace(): df = df_epsg26918() lonlat = df.to_crs(epsg=4326) df.to_crs(epsg=4326, inplace=True) assert_geodataframe_equal(df, lonlat, check_less_precise=True)