0

I would like to convert a GML file with cadastral parcels to GeoJSON format using ogr2ogr. See also attached extract of the GML file at the end of this question.

Additional requirements:

  • Convert projection from EPSG:28992 to EPSG:4326
  • Limit precision in the output file to 7 decimals
  • Convert all features, but only selected attributes from each feature

The command:

ogr2ogr -lco COORDINATE_PRECISION=7 -f "GeoJSON" cadastral_parcels.geojson -t_srs "EPSG:4326" cadastral_parcels.gml

gives the required conversion with the right projection, although it takes two runs. The first time executed, ogr2ogr generates a gfs file (see attached file). In this file I have to change line 6:

<GeometryType>1</GeometryType>

for:

<GeometryElementPath>begrenzingPerceel</GeometryElementPath>

I have no clear idea why this is needed, but it is the same change required when importing the GML file in QGIS to make QGIS show the polygons of each parcel instead of single points. Executing the ogr2ogr command after this change results in effective conversion to GeoJSON format, changed projection from the original EPSG:28992 to EPSG:4326 and limited precision to 7 decimals.

The issue I am left with is to select only particular attributes. The resulting GeoJSON file holds besides the Polygon coordinates in <oz:begrenzingPerceel> .... </oz:begrenzingPerceel> also all additional attributes from the original GML file.

I am only interested in the attributes:

<oz:perceelnummer>2298</oz:perceelnummer> 

and

<oz:perceelnummerRotatie>0.0</oz:perceelnummerRotatie>

Is there an option for ogr2ogr to select which attributes should be included in the output file and which not?

Or is there another command line tool to remove the unwanted attributes from my resulting GeoJSON file?

Extract GML file:

<?xml version="1.0" encoding="UTF-8"?>
<kk:DKK
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:gml="http://www.opengis.net/gml/3.2"
        xmlns:oz="http://www.kadaster.nl/schemas/brkkadastralekaart/v5/imkad-onroerendezaak/v20220601"
        xmlns:h="http://www.kadaster.nl/schemas/brkkadastralekaart/v5/imkad-historie/v20220601"
        xmlns:t="http://www.kadaster.nl/schemas/brkkadastralekaart/v5/imkad-typen/v20220601"
        xmlns:ko="http://www.kadaster.nl/schemas/brkkadastralekaart/v5/imkad-kadastraalobject/v20220601"
        xmlns:q="http://www.kadaster.nl/schemas/brkkadastralekaart/v5/imkad-kwaliteit/v20220601"
        xmlns:kk="http://www.kadaster.nl/schemas/brkkadastralekaart/v5/stand/v20220601"
        xsi:schemaLocation="http://www.kadaster.nl/schemas/brkkadastralekaart/v5/stand/v20220601 http://www.kadaster.nl/schemas/brkkadastralekaart/v5/xsd/dkk/stand/v20220601/DKK-Product_Stand_v5_0_0.xsd">
