-
Notifications
You must be signed in to change notification settings - Fork 19
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
Document format x to GML conversion options #132
Comments
As far as I know, it is not that straightforward to convert shapefiles, geojson, .. to GML with python. At least, But, I think there would be some ways to provide some useful functionality, assuming the user has a GeoDataFrame (so a shapefile, geojson, ... read with fiona/geopandas).
There is actually a rather easy way to create small snippets of gml, and that is by using GDAL/OGR directly:
(and where the Only, it is apparently not really recommended to combine usage of BTW, this looks like really nice functionality. Is it something that would eventually maybe fit in OWSLib ? (like you now use the filter expressions system already from OWSLib) |
Thanks for the explanation! Do I get it right that you should not combine |
Discussed on code spring May 2019: do not mix imports of rasterio with gdal/fiona/geopandas as indicated in the references above |
Small correction: I think it needs ot be "do not mix imports of rasterio/fiona/geopandas with gdal". It's only gdal's own python bindings that don't work with the ecosystem of geo packages around rasterio, fiona, etc |
Just thought about this, there is actually a way to let fiona write GML, by explicitly saying to fiona that it is OK to do so.
gives
|
Hi, I've made a conversion from Shapefile to gml that can be used to filter pyDOV. 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 Workflow# In[1]: Laad de projectzone is - projectzone bestaat uit verschillende polygonen
segmenten = gpd.read_file(os.path.join('hulpmiddelen','projectzone','segmenten.shp'))
# In[2]: Filter de geometrie en zet om naar een lijst punten
# 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)
# Voer de functie uit voor elke rij uit de DataFrame
segmenten.apply(exporteer_vorm, axis=1) ProblemI have to convert the .gml from version 2 to version 3.1.1 using some additional lines of code. You can add some additional keyword in the Something like this (see the 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') Can somebody help me with this? Afterwards, I'll fork pyDOV and add an example. |
sorry, no experience yet, maybe ask at fiona first? |
There seems to be a bug in Fiona, an issue has been made to fix this! Once the issue has been resolved, I'll provide pydov with an example. EDIT: Fix has been implemented and is scheduled to be included in the next version of Fiona that will be released in November |
Yes, an example for using a Shapefile as a spatial filter in pydov would be very interesting. SHP is (unfortunately) still widely used, so adding this to the documentation would benefit a lot of users. Maybe we can enlarge the scope of this tutorial notebook to "spatial querying" and include it there? We can then refer to that notebook in the main documentation on spatial querying. |
After #309 get merged, this issue can be closed. |
With the addition of the location query options #129 we support the usage of GML to query on location. However, users will probably have other file formats (shapefile, geojson,...) to use within the location search context.
Without supporting more formats within the pydov package, we could point the user to some other references or code examples on how to deal with other file formats.
@jorisvandenbossche, taking into account your experiences on spatial matter, do you have pointers or ideas that we can include in the documentation on how to convert xxxx to GML? with xxxx equal to shapefile, geojson,... (feature file types)
The text was updated successfully, but these errors were encountered: