File: json_export.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (43 lines) | stat: -rw-r--r-- 1,358 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Copyright (c) 2023-2024, Manfred Moitzi
# License: MIT License
import pathlib

import ezdxf
from ezdxf.addons.drawing import Frontend, RenderContext
from ezdxf.addons.drawing import json

CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
if not CWD.exists():
    CWD = pathlib.Path(".")

# ------------------------------------------------------------------------------
# This example shows how to export the modelspace by the drawing add-on and the
# GeoJSONBackend.
#
# docs: https://ezdxf.mozman.at/docs/addons/drawing.html
# ------------------------------------------------------------------------------


def usa_geojson():
    filename = pathlib.Path(__file__).parent / "data" / "usa.dxf"
    doc = ezdxf.readfile(filename)
    msp = doc.modelspace()
    backend = json.GeoJSONBackend()
    Frontend(RenderContext(doc), backend).draw_layout(msp)
    json_string = backend.get_string()
    (CWD / "usa.geo.json").write_text(json_string)


def usa_custom_json():
    filename = pathlib.Path(__file__).parent / "data" / "usa.dxf"
    doc = ezdxf.readfile(filename)
    msp = doc.modelspace()
    backend = json.CustomJSONBackend()
    Frontend(RenderContext(doc), backend).draw_layout(msp)
    json_string = backend.get_string()
    (CWD / "usa.custom.json").write_text(json_string)


if __name__ == "__main__":
    usa_geojson()
    usa_custom_json()