A Python tool for converting Shapefile (.shp) files to 3D OBJ files. This tool can transform geospatial data into 3D models that can be used in 3D modeling software.
The generated OBJ models are converted to GLB format for loading in Cesium. This tool was developed to bridge the gap between geospatial data and 3D web visualization, enabling seamless integration of building footprints into Cesium-based 3D applications. Therefore, the final exported model coordinate system in this tool is consistent with the Cesium coordinate system. If other coordinate systems are needed, modifications must be made accordingly.
When working with 3D geospatial visualization projects, we often encountered the challenge of converting 2D building footprint data from Shapefiles into 3D models that could be loaded into Cesium for web-based 3D visualization. Existing tools were either too complex, expensive, or didn't provide the specific functionality needed for this workflow.
This tool provides a streamlined solution that:
- Converts Shapefile building footprints directly to 3D OBJ models
- Handles complex polygons with holes automatically
- Performs accurate coordinate transformations from geographic to 3D space
- Generates GLB format output for seamless Cesium integration
- Maintains building height information and geometric accuracy
The primary use case is for urban planning, architectural visualization, and 3D city modeling where building footprint data needs to be converted into 3D models for web-based visualization platforms like Cesium.
- πΊοΈ Shapefile Support: Read and process Shapefile format geospatial data
- πΊ Triangulation Processing: Automatically convert polygon faces to triangular meshes
- π³οΈ Hole Support: Support complex polygons with holes
- ποΈ 3D Modeling: Generate 3D building models with height information
- π Coordinate Conversion: Convert geographic coordinates to 3D spatial coordinates
- π Rotation Transformation: Support 2D and 3D coordinate rotation
- π OBJ Output: Generate standard OBJ format 3D model files
shp-transform-obj/
βββ main.py # Main program entry point
βββ shp2obj.py # Core Shapefile to OBJ conversion module
βββ coordinate.py # Geographic coordinate conversion utilities
βββ createTriangle.py # Polygon triangulation algorithms
βββ rotation.py # 2D/3D coordinate rotation transformations
βββ LICENSE # MIT License file
βββ README.md # English documentation
βββ README-zh.md # Chinese documentation
βββ buildings.obj # Generated 3D model output
βββ buildings.glb # GLB format 3D model
βββ buildings.txt # Center point coordinates
βββ aspose_3d-25.3.0-py3-none-win_amd64.whl # 3D library wheel file
βββ data/ # Input data directory
β βββ building.shp # Shapefile geometry data
β βββ building.shx # Shapefile index file
β βββ building.dbf # Shapefile attribute data
β βββ building.prj # Shapefile projection file
β βββ building.cpg # Shapefile code page
β βββ building.qmd # Shapefile metadata
βββ test/ # Test and example files
βββ normal-polygon.py # Regular polygon triangulation test
βββ hole-polygon.py # Polygon with holes triangulation test
We use SHP data downloaded from https://download.geofabrik.de/.
- Python 3.10
- Windows/Linux/macOS
pip install geopandas
pip install numpy
pip install shapely
pip install triangle
pip install geopy
pip install trimesh
pip install aspose-threed
pip install matplotlib
pip install ./aspose_3d-25.3.0-py3-none-win_amd64.whl
- Prepare Data: Place your Shapefile file in the
data/
directory - Modify Configuration: Modify file paths and parameters in
main.py
- Run Program: Execute the main program
python main.py
You can modify the following parameters in main.py
:
# Input file path
shapefile_path = r'data\\building.shp'
# Building height (meters)
buildingHeight = 3
# Output file name
output_file = 'buildings.obj'
from shp2obj import shp2obj
if __name__ == '__main__':
shapefile_path = r'data\\building.shp'
obj_path = 'building.obj'
shp2obj(shapefile_path, obj_path)
- Function: Geographic coordinate conversion
- Main Function:
calculateCoordinate()
- Purpose: Convert geographic coordinates to 3D spatial coordinates
- Function: Polygon triangulation
- Main Functions:
polygonToTriangleNormal()
: Process regular polygonspolygonToTriangleHole()
: Process polygons with holes
- Purpose: Convert polygon faces to triangular meshes
- Function: Coordinate rotation transformation
- Main Functions:
rotate_X()
: Rotate around X-axisrotate_Y()
: Rotate around Y-axisrotate_Z()
: Rotate around Z-axisrotate_2d()
: 2D plane rotation
The generated OBJ file contains:
- Vertex Data: 3D coordinate points
- Face Data: Triangle face definitions
- Material Information: Optional material and texture information
# Generated OBJ file
v 0.0 0.0 0.0
v 10.0 0.0 0.0
v 10.0 0.0 10.0
v 0.0 0.0 10.0
v 0.0 3.0 0.0
v 10.0 3.0 0.0
v 10.0 3.0 10.0
v 0.0 3.0 10.0
f 1 2 3
f 1 3 4
...
The project includes test files located in the test/
directory:
python test/normal-polygon.py
- Coordinate System: Ensure the input Shapefile uses the correct coordinate system (such as WGS84)
- Data Quality: Input polygons should be valid geometric shapes
- Memory Usage: Large datasets may require significant memory
- Output Path: Ensure you have write permissions for the output directory
- buildings.txt: This file contains the center coordinates used for positioning the 3D model when loading in Cesium
A: You can process them in batches or increase memory limits.
A: Check if the OBJ file format is correct and ensure all faces are triangles.
A: Check the coordinate system settings of the input data.
We welcome Issue submissions and Pull Requests to improve this project.
We will provide corresponding TypeScript code in the future to facilitate web developers' usage.
This project is licensed under the MIT License. See LICENSE file for details.
- Initial version release
- Support basic Shapefile to OBJ conversion
- Support triangulation and coordinate conversion
- Support polygon processing with holes