Skip to content

Instantly share code, notes, and snippets.

@IngIeoAndSpare
Last active March 28, 2023 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IngIeoAndSpare/eed2a2facc91af3b580192780a2d57f9 to your computer and use it in GitHub Desktop.
Save IngIeoAndSpare/eed2a2facc91af3b580192780a2d57f9 to your computer and use it in GitHub Desktop.
Code snippet to determine whether tiles are included, not included, or across a boundary in a specific polygon area
# start obj read
from shapely.geometry import Polygon, LineString
from Reader.ObjFileReader import ObjFileReader
import glob
import os
import numpy as np
import json
from pathlib import Path
# obj file root directory
root_path = {{OBJ_ROOT_DIR_PATH}}
obj_path_list = []
for path in glob.glob(f"{root_path}/**/*.obj", recursive=True) :
obj_path_list.append(path.replace("\\", "/"))
# target polygon coordinate set. ex => [(lat, lon), (lat, lon)...] or [(x,y), (x,y) ...]
obj_conv_point_list = []
standard_polygon = Polygon(obj_conv_point_list)
result = {}
result["out"] = []
result["in"] = []
result["border"] = []
for obj_path in obj_path_list:
obj_reader = ObjFileReader(True)
content = obj_reader.getObjFileContent(obj_path)
tile_bbox = get_bbox_polygon(content['vector'])
file_name = Path(obj_path).stem
if tile_bbox.within(standard_polygon):
result["in"].append(file_name)
elif tile_bbox.disjoint(standard_polygon) :
result["out"].append(file_name)
else :
result["border"].append(file_name)
print(f"{file_name} is done")
with open("tile_result.json", "w") as f:
# write dictionary to file in JSON format
json.dump(result, f, indent = 4)
## This reader is code written for the personal use of @IngIeoAnd0Spare.
## Unauthorized modification and use without permission is prohibited.
# sub moudules
def get_polygon_items (face_dict) :
result_f = {}
result_uv_idx = {}
face_name_list = []
for face_name in face_dict:
result_f[face_name] = []
result_uv_idx[face_name] = []
if not "untextured" in face_name :
face_name_list.append(face_name)
for face_info in face_dict[face_name]:
result_f[face_name].append(
[int(face_item.split('/')[0]) for face_key, face_item in face_info.getDict().items()] # face-v index -> using clustering
)
result_uv_idx[face_name].append(
[int(face_item.split('/')[1]) for face_key, face_item in face_info.getDict().items()] # atlas polygon uv index -> using atlas polygon draw
)
return result_f, result_uv_idx, face_name_list
def get_vector_list(vector_content, face_index_list) :
v = [vector_content[v_index - 1] for v_index in face_index_list]
return LineString([(item.xcoordinate, item.ycoordinate) for item in v])
def get_bbox_polygon(vector_content) :
x_point = []
y_point = []
for vector_item in vector_content :
x_point.append(vector_item.xcoordinate)
y_point.append(vector_item.ycoordinate)
x_point = np.array(x_point)
y_point = np.array(y_point)
x_min = np.min(x_point)
x_max = np.max(x_point)
y_min = np.min(y_point)
y_max = np.max(y_point)
min_point = (x_min, y_min)
max_point = (x_max, y_max)
y_min_point = (x_max, y_min)
x_min_point = (x_min, y_max)
return Polygon([min_point, y_min_point, max_point, x_min_point])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment