简体   繁体   中英

I want to convert a geojson from dxf file and longitude, latitude. How can we do that?

I wrote a function which takes dxfgeometry and coordinate points and converts them to the geojson. But it was not showing the location which it was supposed to show.

Can you provide me a way and mathematical calculations to cretae a geojson file?

According to the documentation, you could try doing it like this:

First get epsg code and the CRS transformation matrix:

    # Get the geo location information from the DXF file:
    geo_data = msp.get_geodata()
    if geo_data:
        # Get transformation matrix and epsg code:
        m, epsg = geo_data.get_crs_transformation()
    else:
        # Identity matrix for DXF files without geo reference data:
        m = Matrix44()

Query the DXF entities to export:

    for track in msp.query("LWPOLYLINE"):
        export_geojson(track, m)

Create a GeoProxy object from the DXF entity:

def export_geojson(entity, m):
    # Convert DXF entity into a GeoProxy object:
    geo_proxy = geo.proxy(entity)

Transform DXF WCS coordinates in modelspace units into the CRS coordinate system by the transformation matrix m:

    # Transform DXF WCS coordinates into CRS coordinates:
    geo_proxy.wcs_to_crs(m)

The next step assumes a EPSG:3395 projection, everything else needs a custom transformation function:

    # Transform 2D map projection EPSG:3395 into globe (polar)
    # representation EPSG:4326
    geo_proxy.map_to_globe()

Use the json module from the Python standard library to write the GeoJSON data, provided by the GeoProxy. geo_interface property:

    # Export GeoJSON data:
    name = entity.dxf.layer + ".geojson"
    with open(TRACK_DATA / name, "wt", encoding="utf8") as fp:
        json.dump(geo_proxy.__geo_interface__, fp, indent=2)

The content of the GeoJSON file looks like this:

{
  "type": "LineString",
  "coordinates": [
    [
      15.430999,
      47.06503
    ],
    [
      15.431039,
      47.064797
    ],
    [
      15.431206,
      47.064582
    ],
    [
      15.431283,
      47.064342
    ],
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM