Skip to content

Commit

Permalink
Merge branch 'geojson_changes'
Browse files Browse the repository at this point in the history
  • Loading branch information
omanges committed Sep 10, 2020
2 parents 348da83 + 3b14729 commit 362f62c
Show file tree
Hide file tree
Showing 10 changed files with 433 additions and 245 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions changes/features/pr.68.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`description` is optional when creating the space.
1 change: 1 addition & 0 deletions changes/features/pr.71.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added feature to upload data from `Geopandas Dataframe` file to the space.
1 change: 1 addition & 0 deletions changes/features/pr.79.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improvement in the performance of `add_features_geojson`
50 changes: 43 additions & 7 deletions docs/notebooks/spaces_class_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"outputs": [],
"source": [
"# Make necessary import\n",
"\n",
"import os\n",
"import warnings\n",
"from xyzspaces.datasets import get_countries_data\n",
Expand All @@ -30,6 +31,7 @@
"outputs": [],
"source": [
"# Make a XYZ object\n",
"\n",
"try:\n",
" xyz_token = os.environ[\"XYZ_TOKEN\"]\n",
"except KeyError:\n",
Expand Down Expand Up @@ -60,18 +62,39 @@
"outputs": [],
"source": [
"# Create a new space\n",
"\n",
"title = \"Testing xyzspaces\"\n",
"description = \"Temporary space containing countries data.\"\n",
"\n",
"space = xyz.spaces.new(title=title, description=description)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"space.info"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"space.isshared()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Read from a existing space id\n",
"\n",
"space = xyz.spaces.from_id(space.info[\"id\"])"
]
},
Expand All @@ -81,9 +104,11 @@
"metadata": {},
"outputs": [],
"source": [
"# Add some data to it\n",
"# Countries Polygons/Multipolygons\n",
"\n",
"gj_countries = get_countries_data()\n",
"space.add_features(features=gj_countries)"
"\n",
"len(gj_countries[\"features\"])"
]
},
{
Expand All @@ -92,7 +117,17 @@
"metadata": {},
"outputs": [],
"source": [
"# Get statistics\n",
"_ = space.add_features(features=gj_countries)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get Statistics of a Space.\n",
"\n",
"space.get_statistics()"
]
},
Expand Down Expand Up @@ -135,7 +170,7 @@
"outputs": [],
"source": [
"# Add a new feature in space\n",
"space.add_feature(feature_id='test1', data=test)"
"_ = space.add_feature(feature_id='test1', data=test['features'][0])"
]
},
{
Expand All @@ -155,7 +190,7 @@
"outputs": [],
"source": [
"# Update a feature in space\n",
"space.update_feature(feature_id='test1', data=test)"
"_ = space.update_feature(feature_id='test1', data=test['features'][1])"
]
},
{
Expand All @@ -175,7 +210,7 @@
"outputs": [],
"source": [
"# Add new features in space\n",
"space.add_features(features=test)"
"_ = space.add_features(features=test)"
]
},
{
Expand Down Expand Up @@ -259,6 +294,7 @@
"outputs": [],
"source": [
"# Get features which interesect the provided geometry.\n",
"\n",
"geom = {\"type\": \"Point\", \"coordinates\": [72.8557, 19.1526]}\n",
"for feature in space.spatial_search_geometry(data=geom):\n",
" print(feature)"
Expand Down Expand Up @@ -322,7 +358,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
"version": "3.8.3"
}
},
"nbformat": 4,
Expand Down
38 changes: 10 additions & 28 deletions docs/notebooks/xyz_pro_features_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"space_id1 = space1.info[\"id\"]\n",
"\n",
"title2 = \"Testing xyzspaces\"\n",
"description2 = \"Temporary space containing chicago parks data.\"\n",
"description2 = \"Temporary space containing Chicago parks data.\"\n",
"space2 = xyz.spaces.new(title=title2, description=description2)\n",
"\n",
"# Add some data to space2\n",
Expand Down Expand Up @@ -119,7 +119,7 @@
"source": [
"# Create a new virtual space by grouping two spaces created above.\n",
"\n",
"title = \"Virtual Space for coutries and chicago parks data\"\n",
"title = \"Virtual Space for coutries and Chicago parks data\"\n",
"description = \"Test group functionality of virtual space\"\n",
"\n",
"upstream_spaces = [space_id1, space_id2]\n",
Expand Down Expand Up @@ -192,7 +192,7 @@
"# create two spaces with duplicate data \n",
"\n",
"title1 = \"Testing xyzspaces\"\n",
"description1 = \"Temporary space containing countries data.\"\n",
"description1 = \"Temporary space containing Chicago parks data.\"\n",
"space1 = xyz.spaces.new(title=title1, description=description1)\n",
"\n",
"with open('./data/chicago_parks.geo.json', encoding=\"utf-8-sig\") as json_file:\n",
Expand All @@ -203,7 +203,7 @@
"space_id1 = space1.info[\"id\"]\n",
"\n",
"title2 = \"Testing xyzspaces duplicate\"\n",
"description2 = \"Temporary space containing countries data duplicate\"\n",
"description2 = \"Temporary space containing Chicago parks data duplicate\"\n",
"space2 = xyz.spaces.new(title=title1, description=description1)\n",
"\n",
"# Add some data to it space2\n",
Expand All @@ -230,7 +230,7 @@
"source": [
"# Create a new virtual space by merging two spaces created above.\n",
"\n",
"title = \"Virtual Space for coutries and chicago parks data\"\n",
"title = \"Virtual Space for coutries and Chicago parks data\"\n",
"description = \"Test merge functionality of virtual space\"\n",
"\n",
"upstream_spaces = [space_id1, space_id2]\n",
Expand Down Expand Up @@ -299,7 +299,7 @@
"# create two spaces with duplicate data \n",
"\n",
"title1 = \"Testing xyzspaces\"\n",
"description1 = \"Temporary space containing countries data.\"\n",
"description1 = \"Temporary space containing Chicago parks data.\"\n",
"space1 = xyz.spaces.new(title=title1, description=description1)\n",
"\n",
"with open('./data/chicago_parks.geo.json', encoding=\"utf-8-sig\") as json_file:\n",
Expand All @@ -310,7 +310,7 @@
"space_id1 = space1.info[\"id\"]\n",
"\n",
"title2 = \"Testing xyzspaces duplicate\"\n",
"description2 = \"Temporary space containing countries data duplicate\"\n",
"description2 = \"Temporary space containing Chicago parks data duplicate\"\n",
"space2 = xyz.spaces.new(title=title1, description=description1)\n",
"\n",
"# Add some data to it space2\n",
Expand All @@ -326,7 +326,7 @@
"source": [
"# Create a new virtual space by override operation.\n",
"\n",
"title = \"Virtual Space for coutries and chicago parks data\"\n",
"title = \"Virtual Space for coutries and Chicago parks data\"\n",
"description = \"Test merge functionality of virtual space\"\n",
"\n",
"upstream_spaces = [space_id1, space_id2]\n",
Expand Down Expand Up @@ -367,24 +367,6 @@
"vspace.delete()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='custom_cell'></a>\n",
"#### Custom"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# TODO: Add custom example\n",
"# Custom operation needs special permissions via CMEKB JIRA, add example once permissions are granted."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -448,7 +430,7 @@
"source": [
"# Create a new space\n",
"title = \"Testing xyzspaces\"\n",
"description = \"Temporary space containing chicago parks data.\"\n",
"description = \"Temporary space containing Chicago parks data.\"\n",
"space = xyz.spaces.new(title=title, description=description)"
]
},
Expand Down Expand Up @@ -570,7 +552,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
"version": "3.8.3"
}
},
"nbformat": 4,
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ requests
geopandas
turfpy>=0.0.3
geobuf
ijson==3.1.1
65 changes: 48 additions & 17 deletions tests/space/test_space_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pathlib import Path
from time import sleep

import geopandas as gpd
import pytest
from geojson import GeoJSON

Expand Down Expand Up @@ -109,9 +110,10 @@ def test_space_search(space_object):
assert feats[0]["type"] == "Feature"
assert len(feats) > 0

feats = list(space_object.search(limit=10))
assert feats[0]["type"] == "Feature"
assert len(feats) <= 10
feats = space_object.search(limit=10, geo_dataframe=True)
gdf = next(feats)
assert isinstance(gdf, gpd.GeoDataFrame)
assert gdf["name"][0] == "Afghanistan"

feats = list(space_object.search(tags=["non-existing"]))
assert len(feats) == 0
Expand Down Expand Up @@ -167,6 +169,10 @@ def test_space_feature_operations(space_object):
@pytest.mark.skipif(not XYZ_TOKEN, reason="No token found.")
def test_space_features_operations(space_object):
"""Test for get, add, update and delete features operations."""
gdf = space_object.get_features(
feature_ids=["DEU", "ITA"], geo_dataframe=True
)
assert isinstance(gdf, gpd.GeoDataFrame)
# get two features
data = space_object.get_features(feature_ids=["DEU", "ITA"])
assert isinstance(data, GeoJSON)
Expand All @@ -190,9 +196,10 @@ def test_space_features_search_operations(space_object):
assert len(bbox) == 15
assert bbox[0]["type"] == "Feature"

tile = list(space_object.features_in_tile(tile_type="here", tile_id="12"))
assert len(tile) == 97
assert tile[0]["type"] == "Feature"
gdf = next(
space_object.features_in_bbox(bbox=[0, 0, 20, 20], geo_dataframe=True)
)
assert gdf.shape == (15, 3)

spatial_search = list(
space_object.spatial_search(
Expand All @@ -202,32 +209,47 @@ def test_space_features_search_operations(space_object):
assert spatial_search[0]["type"] == "Feature"
assert spatial_search[0]["id"] == "AFG"

ss_gdf = next(
space_object.spatial_search(
lat=37.377228699000057, lon=74.512691691000043, geo_dataframe=True
)
)
assert ss_gdf.shape == (1, 3)

data1 = {"type": "Point", "coordinates": [72.8557, 19.1526]}
spatial_search_geom = list(
space_object.spatial_search_geometry(data=data1)
)
assert spatial_search_geom[0]["type"] == "Feature"
assert spatial_search_geom[0]["id"] == "IND"
ss_gdf = next(
space_object.spatial_search_geometry(data=data1, geo_dataframe=True)
)
assert ss_gdf.shape == (1, 3)
with pytest.raises(ValueError):
list(space_object.features_in_tile(tile_type="dummy", tile_id="12"))
res = space_object.features_in_tile(
tile_type="here", tile_id="12", limit=10, geo_dataframe=True
)
gdf = next(res)
assert gdf.shape == (10, 3)


@pytest.mark.skipif(not XYZ_TOKEN, reason="No token found.")
def test_space_add_features_from_files_without_altitude(
space_object, tmp_path
):
def test_space_add_features_from_files_without_altitude(empty_space, tmp_path):
"""Test for adding features using csv and geojson."""
fp_csv = Path(__file__).parents[1] / "data" / "test.csv"
space_object.add_features_csv(
fp_csv, lat_col="latitude", lon_col="longitude", id_col="policyID"
space = empty_space
space.add_features_csv(
fp_csv, lon_col="longitude", lat_col="latitude", id_col="policyID"
)
feature = space_object.get_feature(feature_id="333743")
feature = space.get_feature(feature_id="333743")
assert feature["type"] == "Feature"

fp_geojson = Path(__file__).parents[1] / "data" / "test.geojson"
space_object.add_features_geojson(fp_geojson)
space.add_features_geojson(fp_geojson)

feature = space_object.get_feature(feature_id="test_geojson_1")
feature = space.get_feature(feature_id="test_geojson_1")
assert feature["type"] == "Feature"
geo_data_dict = {
"type": "Feature",
Expand All @@ -239,8 +261,8 @@ def test_space_add_features_from_files_without_altitude(
temp_file = Path(tmp_path) / "temp.geojson"
with open(temp_file, "w") as f:
f.write(geo_data)
space_object.add_features_geojson(temp_file)
feature = space_object.get_feature(feature_id="test_id")
space.add_features_geojson(temp_file)
feature = space.get_feature(feature_id="test_id")
assert feature["type"] == "Feature"


Expand Down Expand Up @@ -468,7 +490,7 @@ def test_add_features_csv_exception(space_object, tmp_path):
f.write(csv_data)
with pytest.raises(Exception):
space_object.add_features_csv(
temp_file, lon_col="", lat_col="", id_col=""
temp_file, lon_col="dummy_a", lat_col="dummy", id_col=""
)


Expand Down Expand Up @@ -690,3 +712,12 @@ def test_add_features_duplicate(empty_space):
empty_space.add_features(geojson, features_size=100)
stats = empty_space.get_statistics()
assert stats["count"]["value"] == 180


@pytest.mark.skipif(not XYZ_TOKEN, reason="No token found.")
def test_add_features_geopandas(empty_space):
geojson_file = Path(__file__).parents[1] / "data" / "countries.geo.json"
df = gpd.read_file(geojson_file)
empty_space.add_features_geopandas(data=df)
stats = empty_space.get_statistics()
assert stats["count"]["value"] == 292

0 comments on commit 362f62c

Please sign in to comment.