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

GML Version 3.1.1 Export #968

Closed
rebot opened this issue Oct 16, 2020 · 3 comments
Closed

GML Version 3.1.1 Export #968

rebot opened this issue Oct 16, 2020 · 3 comments
Milestone

Comments

@rebot
Copy link

rebot commented Oct 16, 2020

Hi,

I'm trying to make a GML export of a Polygon using Fiona. I've managed to make the export, but it seems like the export is in version 2 format of GML. How can I enable Fiona to make an export to GML Version 3.1.1. Should I add a new kind of schema? Is this related to my GDAL version?

This issue is related to a question I've asked on another project

Steps to reproduce the problem.

I make use of GeoPandas, Fiona and Shapely. I've installed Fiona and GDAL using the wheel provided on the following website.

GDAL Version 3.1.3

Fiona @ file:///C:/Users/BEGILT/Development/Python/RockWorks/Fiona-1.8.17-cp36-cp36m-win_amd64.whl
GDAL @ file:///C:/Users/BEGILT/Development/Python/RockWorks/GDAL-3.1.3-cp36-cp36m-win_amd64.whl

Next, I make a shapely Polygon object and use the mapping function to make sure Fiona can save it as a GML.
I've written a function to convert the GML from v2 tot v3.1.1, but I hope it's possible without the function!

# Schema = http://schemas.opengis.net/gml/ - Schema definitie voor de .gml
schema = {'properties': OrderedDict([]),'geometry': 'Polygon'}# Schema = http://schemas.opengis.net/gml/ - Schema definitie voor de .gml
schema = {'properties': OrderedDict([]),'geometry': 'Polygon'}

# https://gis.stackexchange.com/questions/52705/how-to-write-shapely-geometries-to-shapefiles
# https://geoscripting-wur.github.io/PythonVector/

def exporteer_vorm(segment):
    # Haal GML op schrijf de file weg in memory
    gml_bytes = BytesIO()
    gml_filename = os.path.join('hulpmiddelen','projectzone',f'{segment.segment}.gml')
    with fiona.open(gml_bytes, 'w', driver='GML', schema=schema, crs=from_epsg(31370)) as f:
        f.write({
            'geometry': mapping(segment.geometry),
            'properties': {}
        })
    gml_bytes.seek(0)
    # Omzetting .gml van versie 2 naar versie 3.1.1 dat bruikbaar is in pyDOV
    gml_content = gml_bytes.read().decode('utf-8')
    gml_content = gml_content.replace('Box','Envelope')
    gml_content = gml_content.replace('coord><gml:X', 'lowerCorner', 1)
    gml_content = gml_content.replace('Y></gml:coord', 'lowerCorner', 1)
    gml_content = gml_content.replace('coord><gml:X', 'upperCorner', 1)
    gml_content = gml_content.replace('Y></gml:coord', 'upperCorner', 1)
    gml_content = gml_content.replace('</gml:X><gml:Y>', ' ')
    gml_content = gml_content.replace('fid', 'gml:id')
    gml_content = gml_content.replace('outerBoundaryIs', 'exterior')
    gml_content = gml_content.replace('coordinates', 'posList')
    gml_content = gml_content.replace(',', ' ')
    gml_content,_ = re.subn(r'\n\n', r'\n', gml_content)
    gml_id = re.search('ogr:(\w{32})\s',gml_content).group(1)
    gml_content = gml_content.replace(gml_id, segment.segment)
    # Schrijf het omgezette bestand weg naar een .gml
    with open(gml_filename, 'w', newline='') as f:
        f.write(gml_content)  

I saw you can add additional keyword to the fiona.open function that then will be passed to GDAL, but I couldn't figure out what to add to make it work. I like to be able to save my file as a 3.1.1 .gml using Fiona.

Something like this (see the XSD), but It didn't worked out :

fiona.open(gml_bytes, 'w', driver='GML', schema=schema, crs=from_epsg(31370), XSD='http://schemas.opengis.net/gml/3.1.1/base/feature.xsd')
@rbuffat
Copy link
Contributor

rbuffat commented Oct 16, 2020

Fiona is a wrapper library around GDAL. If no additional arguments are passed to fiona.open, the driver uses its default behavior, which potentially can be different depending on the version of GDAL Fiona is built against.

GDAL documentation for the GML driver can be found here: https://gdal.org/drivers/vector/gml.html#vector-gml

There seems to be a FORMAT parameter:

FORMAT: This can be set to :

GML3 in order to write GML files that follow GML 3.1.1 SF-0 profile.
GML3Deegree in order to produce a GML 3.1.1 .XSD schema, with a few variations with respect to what is recommended by GML3 SF-0 profile, but that will be better accepted by some software (such as Deegree 3).
GML3.2in order to write GML files that follow GML 3.2.1 SF-0 profile.

This parameter should be passed to fiona.open:

with fiona.open(gml_bytes, 'w', driver='GML', schema=schema, crs=from_epsg(31370), FORMAT="GML3") as f:

Disclaimer: I did not test if this works.

@rebot
Copy link
Author

rebot commented Oct 16, 2020

Hi,

I've tried your solution, but it doesn't work. It keeps turning it into GML version 2 files. I've checked the GDAL version Fiona is using and it's 3.1.3. I guess GML3 is supported by this version of GDAL, but Fiona is not able to properly pass the options provided in the fiona.open function.

Any further suggestions?

@rbuffat
Copy link
Contributor

rbuffat commented Oct 17, 2020

It looks as there is a bug. Currently, only layer creation options are taken into account, and not dataset creation options such as FORMAT.

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

No branches or pull requests

3 participants