Skip to main content

High-performance building footprint extractor from OBJ to GPKG/SHP

Project description

alobj2shp

import alobj2shp

alobj2shp.extract_footprints(
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_YUp",
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_YUp_v2.gpkg",
    y_is_up=True
)

功能概述

alobj2shp 用于批量从 OBJ 中提取建筑底面轮廓(footprints),并把结果直接写出为 GIS 矢量文件(推荐 .gpkg)。

处理流程(实现层面的概念)大致为:

  1. 递归读取 input_dir 下所有 *.obj
  2. 将每个 OBJ 的三角网格进行投影/栅格化生成二维轮廓
  3. 使用 GEOS 做布尔运算、拓扑构建/简化
  4. 输出每个 OBJ 对应一条(Polygon 或 MultiPolygon)的 geometry,并写出到 output_file

安装与依赖(简要)

  • Python >= 3.11
  • 依赖(来自 pyproject.toml):ninja, numpy, moderngl, opencv-python-headless, geopandas, tqdm

Python API

主入口

alobj2shp.extract_footprints(...)

签名(类型声明):

extract_footprints(
    input_dir: str,
    output_file: str,
    gsd: float = 0.05,
    simplify_tolerance: float = 0.1,
    target_crs: str | None = "EPSG:3857",
    max_workers: int | None = None,
    y_is_up: bool = False,
    export_base_height: bool = False,
    export_model_height: bool = False,
    height_percentile: str = "p98",
) -> geopandas.GeoDataFrame

参数说明

  • input_dir:OBJ 模型目录。会对该目录进行递归查找 *.obj
  • output_file:输出路径。建议使用 .gpkg
    • 当后缀为 .gpkg 时,以 driver="GPKG" 写出
    • 其它后缀由 geopandas.GeoDataFrame.to_file 按后缀/默认规则写出
  • gsd:地面采样距离(单位与 OBJ 坐标一致)。
    • gsd 越小,栅格越细,几何精度通常更高,但会显著增加计算/显存开销
  • simplify_tolerance:简化容差(与坐标单位一致),内部对应 GEOS 的拓扑保持简化。
  • target_crs:结果写入/标注的 CRS。
    • 默认 "EPSG:3857"
    • target_crs=None 时,不会设置 CRS
  • max_workers:并发线程数(线程池)。
    • 默认 None:使用 os.cpu_count() or 4
    • 每个 OBJ 会在独立任务中解析/渲染/计算;线程过多可能引发 GPU/系统资源竞争,必要时可调小
  • y_is_up:输入 OBJ 是否为 Y-up 坐标系
    • y_is_up=False(默认):使用 x=val1, y=val2, z=val3
    • y_is_up=True:启用轴变换以适配 GIS 的 Z-up 规范,内部为 x=val1, z=val2, y=-val3
  • export_base_height:为 True 时写入 elevation 列(float,模型底部 Z,世界坐标)。默认 False 不创建该列。
  • export_model_height:为 True 时写入 height 列(float,建筑高度 = 顶部 Z − 底部 Z,世界坐标)。默认 False 不创建该列。
  • height_percentile:高度百分位过滤,与上两项联合使用。可选 p90 / p95 / p98 / p100,默认 p98
    • 仅当 export_base_heightexport_model_heightTrue 且取值不是 p100 时,按顶点 Z 分位裁剪后再计算属性(p90→10%~90%,p95→5%~95%,p98→2%~98%)。
    • 不改变 footprint 几何(栅格化仅使用 XY);默认不导出高度时行为与旧版一致。

返回值与输出字段

  • 正常情况下返回 geopandas.GeoDataFrame,包含列:
    • old_code:OBJ 文件相对于 input_dir 的相对路径(字符串)
    • geometry:由 WKB 解析得到的 shapely 几何(通常为 Polygon 或 MultiPolygon)
    • elevation(可选):export_base_height=True 时存在
    • height(可选):export_model_height=True 时存在
  • 当在 input_dir 中找不到 *.obj,或所有 OBJ 都处理失败时,运行时可能返回 None

坐标系与坐标平面约定

  • y_is_up 决定 OBJ 顶点在进入后续“投影/轮廓生成”之前的轴变换方式(见参数说明)。
  • gsdsimplify_tolerance 都依赖“OBJ 坐标单位”:
    • 如果你的 OBJ 单位不是米,建议相应缩放 gsd / simplify_tolerance,避免几何过度简化或细节丢失。

调试/高级用法(可选)

如果你想对单个 OBJ直接得到几何的 WKB,可以使用内部接口(注意它不是顶层公开 API):

from shapely import wkb
from alobj2shp._core import process_single_obj_to_wkb

obj_wkb, attrs = process_single_obj_to_wkb(
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_ZUp\East_Block_1\xxx.obj",
    gsd=0.05,
    tolerance=0.1,   # 对应 simplify_tolerance
    y_is_up=False,
    export_base_height=True,
    export_model_height=True,
    height_percentile="p98",
)

geom = wkb.loads(obj_wkb) if obj_wkb else None
print(geom, attrs)

调用样例

下面样例中,建议使用 r"..." 书写 Windows 路径,避免 Python 把反斜杠当作转义字符。

1. Z-Up(默认)

import alobj2shp

gdf = alobj2shp.extract_footprints(
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_ZUp\East_Block_1",
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_ZUp\East_Block_1_v2.gpkg"
)

print(gdf.shape)
print(gdf[["old_code"]].head())

2. Y-Up

import alobj2shp

gdf = alobj2shp.extract_footprints(
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000\East_Block_1",
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000\East_Block_1_v2.gpkg",
    y_is_up=True
)

print(gdf.shape)

3. 参数调优(更细栅格)

import alobj2shp

gdf = alobj2shp.extract_footprints(
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_ZUp\East_Block_1",
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_ZUp\East_Block_1_tuned.gpkg",
    gsd=0.02,
    simplify_tolerance=0.2,
    max_workers=8
)

4. 不写入 CRS

import alobj2shp

gdf = alobj2shp.extract_footprints(
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_ZUp\East_Block_1",
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_ZUp\East_Block_1_no_crs.shp",
    target_crs=None
)
print(gdf.crs)

5. 导出底部高程与建筑高度(含百分位过滤)

import alobj2shp

gdf = alobj2shp.extract_footprints(
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_ZUp\East_Block_1",
    r"H:\Work\PT\普陀东部\Data_OBJ_SH2000_ZUp\East_Block_1_heights.gpkg",
    export_base_height=True,
    export_model_height=True,
    height_percentile="p98",
)

print(gdf[["old_code", "elevation", "height"]].head())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

alobj2shp-0.2.0-cp314-cp314-win_amd64.whl (501.3 kB view details)

Uploaded CPython 3.14Windows x86-64

alobj2shp-0.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

alobj2shp-0.2.0-cp313-cp313-win_amd64.whl (487.2 kB view details)

Uploaded CPython 3.13Windows x86-64

alobj2shp-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

alobj2shp-0.2.0-cp312-cp312-win_amd64.whl (487.3 kB view details)

Uploaded CPython 3.12Windows x86-64

alobj2shp-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

alobj2shp-0.2.0-cp311-cp311-win_amd64.whl (487.8 kB view details)

Uploaded CPython 3.11Windows x86-64

alobj2shp-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file alobj2shp-0.2.0-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: alobj2shp-0.2.0-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 501.3 kB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for alobj2shp-0.2.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 a739255178c1f987bde36aecfb57fc17490a75fee210825559ba861a4427c2b9
MD5 9bd5b169b9b218f3a2f24a4084771c7f
BLAKE2b-256 e48f7338e11c82ba4b6875200b64415830c7edff3675d6d04e862142624cc791

See more details on using hashes here.

File details

Details for the file alobj2shp-0.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for alobj2shp-0.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 86d1136b183e08f371054c7ca3d09a157f6866fad21ef2c68cdc45bac20af410
MD5 037c3acc1aeb82b097cf475c5145cdcb
BLAKE2b-256 6cac89646f493be707e409f350b55da10cebebd4324e3045db3fdf725db543b9

See more details on using hashes here.

File details

Details for the file alobj2shp-0.2.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: alobj2shp-0.2.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 487.2 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for alobj2shp-0.2.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fe287dbf1450867477cd5bbffcba53921cc8fa80bbcb9bbe4e22743beb0ba573
MD5 4fad699d1718e4ea7deb1f63aa9f9f7c
BLAKE2b-256 0aa8f7c8d009c0767ca3dcc78f41a15d0c24e52742e2c91363dd5f0d6a619f8c

See more details on using hashes here.

File details

Details for the file alobj2shp-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for alobj2shp-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cbdf08cc43a31a69e2df4491580589295fadd36ba391f1c3a45a5c8930a08ed6
MD5 052a8734e9ef713f7d1bf9a8f84261d6
BLAKE2b-256 823f532f62075e4dee6fef50dffa39e72a1e3deec672496dc0e7fe1497f48453

See more details on using hashes here.

File details

Details for the file alobj2shp-0.2.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: alobj2shp-0.2.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 487.3 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for alobj2shp-0.2.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 a3c54770af8f893f01b7651a96e47f1bd031516374051d1e7e272bf9fc65b5b8
MD5 6061047f6b850a4973d9401ae234eb3a
BLAKE2b-256 7e11cb0949825d57b4e98d3dd4ca8aa1b34b042ea845cde57777e643ea8e7ac2

See more details on using hashes here.

File details

Details for the file alobj2shp-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for alobj2shp-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b8c9a58f4a762957b67c1a036c04224b0362fbfdcbc93f452870f9b9159cab88
MD5 c21a63fc0ac17a49e9b6d5d275ba1bc3
BLAKE2b-256 141458f5d2cb8c6ce37f5eec7bc1e780351934551ff59b0ff63d2cc17522e9e4

See more details on using hashes here.

File details

Details for the file alobj2shp-0.2.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: alobj2shp-0.2.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 487.8 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for alobj2shp-0.2.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 343d78fb094795b6f5c7688f4217f654b039aba724293127e226189a6c692054
MD5 655d365c12666445a0ac768ec6f4fe17
BLAKE2b-256 2cc01f7db56caa417da6fde387de9e0796ea2c0f9e5cd44860d0352d027128ec

See more details on using hashes here.

File details

Details for the file alobj2shp-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for alobj2shp-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 e8c4ada0cb54ab124131cdcde10bd5cea28aef2523c5ac971657b75d5bd69a84
MD5 92f9613ad06a1bd48d0a97433022d4e5
BLAKE2b-256 155c64c9eb08639e66bfcc711b4f9bd44bee10e2b391e6e09592649db65a6f52

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page