I am wondering what is the expression that truncate a given name to the 10 characters name used in ESRI shapefiles when an object is saved as shapefile with the function writeOGR in R. For example, the attribute field from a spatial dataframe "Ablepharus kitaibelii" is converted to "Ablphrk" when saved as shapefile, how the truncation is done?
1 Answer
sf::st_write
in R uses the abbreviate
function in its base package to create unique names of the right length for a shapefile.
If I have a spatial object with these two long names (plus "geometry"):
> names(p)
[1] "longnamehereplease" "longnamehereaswell" "geometry"
then writing them gives:
> st_write(p,"p.shp")
Writing layer `p' to data source `p.shp' using driver `ESRI Shapefile'
Writing 10 features with 2 fields and geometry type Point.
Warning message:
In abbreviate_shapefile_names(obj) :
Field names abbreviated for ESRI Shapefile driver
and the names in the shapefile will be:
> abbreviate(names(p)[1:2] minlength=5)
longnamehereplease longnamehereaswell
"lngnmhrp" "lngnmhrs"
Note that its the writing program's job to truncate the field names and different software does it differently. The well-known GDAL/OGR conversion tool for example,gGiven a geopackage with the same long names, when converting to shapefile:
$ ogr2ogr short.shp p.gpkg
Warning 6: Normalized/laundered field name: 'longnamehereplease' to 'longnamehe'
Warning 6: Normalized/laundered field name: 'longnamehereaswell' to 'longname_1'
and note the laundered field names are different to the ones created by R.
Starting with version 1.7, the OGR Shapefile driver tries to generate unique field names. Successive duplicate field names, including those created by truncation to 10 characters, will be truncated to 8 characters and appended with a serial number from 1 to 99.
This software must be something else.ogr2ogr
creates different names to R'sst_write
.