Skip to main content

Convert CAD DXF data into geospatial formats and visualisations using GeoPandas/Pyogrio.

Project description

dxf2geo

PyPI - Version PyPI - Python Version Tests Lint DOI

[!WARNING]
This package is in the early stages of development and should not be installed unless you are one of the developers.

dxf2geo is a small Python package for converting CAD .dxf files into geospatial formats such as Shapefiles and GeoPackages, and for producing interactive visualisations of the extracted geometry.

It is designed to automate the process of extracting geometry by type (point, line, polygon, etc.), filtering or cleaning the results, and inspecting the output spatially.


Table of contents

Installation

dxf2geo uses the GDAL Python bindings (osgeo.*). On supported platforms, pip install dxf2geo will pull a compatible GDAL wheel from PyPI.

pip install dxf2geo

If your platform does not have a prebuilt GDAL wheel, install GDAL from your system/package manager first (or via Conda), then install dxf2geo:

sudo apt install gdal-bin libgdal-dev
pip install dxf2geo

Before usage, it may be worth verifying your installation to ensure that GDAL is installed:

python -c "from osgeo import gdal, ogr; print('GDAL', gdal.VersionInfo(), 'DXF driver:', bool(ogr.GetDriverByName('DXF')))"

If the installation has worked, you should see something like GDAL ##### DXF driver: True.

Features

  • Converts DXF files to common vector formats (e.g. Shapefile, GeoPackage),
  • Supports geometry filtering by type (e.g., LINESTRING, POLYGON),
  • Skips invalid geometries,
  • Visualises output geometries in an interactive Plotly-based HTML map,
  • Filters out short, axis-aligned DXF gridding lines (optional cleanup step).

Example usage

Below is an example of using the functionality of this package on a CAD file example.dxf. This creates a set of shapefiles for of the types of geometry in a new output/ directory.

# Imports
from dxf2geo.extract import extract_geometries
from dxf2geo.visualise import (
    load_geometries,
    plot_geometries,
)
from pathlib import Path

# Define paths
input_dxf = Path("./example.dxf").expanduser()
output_dir = Path("output")

# Process CAD file
extract_geometries(input_dxf, output_dir)

# Produce `plotly` html figure
gdf = load_geometries(output_dir)
plot_geometries(gdf, output_dir / "geometry_preview.html")

Following this, we would have an output folder that looks like:

output/
├── point/
│   └── point.shp
├── linestring/
│   └── linestring.shp
├── polygon/
│   └── polygon.shp
...
└── export.log

Layer filtering

At present we can filter CAD layers based on a number of different criteria.

Layer name (exact, case-insensitive)

FilterOptions(
    include_layers=("roads", "buildings"),
    exclude_layers=("defpoints", "tmp"),
)

Layer name (regular expressions)

We can also filter layers using regular expressions (applied to the CAD layer name).

FilterOptions(
    # Include any "roads" or "road" layer (case-insensitive), and any layer starting with "bldg_"
    include_layer_patterns=(r"(?i)^roads?$", r"^bldg_.*"),
    # Exclude layers named "defpoints" (any case) or prefixed with "tmp_"
    exclude_layer_patterns=(r"(?i)^defpoints$", r"^tmp_"),
)

Geometry size/structure

# Drop empty geometries and zero-area/length features
FilterOptions(drop_empty=True, drop_zero_geom=True)

# Minimum polygon area
FilterOptions(min_area=5.0)

# Minimum line length
FilterOptions(min_length=10.0)

Spatial bounding box

# (minx, miny, maxx, maxy)
FilterOptions(bbox=(430000.0, 420000.0, 435000.0, 425000.0))

Attribute-value exclusions (exact match)

# Exclude features where fields have disallowed values
FilterOptions(exclude_field_values={
    "EntityType": {"TEXT", "MTEXT"},
    "Linetype": {"HIDDEN"},
})

Geometry type selection (at extraction call)

extract_geometries(
    dxf_path,
    output_root,
    geometry_types=("POINT", "LINESTRING", "POLYGON", "MULTILINESTRING", "MULTIPOLYGON"),
    filter_options=FilterOptions(...),
)

License

dxf2geo is distributed under the terms of the MIT license.

Funding

This package is being developed as part of work on the EPSRC Digital Health Hub for Antimicrobial Resistance (EP/X031276/1).

Project details


Download files

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

Source Distribution

dxf2geo-0.1.4.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

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

dxf2geo-0.1.4-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file dxf2geo-0.1.4.tar.gz.

File metadata

  • Download URL: dxf2geo-0.1.4.tar.gz
  • Upload date:
  • Size: 19.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dxf2geo-0.1.4.tar.gz
Algorithm Hash digest
SHA256 40686ba58c014afd8d2971700fae33ed95495a3ed57193041231db00febe1f30
MD5 f0bd09c1843b4c084be7a19c5fdfb284
BLAKE2b-256 5713b0613dae4f21a4174ae3626759d811a0e4d5148f47266eade46632bf416b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dxf2geo-0.1.4.tar.gz:

Publisher: publish.yml on ksuchak1990/dxf2geo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dxf2geo-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: dxf2geo-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for dxf2geo-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 49d87e6a65d4da3988404c7fcda7264d78a4c171b594b724b105d6183827b692
MD5 4b8616afaad0f3c85ce342270b350bd5
BLAKE2b-256 78bf05a54efbc62cf6354df0f26b2dc9f00a466b990717613c88a82c8e6b4d55

See more details on using hashes here.

Provenance

The following attestation bundles were made for dxf2geo-0.1.4-py3-none-any.whl:

Publisher: publish.yml on ksuchak1990/dxf2geo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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