Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Mar 7, 2024
1 parent 4becd52 commit 1ad2e49
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
23 changes: 11 additions & 12 deletions osm_fieldwork/basemapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import re
import sys
import threading
from io import BytesIO
from pathlib import Path
from typing import Union
from io import BytesIO

import geojson
import mercantile
Expand Down Expand Up @@ -127,15 +127,15 @@ class BaseMapper(object):

def __init__(
self,
boundary: Union[str, BytesIO], # Updated type hint to accept BytesIO
boundary: Union[str, BytesIO], # Updated type hint to accept BytesIO
base: str,
source: str,
xy: bool,
):
"""Create an tile basemap for ODK Collect.
Args:
boundary (Union[str, BytesIO]): The boundary for the area you want.
boundary (Union[str, BytesIO]): The boundary for the area you want.
Can be a BBOX string, or GeoJSON file of the AOI or BytesIO object containing GeoJSON data.
The GeoJSON can contain multiple geometries.
base (str): The base directory to cache map tile in
Expand Down Expand Up @@ -275,7 +275,7 @@ def tileExists(

def makeBbox(
self,
boundary: Union[str, BytesIO],
boundary: Union[str, BytesIO],
) -> tuple[float, float, float, float]:
"""Make a bounding box from a shapely geometry.
Expand Down Expand Up @@ -321,19 +321,18 @@ def makeBbox(
# Process BytesIO object
try:
boundary.seek(0)
geojson_data = boundary.read().decode('utf-8')
geojson_data = boundary.read().decode("utf-8")
poly = geojson.loads(geojson_data)
return self.extract_bbox(poly)
except Exception as e:
log.error(e)
raise ValueError("Failed to decode GeoJSON data from BytesIO object") from e
else:
raise ValueError(f"Invalid boundary type: {type(boundary)}. It must be a BBOX string or (.json, .geojson) flie or BytesIO object")
raise ValueError(
f"Invalid boundary type: {type(boundary)}. It must be a BBOX string or (.json, .geojson) flie or BytesIO object"
)


def extract_bbox(
self, poly: Union[BaseGeometry, None]
) -> tuple[float, float, float, float]:
def extract_bbox(self, poly: Union[BaseGeometry, None]) -> tuple[float, float, float, float]:
"""Extract bounding box from GeoJSON polygon."""
if "features" in poly:
geometry = shape(poly["features"][0]["geometry"])
Expand All @@ -355,7 +354,7 @@ def extract_bbox(
bbox = geometry.bounds
# left, bottom, right, top
# minX, minY, maxX, maxY
return bbox
return bbox


def tileid_from_y_tile(filepath: Union[Path | str]):
Expand Down Expand Up @@ -448,7 +447,7 @@ def create_basemap_file(
Args:
verbose (bool, optional): Enable verbose output if True.
boundary (Union[str, BytesIO], optional): The boundary for the area you want.
boundary (Union[str, BytesIO], optional): The boundary for the area you want.
tms (str, optional): Custom TMS URL.
xy (bool, optional): Swap the X & Y coordinates when using a
custom TMS if True.
Expand Down
5 changes: 3 additions & 2 deletions outreachy.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from io import BytesIO

from osm_fieldwork.basemapper import create_basemap_file

with open("D:\\customers\\osm-fieldwork\\tests\\testdata\\Rollinsville.geojson", "rb") as f:
boundary = f.read() # Read the file into memory
boundary_bytesio = BytesIO(boundary) # Convert the file into a BytesIO object
boundary = f.read() # Read the file into memory
boundary_bytesio = BytesIO(boundary) # Convert the file into a BytesIO object


create_basemap_file(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_basemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ def test_create_with_byteio():
os.remove(outfile)
shutil.rmtree(base)

assert hits == 2
assert hits == 2


if __name__ == "__main__":
test_create()
Expand Down

0 comments on commit 1ad2e49

Please sign in to comment.