def test_format_geodataframe(): # GIVEN bpolys = geojson.FeatureCollection([{"type": "Feature", "properties": {"id": 0}, "geometry": {"coordinates": [ [[[13, 51], [13, 51.1], [13.1, 51.1], [13.1, 51], [13, 51]]], [[[14, 51], [14, 51.1], [14.1, 51.1], [14.1, 51], [14, 51]]]], "type": "MultiPolygon"}}]) bpolys_df = gpd.GeoDataFrame().from_features(bpolys) time = "2018-01-01" keys = ["amenity"] values = [""] format = "geojson" properties = ["tags", "metadata"] # WHEN client = ohsome.OhsomeClient() response = client.elements.count.groupBy.boundary.post(bpolys=bpolys_df, time=time, keys=keys, values=values, format=format, properties=properties) result = response.as_geodataframe() del client # THEN assert result["value"][0] == 538
def ohsome_users_tokyo(bpolys, timeperiod): """ :param bpolys: :return: """ client = ohsome.OhsomeClient() # All users res = client.users.count.post(bpolys=bpolys, time=timeperiod, types=["NODE", "WAY"]) users_all_unstacked = res.as_dataframe().drop( "fromTimestamp", axis=1).set_index("toTimestamp") users_all_unstacked.rename(columns={"value": "All active users"}, inplace=True) # Users mapping parks res = client.users.count.post(bpolys=bpolys, keys=["leisure"], values=["park"], time=timeperiod, types=["WAY", "RELATION"]) users_parks_unstacked = res.as_dataframe().drop( "fromTimestamp", axis=1).set_index("toTimestamp") users_parks_unstacked.rename(columns={"value": "Users mapping parks"}, inplace=True) del client return users_all_unstacked.join(users_parks_unstacked)
def ohsome_parks_over_time(config_cities, keys, values, timeperiod, types): """ Calculates the number of parks in OSM for each city over time """ # Create bounding box geometry of each city bboxes = { c: box(*create_bbox(config_cities[c]["center"], config_cities[c] ["width"])) for c in config_cities.keys() } bbox_df = pd.DataFrame().from_dict(bboxes, orient="index", columns=["geometry"]) bbox_df = gpd.GeoDataFrame(bbox_df) pretty_city_names = { city: config_cities[city]["name"] for city in config_cities.keys() } client = ohsome.OhsomeClient() res = client.elements.count.groupBy.boundary.post(bpolys=bbox_df, keys=keys, values=values, time=timeperiod, types=types) del client parks_unstacked = res.as_dataframe().unstack(level=0) parks_unstacked.columns = parks_unstacked.columns.droplevel(level=0) parks_unstacked.rename(columns=pretty_city_names, inplace=True) parks_unstacked.columns.name = "Cities" return parks_unstacked
def custom_client_without_log(tmpdir_factory): """Session-wide test client.""" ohsome_url = os.getenv("OHSOME_URL", "localhost") ohsome_port = os.getenv("OHSOME_PORT", "8080") client = ohsome.OhsomeClient( base_api_url=f"http://{ohsome_url}:{ohsome_port}/", log=False) try: client.metadata except OhsomeException as err: logger.warning( f"Local/custom ohsome-api endpoint not found. Using the public one as fallback: " f"https://api.ohsome.org/v1/. It is highly recommended to use a local endpoint for testing and development." f" The error while searching for a local one: {err}") client = ohsome.OhsomeClient(base_api_url="https://api.ohsome.org/v1/", log=False) yield client
def test_invalid_endpoint(): """ Test whether request can be sent to alternative url :return: """ base_api_url = "https://api.ohsme.org/v0.9/" bboxes = "8.7137,49.4096,8.717,49.4119" time = "2018-01-01" fltr = "name=Krautturm and type:way" client = ohsome.OhsomeClient(base_api_url=base_api_url, log=False) with pytest.raises(AttributeError): client.elements.cout.post(bboxes=bboxes, time=time, filter=fltr)
def custom_client_with_wrong_url(tmpdir_factory): """Session-wide test client.""" temp_directory = tmpdir_factory.mktemp("custom_client").mkdir( "logs").strpath ohsome_url = os.getenv("OHSOME_URL", "imwrong") ohsome_port = os.getenv("OHSOME_PORT", "8080") client = ohsome.OhsomeClient( base_api_url=f"http://{ohsome_url}:{ohsome_port}/", log_dir=temp_directory, ) yield client
def test_elements_count_exception(): """ Tests whether a TypeError is raised if the result cannot be converted to a geodataframe object :return: """ # GIVEN bboxes = "8.67066,49.41423,8.68177,49.4204" time = "2010-01-01/2011-01-01/P1Y" keys = ["building"] values = [""] # WHEN client = ohsome.OhsomeClient() response = client.elements.count.post(bboxes=bboxes, time=time, keys=keys, values=values) response.as_geodataframe()
def test_elemets_count_ratio(): """ Tests count ratio :return: """ bboxes = "8.67066,49.41423,8.68177,49.4204" time = "2010-01-01" keys = ["building"] keys2 = ["addr:city"] values = [""] values2 = [""] expected = 365.0 client = ohsome.OhsomeClient() response = client.elements.count.ratio.post(bboxes=bboxes, time=time, keys=keys, keys2=keys2, values=values, values2=values2)
def test_invalid_url(): """ Test whether request can be sent to alternative url :return: """ base_api_url = "https://api.ohsme.org/v0.9/" bboxes = "8.7137,49.4096,8.717,49.4119" time = "2018-01-01" fltr = "name=Krautturm and type:way" client = ohsome.OhsomeClient(base_api_url=base_api_url, log=False) with pytest.raises(ohsome.OhsomeException) as e_info: client.elements.count.post(bboxes=bboxes, time=time, filter=fltr) assert ( "Connection Error: Query could not be sent. Make sure there are no network problems and " f"that the ohsome API URL {base_api_url}elements/count is valid." in e_info.value.message)
def test_elements_geometry(): """ Tests whether the result of an elements/geometry query can be converted to a geodataframe :return: """ # GIVEN bboxes = "8.67066,49.41423,8.68177,49.4204" time = "2010-01-01" keys = ["landuse"] values = ["grass"] # WHEN client = ohsome.OhsomeClient() response = client.elements.geometry.post(bboxes=bboxes, time=time, keys=keys, values=values) result = response.as_geodataframe() del client # THEN assert len(result.geometry) == 9
def test_format_coordinates(): """ Asserts that coordinates of a MultiPolygon are concerted correctly :return: """ # GIVEN bpolys = geojson.FeatureCollection([{"type": "Feature", "geometry": {"coordinates": [[[[13,51], [13,51.1], [13.1,51.1], [13.1,51], [13,51]], [[13,51], [14,51.1], [14.1,51.1], [14.1,51], [14,51]]]], "type": "MultiPolygon"}}]) time = "2018-01-01" keys = ["landuse"] values = ["grass"] # WHEN client = ohsome.OhsomeClient() response = client.elements.geometry.post(bpolys=ohsome.format_coordinates(bpolys), time=time, keys=keys, values=values) result = response.as_geodataframe() del client # THEN assert len(result.geometry) == 74
def test_elements_count(): """ Tests counting elements within a bounding box for two timestamps :return: """ # GIVEN bboxes = [8.67066,49.41423,8.68177,49.4204] time = "2010-01-01/2011-01-01/P1Y" keys = ["building"] values = [""] timestamps = ["2010-01-01T00:00:00Z", "2011-01-01T00:00:00Z"] counts = [53.0, 256.0] expected = pd.DataFrame({"timestamp": timestamps, "value": counts}) # WHEN client = ohsome.OhsomeClient() response = client.elements.count.post(bboxes=bboxes, time=time, keys=keys, values=values) result = response.as_dataframe() del client # THEN assert expected.equals(result)
def test_elements_count_group_by_key(): """ Tests counting elements within a bounding box and grouping them by keys :return: """ #GIVEN bboxes = "8.67066,49.41423,8.68177,49.4204" time = "2010-01-01/2011-01-01/P1Y" groupByKeys = ["building"] timestamps = ["2010-01-01T00:00:00Z", "2011-01-01T00:00:00Z", "2010-01-01T00:00:00Z", "2011-01-01T00:00:00Z"] counts = [482.0, 628.0, 53.0, 256.0] keys = ["remainder", "remainder", "building", "building"] expected = pd.DataFrame({"key": keys, "timestamp": timestamps, "value": counts}) expected.set_index(["key", "timestamp"], inplace=True) # WHEN client = ohsome.OhsomeClient() response = client.elements.count.groupBy.key.post(bboxes=bboxes, groupByKeys=groupByKeys, time=time) results = response.as_dataframe() # THEN assert expected.equals(results)
def ohsome_source_tag(bpolys, start_date, end_date): """ Query the source tag :param bpolys: :param start_date: :param end_date: :return: """ client = ohsome.OhsomeClient() res = client.elements.count.groupBy.tag.post(bpolys=bpolys, keys=["leisure"], values=["park"], groupByKey="source", time="{0},{1}".format( start_date, end_date), types=["WAY", "RELATIONS"]) source_tags = res.as_dataframe().reset_index(1) source_tags = source_tags.pivot(columns="timestamp", values="value") source_tags["difference"] = source_tags[end_date] - source_tags[start_date] source_tags.sort_values("difference", ascending=False, inplace=True) return source_tags
if b[1] < miny: miny = b[1] if b[2] > maxx: maxx = b[2] if b[3] > maxy: maxy = b[3] print((minx, miny, maxx, maxy)) #limit to x countries for testing #countries=countries.head(2) n = 1 #chunk row size list_df = [countries[i:i + n] for i in range(0, countries.shape[0], n)] client = ohsome.OhsomeClient() df = pandas.DataFrame(columns=['date', 'value', 'country', 'population']) errorCountries = list() for row in tqdm(list_df): try: response = client.contributions.count.post(bpolys=row, time="//P1M", filter="type:node") response_df = response.as_dataframe() response_df = response_df.reset_index() response_df.rename(columns={'toTimestamp': 'date'}, inplace=True) response_df = response_df.drop('fromTimestamp', axis=1) response_df['country'] = row.iloc[0].SOVEREIGNT response_df['population'] = row.iloc[0].POP_EST