<kk:perceelMember>
    <oz:Perceel gml:id="NL.IMKAD.KadastraalObject.84920229870000">
        <ko:identificatie domein="NL.IMKAD.KadastraalObject">84920229870000</ko:identificatie>
        <oz:historie>
            <h:beginGeldigheid>2000-11-20T14:29:09+01:00</h:beginGeldigheid>
            <h:tijdstipRegistratie>2000-11-20T14:29:09+01:00</h:tijdstipRegistratie>
            <h:volgnummer>0</h:volgnummer>
            <h:statusHistorie>
                <h:StatusHistorie>
                    <t:code>G</t:code>
                    <t:waarde>Geldig</t:waarde>
                </h:StatusHistorie>
            </h:statusHistorie>
        </oz:historie>
        <oz:kadastraleAanduiding>
            <oz:TypeKadastraleAanduiding>
                <oz:kadastraleGemeente>
                    <oz:KadastraleGemeente>
                        <t:code>761</t:code>
                        <t:waarde>Otterlo</t:waarde>
                    </oz:KadastraleGemeente>
                </oz:kadastraleGemeente>
                <oz:sectie>B</oz:sectie>
                <oz:perceelnummer>2298</oz:perceelnummer>
                <oz:aKRKadastraleGemeenteCode>
                    <oz:AKRKadastraleGemeenteCode>
                        <t:code>812</t:code>
                        <t:waarde>OTL02</t:waarde>
                    </oz:AKRKadastraleGemeenteCode>
                </oz:aKRKadastraleGemeenteCode>
            </oz:TypeKadastraleAanduiding>
        </oz:kadastraleAanduiding>
        <oz:begrenzingPerceel><gml:Polygon xmlns:gml="http://www.opengis.net/gml/3.2" gml:id="GEOMETRY_d68dfb9b-728b-411f-88be-f3c90cb54237" srsName="urn:ogc:def:crs:EPSG::28992"><gml:exterior><gml:LinearRing><gml:posList>181182.346 456712.174 181196.546 456703.704 181203.710 456699.711 181208.183 456707.260 181186.952 456719.878 181182.346 456712.174</gml:posList></gml:LinearRing></gml:exterior></gml:Polygon></oz:begrenzingPerceel>
        <oz:kadastraleGrootte>
            <oz:TypeOppervlak>
                <oz:waarde>222</oz:waarde>
                <oz:soortGrootte>
                    <oz:SoortGrootte>
                        <t:code>1</t:code>
                        <t:waarde>Vastgesteld</t:waarde>
                    </oz:SoortGrootte>
                </oz:soortGrootte>
            </oz:TypeOppervlak>
        </oz:kadastraleGrootte>
        <oz:perceelnummerRotatie>0.0</oz:perceelnummerRotatie>
        <oz:perceelnummerVerschuiving>
            <oz:TypePerceelnummerVerschuiving>
                <oz:deltaX>0.0</oz:deltaX>
                <oz:deltaY>0.0</oz:deltaY>
            </oz:TypePerceelnummerVerschuiving>
        </oz:perceelnummerVerschuiving>
        <oz:plaatscoordinaten><gml:Point xmlns:gml="http://www.opengis.net/gml/3.2" gml:id="GEOMETRY_061c9c35-c29f-45df-8e5f-9c7fabcf3f82" srsName="urn:ogc:def:crs:EPSG::28992"><gml:pos>181202.040 456706.351</gml:pos></gml:Point></oz:plaatscoordinaten>
    </oz:Perceel>
</kk:perceelMember>
</kk:DKK>


**GFS File created at first run:**

<GMLFeatureClassList>
  <GMLFeatureClass>
    <Name>Perceel</Name>
    <ElementPath>Perceel</ElementPath>
    <!--POINT-->
    <GeometryType>1</GeometryType>
    <SRSName>urn:ogc:def:crs:EPSG::28992</SRSName>
    <DatasetSpecificInfo>
      <FeatureCount>1</FeatureCount>
      <ExtentXMin>181202.04000</ExtentXMin>
      <ExtentXMax>181202.04000</ExtentXMax>
      <ExtentYMin>456706.35100</ExtentYMin>
      <ExtentYMax>456706.35100</ExtentYMax>
    </DatasetSpecificInfo>
    <PropertyDefn>
      <Name>identificatie</Name>
      <ElementPath>identificatie</ElementPath>
      <Type>Integer</Type>
      <Subtype>Integer64</Subtype>
    </PropertyDefn>
    <PropertyDefn>
      <Name>beginGeldigheid</Name>
      <ElementPath>historie|beginGeldigheid</ElementPath>
      <Type>String</Type>
      <Width>25</Width>
    </PropertyDefn>
    <PropertyDefn>
      <Name>tijdstipRegistratie</Name>
      <ElementPath>historie|tijdstipRegistratie</ElementPath>
      <Type>String</Type>
      <Width>25</Width>
    </PropertyDefn>
    <PropertyDefn>
      <Name>volgnummer</Name>
      <ElementPath>historie|volgnummer</ElementPath>
      <Type>Integer</Type>
    </PropertyDefn>
    <PropertyDefn>
      <Name>code</Name>
      <ElementPath>historie|statusHistorie|StatusHistorie|code</ElementPath>
      <Type>String</Type>
      <Width>1</Width>
    </PropertyDefn>
    <PropertyDefn>
      <Name>waarde</Name>
      <ElementPath>historie|statusHistorie|StatusHistorie|waarde</ElementPath>
      <Type>String</Type>
      <Width>6</Width>
    </PropertyDefn>
    <PropertyDefn>
      <Name>kadastraleAanduiding|TypeKadastraleAanduiding|kadastraleGemeente|KadastraleGemeente|code</Name>
      <ElementPath>kadastraleAanduiding|TypeKadastraleAanduiding|kadastraleGemeente|KadastraleGemeente|code</ElementPath>
      <Type>Integer</Type>
    </PropertyDefn>
    <PropertyDefn>
      <Name>kadastraleAanduiding|TypeKadastraleAanduiding|kadastraleGemeente|KadastraleGemeente|waarde</Name>
      <ElementPath>kadastraleAanduiding|TypeKadastraleAanduiding|kadastraleGemeente|KadastraleGemeente|waarde</ElementPath>
      <Type>String</Type>
      <Width>7</Width>
    </PropertyDefn>
    <PropertyDefn>
      <Name>sectie</Name>
      <ElementPath>kadastraleAanduiding|TypeKadastraleAanduiding|sectie</ElementPath>
      <Type>String</Type>
      <Width>1</Width>
    </PropertyDefn>
    <PropertyDefn>
      <Name>perceelnummer</Name>
      <ElementPath>kadastraleAanduiding|TypeKadastraleAanduiding|perceelnummer</ElementPath>
      <Type>Integer</Type>
    </PropertyDefn>
    <PropertyDefn>
      <Name>kadastraleAanduiding|TypeKadastraleAanduiding|aKRKadastraleGemeenteCode|AKRKadastraleGemeenteCode|code</Name>
      <ElementPath>kadastraleAanduiding|TypeKadastraleAanduiding|aKRKadastraleGemeenteCode|AKRKadastraleGemeenteCode|code</ElementPath>
      <Type>Integer</Type>
    </PropertyDefn>
    <PropertyDefn>
      <Name>kadastraleAanduiding|TypeKadastraleAanduiding|aKRKadastraleGemeenteCode|AKRKadastraleGemeenteCode|waarde</Name>
      <ElementPath>kadastraleAanduiding|TypeKadastraleAanduiding|aKRKadastraleGemeenteCode|AKRKadastraleGemeenteCode|waarde</ElementPath>
      <Type>String</Type>
      <Width>5</Width>
    </PropertyDefn>
    <PropertyDefn>
      <Name>kadastraleGrootte|TypeOppervlak|waarde</Name>
      <ElementPath>kadastraleGrootte|TypeOppervlak|waarde</ElementPath>
      <Type>Integer</Type>
    </PropertyDefn>
    <PropertyDefn>
      <Name>kadastraleGrootte|TypeOppervlak|soortGrootte|SoortGrootte|code</Name>
      <ElementPath>kadastraleGrootte|TypeOppervlak|soortGrootte|SoortGrootte|code</ElementPath>
      <Type>Integer</Type>
    </PropertyDefn>
    <PropertyDefn>
      <Name>kadastraleGrootte|TypeOppervlak|soortGrootte|SoortGrootte|waarde</Name>
      <ElementPath>kadastraleGrootte|TypeOppervlak|soortGrootte|SoortGrootte|waarde</ElementPath>
      <Type>String</Type>
      <Width>11</Width>
    </PropertyDefn>
    <PropertyDefn>
      <Name>perceelnummerRotatie</Name>
      <ElementPath>perceelnummerRotatie</ElementPath>
      <Type>Real</Type>
    </PropertyDefn>
    <PropertyDefn>
      <Name>deltaX</Name>
      <ElementPath>perceelnummerVerschuiving|TypePerceelnummerVerschuiving|deltaX</ElementPath>
      <Type>Real</Type>
    </PropertyDefn>
    <PropertyDefn>
      <Name>deltaY</Name>
      <ElementPath>perceelnummerVerschuiving|TypePerceelnummerVerschuiving|deltaY</ElementPath>
      <Type>Real</Type>
    </PropertyDefn>
  </GMLFeatureClass>
</GMLFeatureClassList>

