[R-sig-Geo] Setting FID values when saving GML files with writeOGR
Karl Ove Hufthammer
karl at huftis.org
Wed Sep 7 10:11:25 CEST 2011
Roger Bivand wrote:
> Indeed, the driver internals are a possible solution, see:
>
> http://www.gdal.org/ogr/drv_gml.html
>
> which suggests that some reading is possible in OGR >= 1.8.0, but that
> writing will first come with OGR >= 1.9.0, that is the development
> version, I believe.
Thanks. I should have mentioned that I actually had read that page (and
that was the reason I adding ‘fid’ and ‘gml_id’ columns), but couldn’t
get the solution mentioned there to work.
Now I have actually managed to get it to work, on Linux. The Windows build
of rgdal apparently uses a version of rgdal < 1.8.0. Is there a way to
get information on which version (from inside R)?
My Linux system has OGR 1.8.1, which doesn’t support writing FID values
(this will come in OGR 1.9.0), but *does* support writing gml:id values,
which is used in GML 3. GML 2 is the default, which means that to specifiy
the feature ID, you have to:
1. Add a column called ‘gml_id’ to your spatial object.
2. Add the argument ‘dataset_options="FORMAT=GML3"’ to the writeOGR call.
This creates a GML 3 document with features identified by the values in the
‘gml_id’ column (in the GML file they’re named ‘gml:id’).
Note that neither RGDAL nor GDAL does any validation on the values in the
‘gml_id’ column. To ensure that they’re valid, you can use the following
‘Name’ regexp:
NameStartChar=":_A-Za-
z\uC0-\uD6\uD8-\uF6\uF8-\u2FF\u370-\u37D\u37F-\u1FFF\u200C-\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD"
NameChar=paste(NameStartChar,"0-9\uB7\u0300-\u036F\u203F-\u2040F.-", sep="")
Name=paste("^[", NameStartChar, "][", NameChar, "]*$", sep="")
Example:
grepl(Name,c("F23","23F","F","",":ABC","-ABC","A-B.","-AB","_.-",".-_","\u200C","\u200E"))
Note that the regexp is missing the character range \u10000-\uEFFFF, which I
couldn’t get R to recognise on my (UTF-8 Linux) system. But if R doesn’t
recognize the characters, they won’t appear in any strings, so this shouldn’t
be a problem … :)
You also need to run ‘anyDuplicated’ to check that all the values are unique.
--
Karl Ove Hufthammer
More information about the R-sig-Geo
mailing list