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
) -> 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

返回值与输出字段

  • 正常情况下返回 geopandas.GeoDataFrame,包含列:
    • old_code:OBJ 文件相对于 input_dir 的相对路径(字符串)
    • geometry:由 WKB 解析得到的 shapely 几何(通常为 Polygon 或 MultiPolygon)
  • 当在 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 = 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
)

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

调用样例

下面样例中,建议使用 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)

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.1.9-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.1.9-cp313-cp313-win_amd64.whl (474.5 kB view details)

Uploaded CPython 3.13Windows x86-64

alobj2shp-0.1.9-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.1.9-cp312-cp312-win_amd64.whl (474.6 kB view details)

Uploaded CPython 3.12Windows x86-64

alobj2shp-0.1.9-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.1.9-cp311-cp311-win_amd64.whl (474.8 kB view details)

Uploaded CPython 3.11Windows x86-64

alobj2shp-0.1.9-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.1.9-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for alobj2shp-0.1.9-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 19eac7d9bbe06ddacc7c61349b1947d594e086bb23556f25765afb29d67a8af4
MD5 855a2cf0d5f5f2d5db5ff4b9969aa5bb
BLAKE2b-256 32ab39480beb518cd40854954f0f275b47a80bfd418776a330be5707f2cb62cb

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for alobj2shp-0.1.9-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1d6e5de694de648c0e7237f992da2fcdeaf87e4cccd22f2cbee542e15376aacd
MD5 674107cc372a3b615d1a95a3af62ad79
BLAKE2b-256 ef3051b1f2fabc9509a222d465c304b8c54d11b585c8db66d7c15a93a7171ec3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for alobj2shp-0.1.9-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3c3bbeeb730f1577c93bb5c2fb969124fe6ee480723778011e635682a803e499
MD5 256bd0189c1718e25157e434cb488b11
BLAKE2b-256 6d9e9b1eae656001c3e3fd145c88efb78bddd50a91e97227047af8fb05b66b3e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for alobj2shp-0.1.9-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 1d9ac178f4ce212252d8bc6057f0caa8b80fcc4341a40ebc686f1e32d00f6599
MD5 e75c6b7c95ab48fc2c2cb44534d9a546
BLAKE2b-256 1c6f37e2647f2a07193902f7eb769a06fa14d9b9e292829947ccb774e03f1b6b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for alobj2shp-0.1.9-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 679e94be6f6bd9ae63839b642d35c2377df7914c55d152c7c4bd15973ca36a81
MD5 8b6e78d3235c690a5f5c04622915148e
BLAKE2b-256 33ef6b18e63469399fc2cad74c25c9396ef78902b3bd3329b361775eb75b2d05

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for alobj2shp-0.1.9-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9ef84e893c95fddf000a9e1e85489c1d7399e25236239e479e0ab0c09ab55acc
MD5 3edfa27e5dc13d32d153b1f400b669b3
BLAKE2b-256 72e67bd33ccb700a4acd7c5e4650fc540d3b44cf5c00269d534b12a6cb6e747b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for alobj2shp-0.1.9-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b8b2cdd5b78a4beffcf46e6a2db778965f0f880ef06220c8c41ce1bbdcb5d7a8
MD5 9c2dade2f600e681b2c0d7a8398d6dca
BLAKE2b-256 9709318500e430ebb1e8259911aaf9dc50a25371511acd40ebd38a43314ea90d

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