Skip to content

Commit

Permalink
Merge pull request #1 from mathieuripert/0.2
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
mathieuripert authored Oct 11, 2019
2 parents 4af001d + 52f8053 commit d77c82f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: python
python:
- "2.7"
- "3.5"
# command to install dependencies
install: "pip install -r requirements.txt"
# command to run tests
script: python -m pytest
11 changes: 8 additions & 3 deletions geoh/geoh.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ def _multi_polygon_to_polygons(geom):

def _polygon_from_geojson(geojson={}):
if geojson.get("type", None) == "FeatureCollection":
df = pd.DataFrame(geojson.get("features", []))
df["polygon"] = df.geometry.map(lambda x: shape(x))
return MultiPolygon(_flatten(map(_multi_polygon_to_polygons, df["polygon"].values)))
features = [f for f in geojson.get("features", []) if f is not None]
df = pd.DataFrame(features, columns=["geometry"])
df["polygon"] = df[pd.notnull(df.geometry)].geometry.map(lambda x: shape(x))
df = df[pd.notnull(df.polygon)]
if len(df.index):
return MultiPolygon(_flatten(map(_multi_polygon_to_polygons, df["polygon"].values)))
else:
return None
elif geojson.get("type", None) == "Feature":
return shape(geojson.get("geometry", None))
return None
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pandas
geopandas
shapely
python-geohash
pytest
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="geoh",
version="0.1",
version="0.2",
author="Mathieu Ripert",
author_email="mathieu@instacart.com",
url="https://github.com/mathieuripert/geoh",
Expand Down
Empty file added test/__init__.py
Empty file.
File renamed without changes.
45 changes: 45 additions & 0 deletions test/test_zero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
import json
import pytest
import geoh

@pytest.fixture
def geojson_sf():
__location__ = os.path.realpath(os.path.join(
os.getcwd(), os.path.dirname(__file__)))
geojson = json.loads(
open(os.path.join(__location__, './geojson-sf.json')).read())
return geojson

@pytest.fixture
def geojson_none():
return {
'type': 'FeatureCollection',
'features': [None]
}

@pytest.fixture
def malformed_geojson():
return {
'type': 'FeatureCollection',
'features': [{
'geometry': None,
'type': 'Polygon',
}]
}

@pytest.mark.parametrize("precision", [1, 2 ,3, 4, 5, 6])
def test_sf(geojson_sf, precision):
geohashes = geoh.geohashes(geojson=geojson_sf, precision=precision)
assert(len(geohashes) > 0)

@pytest.mark.parametrize("precision", [1, 2, 3, 4, 5, 6])
def test_with_none_geojson(geojson_none, precision):
geohashes = geoh.geohashes(geojson=geojson_none, precision=precision)
assert(geohashes == [])


@pytest.mark.parametrize("precision", [1, 2, 3, 4, 5, 6])
def test_with_malformed_geojson(malformed_geojson, precision):
geohashes = geoh.geohashes(geojson=malformed_geojson, precision=precision)
assert(geohashes == [])

0 comments on commit d77c82f

Please sign in to comment.