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?
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)  "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.