def test_read_locations_csv_crs_parameter(self): file = os.path.join('tests', 'data', 'locations.csv') crs = "EPSG:2056" locs = ti.read_locations_csv(file, sep=';', index_col="id") assert locs.crs is None locs = ti.read_locations_csv(file, sep=';', index_col="id", crs=crs) assert locs.crs == crs
def test_locations_csv_index_col(self): file = os.path.join('tests', 'data', 'locations.csv') ind_name = 'id' pfs = ti.read_locations_csv(file, sep=";", index_col=ind_name) assert pfs.index.name == ind_name pfs = ti.read_locations_csv(file, sep=";", index_col=None) assert pfs.index.name is None
def test_set_index(self): """Test if `index_col` can be set.""" file = os.path.join("tests", "data", "locations.csv") ind_name = "id" pfs = ti.read_locations_csv(file, sep=";", index_col=ind_name) assert pfs.index.name == ind_name pfs = ti.read_locations_csv(file, sep=";", index_col=None) assert pfs.index.name is None
def test_set_crs(self): """Test setting the crs when reading.""" file = os.path.join("tests", "data", "locations.csv") crs = "EPSG:2056" locs = ti.read_locations_csv(file, sep=";", index_col="id") assert locs.crs is None locs = ti.read_locations_csv(file, sep=";", index_col="id", crs=crs) assert locs.crs == crs
def test_locations_from_to_csv(self): orig_file = os.path.join('tests', 'data', 'locations.csv') mod_file = os.path.join('tests', 'data', 'locations_mod_columns.csv') tmp_file = os.path.join('tests', 'data', 'locations_test.csv') mod_locs = ti.read_locations_csv(mod_file, columns={'geom': 'center'}, sep=';', index_col="id") locs = ti.read_locations_csv(orig_file, sep=';', index_col="id") assert mod_locs.equals(locs) locs.as_locations.to_csv(tmp_file, sep=';', columns=['user_id', 'elevation', 'center', 'extent']) assert filecmp.cmp(orig_file, tmp_file, shallow=False) os.remove(tmp_file)
def test_from_to_csv(self): """Test basic reading and writing functions.""" orig_file = os.path.join("tests", "data", "locations.csv") mod_file = os.path.join("tests", "data", "locations_mod_columns.csv") tmp_file = os.path.join("tests", "data", "locations_test_1.csv") mod_locs = ti.read_locations_csv(mod_file, columns={"geom": "center"}, sep=";", index_col="id") locs = ti.read_locations_csv(orig_file, sep=";", index_col="id") assert mod_locs.equals(locs) locs.as_locations.to_csv( tmp_file, sep=";", columns=["user_id", "elevation", "center", "extent"]) assert filecmp.cmp(orig_file, tmp_file, shallow=False) os.remove(tmp_file)
def test_locations_from_to_csv(self): orig_file = os.path.join('tests', 'data', 'locations.csv') tmp_file = os.path.join('tests', 'data', 'locations_test.csv') plcs = ti.read_locations_csv(orig_file, sep=';') plcs.as_locations.to_csv( tmp_file, sep=';', columns=['user_id', 'elevation', 'center', 'extent']) assert filecmp.cmp(orig_file, tmp_file, shallow=False) os.remove(tmp_file)
def test_locations_from_gpd(self): # TODO: Problem with multiple geometry columns and geojson format gdf = gpd.read_file(os.path.join('tests', 'data', 'locations.geojson')) gdf.set_index('id', inplace=True) locs_from_gpd = ti.io.from_geopandas.locations_from_gpd(gdf, user_id='User', center='geometry') locs_file = os.path.join('tests', 'data', 'locations.csv') locs_from_csv = ti.read_locations_csv(locs_file, sep=';', index_col='id') # drop the second geometry column manually because not storable in GeoJSON (from Geopandas) locs_from_csv = locs_from_csv.drop(columns='extent') pd.testing.assert_frame_equal(locs_from_csv, locs_from_gpd, check_exact=False)
def test_locations_plot(self): """Use trackintel visualization function to plot locations and check if file exists.""" tmp_file = os.path.join("tests", "data", "locations_plot.png") pfs_file = os.path.join('tests', 'data', 'positionfixes.csv') pfs = pfs = ti.read_positionfixes_csv(pfs_file, sep=';', index_col='id', crs='EPSG:4326') stps_file = os.path.join('tests', 'data', 'staypoints.csv') stps = ti.read_staypoints_csv(stps_file, sep=';', index_col='id', crs='EPSG:4326') locs_file = os.path.join('tests', 'data', 'locations.csv') locs = ti.read_locations_csv(locs_file, sep=';', index_col='id', crs='EPSG:4326') locs.as_locations.plot(out_filename=tmp_file, radius=120, positionfixes=pfs, staypoints=stps, staypoints_radius=100, plot_osm=False) assert os.path.exists(tmp_file) os.remove(tmp_file)
def test_read_locations_gpd(self): """Test if the results of reading from gpd and csv agrees.""" # TODO: Problem with multiple geometry columns and geojson format gdf = gpd.read_file(os.path.join("tests", "data", "locations.geojson")) gdf.set_index("id", inplace=True) locs_from_gpd = ti.io.from_geopandas.read_locations_gpd( gdf, user_id="User", center="geometry") locs_file = os.path.join("tests", "data", "locations.csv") locs_from_csv = ti.read_locations_csv(locs_file, sep=";", index_col="id") # drop the second geometry column manually because not storable in GeoJSON (from Geopandas) locs_from_csv = locs_from_csv.drop(columns="extent") pd.testing.assert_frame_equal(locs_from_csv, locs_from_gpd, check_exact=False)
def test_locations_csv_index_warning(self): """Test if a warning is raised when not parsing the index_col argument.""" file = os.path.join('tests', 'data', 'locations.csv') with pytest.warns(UserWarning): ti.read_locations_csv(file, sep=';')
def test_set_index_warning(self): """Test if a warning is raised when not parsing the index_col argument.""" file = os.path.join("tests", "data", "locations.csv") with pytest.warns(UserWarning): ti.read_locations_csv(file, sep=";")