Skip to main content

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_height 或 export_model_height 为 True 且取值不是 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 顶点在进入后续“投影/轮廓生成”之前的轴变换方式(见参数说明)。
  • gsd 与 simplify_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())

Metadata

Release files for alobj2shp 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for alobj2shp 0.2.0
File Actions
alobj2shp-0.2.0-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details for alobj2shp-0.2.0-cp314-cp314-win_amd64.whl
alobj2shp-0.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 Details for alobj2shp-0.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
alobj2shp-0.2.0-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for alobj2shp-0.2.0-cp313-cp313-win_amd64.whl
alobj2shp-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.24+ x86-64, Linux glibc 2.28+ x86-64 Details for alobj2shp-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
alobj2shp-0.2.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for alobj2shp-0.2.0-cp312-cp312-win_amd64.whl
alobj2shp-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 Details for alobj2shp-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
alobj2shp-0.2.0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details for alobj2shp-0.2.0-cp311-cp311-win_amd64.whl
alobj2shp-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ x86-64, Linux glibc 2.24+ x86-64 Details for alobj2shp-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Total release size: 8.9 MB

Release files / alobj2shp-0.2.0-cp314-cp314-win_amd64.whl

Download URL alobj2shp-0.2.0-cp314-cp314-win_amd64.whl
Size 501.3 kB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
a739255178c1f987bde36aecfb57fc17490a75fee210825559ba861a4427c2b9
BLAKE2b-256 checksum
How to use checksums
e48f7338e11c82ba4b6875200b64415830c7edff3675d6d04e862142624cc791
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / alobj2shp-0.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL alobj2shp-0.2.0-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 1.7 MB
Tags CPython 3.14 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
86d1136b183e08f371054c7ca3d09a157f6866fad21ef2c68cdc45bac20af410
BLAKE2b-256 checksum
How to use checksums
6cac89646f493be707e409f350b55da10cebebd4324e3045db3fdf725db543b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / alobj2shp-0.2.0-cp313-cp313-win_amd64.whl

Download URL alobj2shp-0.2.0-cp313-cp313-win_amd64.whl
Size 487.2 kB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
fe287dbf1450867477cd5bbffcba53921cc8fa80bbcb9bbe4e22743beb0ba573
BLAKE2b-256 checksum
How to use checksums
0aa8f7c8d009c0767ca3dcc78f41a15d0c24e52742e2c91363dd5f0d6a619f8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / alobj2shp-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL alobj2shp-0.2.0-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 1.7 MB
Tags CPython 3.13 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
cbdf08cc43a31a69e2df4491580589295fadd36ba391f1c3a45a5c8930a08ed6
BLAKE2b-256 checksum
How to use checksums
823f532f62075e4dee6fef50dffa39e72a1e3deec672496dc0e7fe1497f48453
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / alobj2shp-0.2.0-cp312-cp312-win_amd64.whl

Download URL alobj2shp-0.2.0-cp312-cp312-win_amd64.whl
Size 487.3 kB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
a3c54770af8f893f01b7651a96e47f1bd031516374051d1e7e272bf9fc65b5b8
BLAKE2b-256 checksum
How to use checksums
7e11cb0949825d57b4e98d3dd4ca8aa1b34b042ea845cde57777e643ea8e7ac2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / alobj2shp-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL alobj2shp-0.2.0-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 1.7 MB
Tags CPython 3.12 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
b8c9a58f4a762957b67c1a036c04224b0362fbfdcbc93f452870f9b9159cab88
BLAKE2b-256 checksum
How to use checksums
141458f5d2cb8c6ce37f5eec7bc1e780351934551ff59b0ff63d2cc17522e9e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / alobj2shp-0.2.0-cp311-cp311-win_amd64.whl

Download URL alobj2shp-0.2.0-cp311-cp311-win_amd64.whl
Size 487.8 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
343d78fb094795b6f5c7688f4217f654b039aba724293127e226189a6c692054
BLAKE2b-256 checksum
How to use checksums
2cc01f7db56caa417da6fde387de9e0796ea2c0f9e5cd44860d0352d027128ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / alobj2shp-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl

Download URL alobj2shp-0.2.0-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Size 1.7 MB
Tags CPython 3.11 Linux glibc 2.24+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
e8c4ada0cb54ab124131cdcde10bd5cea28aef2523c5ac971657b75d5bd69a84
BLAKE2b-256 checksum
How to use checksums
155c64c9eb08639e66bfcc711b4f9bd44bee10e2b391e6e09592649db65a6f52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for alobj2shp 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page