Skip to content

Commit

Permalink
ENH: add grid_size to feature api
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfleis committed Jun 15, 2023
1 parent 9b8a636 commit c41830f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.5.0 (2023-06-15)

Feature API can now query different resolutions of the data grid.

## 0.4.0 (2023-03-02)

Added Geocoding API covering Switzerland.
Expand Down
10 changes: 7 additions & 3 deletions udlai/feature_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def attribute_detail(token, attribute_id):
_propagate_error(response)


def features(token, latitude, longitude, attribute_id, index_by="id"):
def features(token, latitude, longitude, attribute_id, index_by="id", grid_size=25):
"""
An API Endpoint that will return the attributes for provided
coordinates. The API expects the attribute IDs, that can be fetched using
Expand All @@ -197,6 +197,10 @@ def features(token, latitude, longitude, attribute_id, index_by="id"):
index_by : {"id", "name"}
One of the ``{"id", "name"}`` denoting whether the output should be indexed
using the original attribute ID or its name
grid_size : {25, 75, 225, 675}
Resolution of the UDL grid to be queried. Smaller resolutions are more precise
but may contain gaps, larger resolutions are aggregated and are more likely to
cover entirety of built up area.
Returns
-------
Expand Down Expand Up @@ -261,6 +265,7 @@ def features(token, latitude, longitude, attribute_id, index_by="id"):
}
for x in attribute_id
],
"grid_size": f"grid{grid_size}",
}

# calling the API
Expand All @@ -273,7 +278,6 @@ def features(token, latitude, longitude, attribute_id, index_by="id"):
json=json_data,
)
if response.status_code == 200:

dict_raw = response.json()

if "error" in dict_raw:
Expand Down Expand Up @@ -307,6 +311,7 @@ def features(token, latitude, longitude, attribute_id, index_by="id"):
}
for x in attribute_id
],
"grid_size": f"grid{grid_size}",
}

response = requests.post(
Expand All @@ -319,7 +324,6 @@ def features(token, latitude, longitude, attribute_id, index_by="id"):
)

if response.status_code == 200:

d = defaultdict(list)
missing = False
for pt in response.json()["results"]:
Expand Down
7 changes: 7 additions & 0 deletions udlai/tests/test_feature_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ def test_multi_attr(self):
assert c in r.columns
assert r["net_betw_speed"].notna().sum() == 2

def test_grid_size(self):
r25 = udlai.features(token, 47.37, 8.54, [113, 172], index_by="name")
r675 = udlai.features(
token, 47.37, 8.54, [113, 172], index_by="name", grid_size=675
)
assert r25["net_betw_speed"] != r675["net_betw_speed"]


class TestAggregate:
def setup_method(self):
Expand Down

0 comments on commit c41830f

Please sign in to comment.