Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make DXF writing layers more developer-friendly #11657

Open
michkowalczuk opened this issue Jan 15, 2025 · 9 comments
Open

Make DXF writing layers more developer-friendly #11657

michkowalczuk opened this issue Jan 15, 2025 · 9 comments

Comments

@michkowalczuk
Copy link
Contributor

Feature description

Creating a new layer in a DXF dataset should automatically fill in the built-in "Layer" field to easily create a multi-layer DXF file.

Creating multi-layer datasets for all GDAL vector drivers should be unified as much as possible. This will allow you to create multi-layer datasets regardless of the driver (GPKG, DXF, GML, etc.).

What do you think?

Additional context

No response

@jratike80
Copy link
Collaborator

What other drivers than DXF do you know that require something more than just CreateLayer?
JSON-FG is a bit similar than DXF, but creating JSON-FG that can have several layers even there exists only one is not what all (or any?) users would desire. Therefore the driver has an option SINGLE_LAYER=YES/NO https://gdal.org/en/stable/drivers/vector/jsonfg.html#dataset-creation-options. Perhaps DXF driver could have something similar.

@michkowalczuk
Copy link
Contributor Author

@jratike80, My intention is more general: to have one way (algorithm) to write several layers to a dataset, regardless of the driver.
This means that when writing code, I don't have to create if-else statements to account for different approaches to writing datasets by the driver. And if it's possible to implement it in GDAL, why not? Especially since this driver is quite widely used.

@jratike80
Copy link
Collaborator

Isn't DXF rather different from the GIS formats anyway? I understand that you want to do something more advanced than what ogr2ogr or gdal.VectorTranslate can do, but
ogr2ogr -f [format] twolayers_output.xxx twolayers_input.gpkg
works fine for formats GML, GPKG, ESRI Shapefile, JSONFG. It does not give an error for CSV but maybe it should, because the output is invalid.

I fear that to write several layers to a dataset, regardless of the driver will not work completely because GIS and CAD come from two different worlds. For GIS formats I believe it mostly work already. With DXF it seems that some tricks are needed also for the attributes, not only layers.

ERROR 1: DXF layer does not support arbitrary field creation, field 'korkeusarvo' not created.
ERROR 1: Unable to have more than one OGR entities layer in a DXF file, with one options blocks layer.

@michkowalczuk
Copy link
Contributor Author

michkowalczuk commented Jan 15, 2025

Please, imagine that you have 3 layers: "roads", "buildings" and "parcels". You want to share your GIS project someone out-of-the GIS world, so your choice is DXF, but for other reasons you also want to store this layers in GPKG file.

By default DXF writer uses "0" layer for entities, but you can manage DXF entities localization by manually assigning "Layer" internal field. Thanks to this the DXF will contain your data organized in CAD layers similar to GIS approach.

But now... it requires 2 approaches for writing GPKG and DXF.

GDAL could introduce automatically filling "Layer" field when you invoke Dataset.CreateLayer function, and this simple change let make much simpler and unified method of writing to both formats. In the above case, but thinking more general to any driver that supports multiple vector layers. Remember: I'm writing about GDAL API, not the console utils.

@michkowalczuk
Copy link
Contributor Author

michkowalczuk commented Jan 15, 2025

ERROR 1: DXF layer does not support arbitrary field creation, field 'korkeusarvo' not created.
ERROR 1: Unable to have more than one OGR entities layer in a DXF file, with one options blocks layer.

This problem also can be omitted using smart approach (unified code, without if-else exceptions) ;-)

@michkowalczuk michkowalczuk changed the title Make DXF writing layers more user-friendly Make DXF writing layers more developer-friendly Jan 15, 2025
@jratike80
Copy link
Collaborator

If you exclude DXF, how much if-else exceptions do you need to use?

I believe that unified code cannot resolve DXF conversion but special tools are needed for that purpose. QGIS seems to have one and I tested it a bit by opening my two-layer test gpkg with QGIS and using an option "Export Project to DXF". It created a DXF file where ogrinfo finds one layer, "entities". The result does not have a Layer field but the layers ("tieviiva" and "rakennus") are separated this way:

AcDbEntity
  8
twolayers2 — tieviiva
...
AcDbEntity
  8
twolayers2 — rakennus

For some reason QGIS developers have selected to do this instead of "filling in the built-in "Layer" field to easily create a multi-layer DXF file". For me the connection between GIS layers and DXF layers is not clear, and https://ezdxf.readthedocs.io/en/stable/tables/layer_table_entry.html does not help much.

I fear I do not have anything else to add into this issue. Let's hope some CAD experts appears.

@jratike80
Copy link
Collaborator

By default DXF writer uses "0" layer for entities, but you can manage DXF entities localization by manually assigning "Layer" internal field. Thanks to this the DXF will contain your data organized in CAD layers similar to GIS approach.

Even I still fear I don't have anything to add, I am curious to see a sample of such data.

@michkowalczuk
Copy link
Contributor Author

michkowalczuk commented Jan 17, 2025

I believe that unified code cannot resolve DXF conversion but special tools are needed for that purpose. QGIS seems to have one and I tested it a bit by opening my two-layer test gpkg with QGIS and using an option "Export Project to DXF". It created a DXF file where ogrinfo finds one layer, "entities". The result does not have a Layer field but the layers ("tieviiva" and "rakennus") are separated this way:

AcDbEntity
  8
twolayers2 — tieviiva
...
AcDbEntity
  8
twolayers2 — rakennus

For some reason QGIS developers have selected to do this instead of "filling in the built-in "Layer" field to easily create a multi-layer DXF file".

Did you open this file in AutoCAD or other CAD viewer...?
QGIS export tool does exactly what I'm thinking about. They write each GIS layer into separate CAD layer in DXF file.

And the same can be easily done when you set "Layer" field for each feature written to DXF layer on the GDAL site (tested).

Image

Let's hope some CAD experts appears.

Maybe I'm not the expert, but I have worked with CAD systems for several years and I know how data is organized by CAD users.

I would just like to suggest that introducing two small changes to the GDAL source code (this issue #11657 and this) will enable more flexible and universal use of the DXF driver. I'm not a C++ programmer, so I can't do it.

@jratike80
Copy link
Collaborator

jratike80 commented Jan 17, 2025

Did you open this file in AutoCAD or other CAD viewer...?
QGIS export tool does exactly what I'm thinking about. They write each GIS layer into separate CAD layer in DXF file.

AutoCAD is too expensive for me and and for my employer, but I had a try with FreeCAD. Unfortunately it failed, but I think that it is not due to the data but due to a missing feature in that other fine Open Source project.

12:59:12  pyException: C:\Program Files\FreeCAD 1.0\Mod\Draft\importDXF.py(2839)<class 'SystemError'>: <function _import at 0x000002AC878E7420> returned a result with an exception set
12:59:12  Unsupported DXF features:
12:59:12  Entity type 'HATCH': 3593 time(s) first at line 1385400

It is nice that you have two workarounds, to use the QGIS "export project" tool, or to add and populate a new "layer" field into the source data. Because the task is so well defined, it may be that some of the few GDAL developers who have experience on DXF start working on it some day. Or you can try to do it yourself, or use another developer-friendly method and hire one.

Disclaimer: I am not a developer, I am just a GDAL user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants