forked from district10/geobuf-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update manylinux * update * update headers * init cropping * not ready * bbox filtering * crop geojson * nice * update headers * add crop * not ready * export bbox * update version --------- Co-authored-by: TANG ZHIXIONG <[email protected]>
- Loading branch information
1 parent
ed0fd3e
commit 9205a44
Showing
9 changed files
with
466 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule headers
updated
75 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import os | ||
from typing import List, Optional, Tuple, Union # noqa | ||
|
||
import numpy as np | ||
from _pybind11_geobuf import geojson, tf | ||
from loguru import logger | ||
|
||
|
||
def bbox2polygon(bbox: np.ndarray): | ||
lon0, lat0, lon1, lat1 = bbox | ||
return np.array( | ||
[ | ||
[lon0, lat0, 0.0], | ||
[lon1, lat0, 0.0], | ||
[lon1, lat1, 0.0], | ||
[lon0, lat1, 0.0], | ||
[lon0, lat0, 0.0], | ||
] | ||
) | ||
|
||
|
||
def crop_by_feature_id( | ||
input_path: str, | ||
output_path: str, | ||
*, | ||
feature_id: str, | ||
buffer: Union[float, Tuple[float, float]] = 100.0, | ||
clipping_mode: str = "longest", | ||
max_z_offset: float = None, | ||
) -> bool: | ||
if not feature_id: | ||
logger.info( | ||
f"invalid feature id: {feature_id} (type: {type(feature_id)})" | ||
) # noqa | ||
return False | ||
g = geojson.GeoJSON().load(input_path) | ||
if not g.is_feature_collection(): | ||
logger.warning(f"{input_path} is not valid GeoJSON FeatureCollection") | ||
return False | ||
|
||
fc = g.as_feature_collection() | ||
bbox = None | ||
height = None | ||
for f in fc: | ||
props = f.properties() | ||
if "id" not in props: | ||
continue | ||
fid = props["id"]() | ||
if fid == feature_id: | ||
bbox = f.bbox() | ||
if max_z_offset is not None: | ||
height = f.bbox(with_z=True)[2::3].mean() | ||
if bbox is None: | ||
logger.error(f"not any feature matched by id: {feature_id}") | ||
return False | ||
|
||
dlon, dlat = 1.0 / tf.cheap_ruler_k(bbox[1::2].mean())[:2] | ||
if isinstance(buffer, (int, float, np.generic)): | ||
dlon *= buffer | ||
dlat *= buffer | ||
else: | ||
dlon *= buffer[0] | ||
dlat *= buffer[1] | ||
bbox += [-dlon, -dlat, dlon, dlat] | ||
logger.info(f"bbox: {bbox}") | ||
|
||
polygon = bbox2polygon(bbox) | ||
if height is not None: | ||
polygon[:, 2] = height | ||
logger.info(f"polygon:\n{polygon}") | ||
|
||
logger.info(f"writing to {output_path} ...") | ||
os.makedirs(os.path.dirname(os.path.abspath(output_path)), exist_ok=True) | ||
cropped = g.crop( | ||
polygon, | ||
clipping_mode=clipping_mode, | ||
max_z_offset=max_z_offset, | ||
) | ||
return cropped.to_rapidjson().sort_keys().dump(output_path, indent=True) | ||
|
||
|
||
def crop_by_grid( | ||
input_path: str, | ||
output_dir: str, | ||
*, | ||
anchor_lla: Union[str, List[float]] = None, | ||
grid_size: Union[float, Tuple[float, float]] = 1000.0, | ||
): | ||
os.makedirs(os.path.abspath(output_dir), exist_ok=True) | ||
|
||
|
||
def crop_by_center( | ||
input_path: str, | ||
output_dir: str, | ||
*, | ||
anchor_lla: Union[str, List[float]] = None, | ||
size: Union[float, Tuple[float, float]] = 1000.0, | ||
): | ||
os.makedirs(os.path.abspath(output_dir), exist_ok=True) | ||
|
||
|
||
def crop_by_bbox( | ||
input_path: str, | ||
output_path: str, | ||
*, | ||
bbox: Union[str, List[float]], | ||
z_center: float = None, | ||
z_max_offset: float = None, | ||
): | ||
logger.info(f"wrote to {output_path}") | ||
|
||
|
||
def crop_by_polygon( | ||
input_path: str, | ||
output_path: str, | ||
*, | ||
polygon: Union[str, np.ndarray], | ||
z_max_offset: float = None, | ||
): | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
import fire | ||
|
||
fire.core.Display = lambda lines, out: print(*lines, file=out) | ||
fire.Fire( | ||
{ | ||
"by_feature_id": crop_by_feature_id, | ||
"by_grid": crop_by_grid, | ||
"by_center": crop_by_center, | ||
"by_bbox": crop_by_bbox, | ||
"by_polygon": crop_by_polygon, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,7 +127,7 @@ def build_extension(self, ext): | |
# logic and declaration, and simpler if you include description/version in a file. | ||
setup( | ||
name="pybind11_geobuf", | ||
version="0.1.5", | ||
version="0.1.6", | ||
author="tzx", | ||
author_email="[email protected]", | ||
url="https://geobuf-cpp.readthedocs.io", | ||
|
Oops, something went wrong.