EDIT: My gml file holds place coordinates for a label (parcel id) and two fields deltaX and deltaY to shift the label position on the map for presentation reasons. See the image for an example.

In the conversion from gml to geojson I like to achieve two additional tasks:

  1. In the earlier discussed conversion cycle for the place coordinates, change the given place coordinates (X and Y) by adding deltaX and deltaY before converting the projection from EPSG:28992 to EPSG:4326 (as deltX and deltaY seem to be in EPSG:28992 units)

  2. Create a separate additional geojson file holding a line geometry from the original place coordinates to the changed place coordinates, for those features only where deltaX<>0 or deltaY<>0

These two steps should enable my to recreate the given example in the image attached. In what way should I setup the ogr2ogr command for this purpose?

enter image description here

enter image description here

1 Answer 1

0

Editing the .gfs file is needed because the GML contains also a point geometry and GDAL selects it by default:

<oz:plaatscoordinaten><gml:Point xmlns:gml="http://www.opengis.net/gml/3.2" gml:id="GEOMETRY_061c9c35-c29f-45df-8e5f-9c7fabcf3f82" srsName="urn:ogc:def:crs:EPSG::28992"><gml:pos>181202.040 456706.351</gml:pos></gml:Point></oz:plaatscoordinaten>

Easiest method for selecting just some attributes is to read the documentation from here https://gdal.org/programs/ogr2ogr.html#cmdoption-ogr2ogr-select and use the -select option.

ogr2ogr -f geojson outputtest.json input.gml -select perceelnummer,perceelnummerRotatie

EDIT

Alternatively it is possible to use the -sql parameter.

First check what layers GDAL finds with ogrinfo

ogrinfo input.gml
INFO: Open of `input.gml'
      using driver `GML' successful.
1: Perceel

Use that layer name in the SQL statement. Similarly use ogrinfo for seeing the names of the attbibutes. For example "PerceelnummerVerschuiving|deltaX" seems to get truncated into "deltaX".

ogrinfo input.gml -sql "select deltaX,deltaY from perceel"
...
OGRFeature(perceel):84920229870000
  deltaX (Real) = 0
  deltaY (Real) = 0
  POLYGON ((181182.346 456712.174,181196.546 456703.704,181203.71 456699.711,181208.183 456707.26,181186.952 456719.878,181182.346 456712.174))
9
  • Thx. I tried this approach on other fields as well, but 'nested' fields are a little problematic. For example the field plaatscoordinaten or the field perceelnummerVerschuiving cannot be selected with -select. From the documentation I understand that perhaps the -sql option is necessary. But using -sql "select perceelnummer, perceelnummerRotatie from cadastral_parcels" already results in an error message ("SELECT from table cadastral_parcels failed, no such table/featureclass.") let alone an -sql that refers to the deeper nestes fields. Any suggestion how to achieve this?
    – Sebastiaan
    Commented Apr 1 at 9:17
  • See the edited answer. The name of the layer is "Perceel".
    – user30184
    Commented Apr 1 at 10:22
  • I start to get the picture and managed to extract several fields, except plaatscoordinaten. Gradually I start to understand that my gml file holds two geometries: 1. Point with the coordinates for the parcel number 2. Polygon with the coordinates for the parcel boundaries
    – Sebastiaan
    Commented Apr 1 at 13:52
  • By editing the gfs to the point geometry I am able to create a GeoJSON file holding the point coordinates and setting the gfs file to the polygon geometry results in a GeoJSON file holding the polygon coordinates. Is it possible to create a single GeoJSON file holding both the point coordinates and the polygon coordinates and the selected fields (perceelnumber, rotation, etc.)? Is this perhaps done by creating multiple layers?
    – Sebastiaan
    Commented Apr 1 at 13:52
  • GeoJSON format supports only one layer per file and one geometry object per layer. It is possible to write additional geometries as "foreign members" datatracker.ietf.org/doc/html/rfc7946#section-6.1. If you go that way you must implement the writing and reading of extra geometries yourself. The JSON-FG format gdal.org/drivers/vector/jsonfg.html will support several layers by the use of featureType elements. See the draft of the standard docs.ogc.org/DRAFTS/21-045r1.html.
    – user30184
    Commented Apr 1 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.