Skip to content

Commit

Permalink
Merge branch 'omanges-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
omanges committed Aug 14, 2020
2 parents cc0b324 + 41e4bf0 commit 9717585
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions tests/data/test.wkt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOMETRYCOLLECTION (LINESTRING (30 10, 10 30, 40 40), MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10)), MULTIPOLYGON (((30 20, 10 40, 45 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5))), MULTIPOINT ((10 40), (40 30), (20 20), (30 10)), POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10)), POLYGON ((35 10, 10 20, 15 40, 45 45, 35 10),(20 30, 35 35, 30 20, 20 30)))
12 changes: 12 additions & 0 deletions tests/space/test_space_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,3 +577,15 @@ def test_add_features_shapefile(empty_space):
resp = space.search(params={"p.NAME": "Mumbai"})
flist = list(resp)
assert flist[0]["properties"]["NAME"] == "Mumbai"


@pytest.mark.skipif(not XYZ_TOKEN, reason="No token found.")
def test_add_features_wktfile(empty_space):
"""Test uploading wkt data"""
space = empty_space
wkt_file = Path(__file__).parents[1] / "data" / "test.wkt"
space.add_features_wkt(path=wkt_file)
features = []
for f in space.iter_feature():
features.append(f)
assert len(features) == 6
16 changes: 15 additions & 1 deletion xyzspaces/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from geojson import Feature, GeoJSON

from .apis import HubApi
from .utils import grouper
from .utils import grouper, wkt_to_geojson

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -836,3 +836,17 @@ def add_features_shapefile(
self.add_features_geojson(
path=temp.name, features_size=features_size, chunk_size=chunk_size
)

def add_features_wkt(self, path: str):
"""
To upload data from wkt file to a space
:param path: Path to wkt file
"""
with open(path) as f:
wkt_data = f.read()
geojson_data = wkt_to_geojson(wkt_data)
if geojson_data["type"] == "FeatureCollection":
self.add_features(features=geojson_data)
else:
self.add_feature(data=geojson_data)
24 changes: 24 additions & 0 deletions xyzspaces/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
from itertools import zip_longest
from typing import List

import geojson
from shapely import geometry, wkt


def join_string_lists(**kwargs) -> dict:
"""Convert named lists of strings to one dict with comma-separated strings.
Expand Down Expand Up @@ -99,3 +102,24 @@ def grouper(size, iterable, fillvalue=None):
"""
args = [iter(iterable)] * size
return zip_longest(fillvalue=fillvalue, *args)


def wkt_to_geojson(wkt_data: str) -> dict:
"""
Converts wkt to geojson
:param wkt_data: wkt data to be converted
:return: Geojson
"""
parsed_wkt = wkt.loads(wkt_data)

geo = geometry.mapping(parsed_wkt)

if geo["type"] == "GeometryCollection":
feature_collection = []
for g in geo["geometries"]:
feature = geojson.Feature(geometry=g)
feature_collection.append(feature)
return geojson.FeatureCollection(feature_collection)
else:
return geojson.Feature(geometry=geo)

0 comments on commit 9717585

Please sign in to comment.