2

I have a shapefile with coordinates listed in Easting and Northing in the attribute table. Is it possible to create two new fields called lat and long and populate them with values corresponding to easting and northing using ogr2ogr?

The idea is to try and get this to work without having to use a specialist GIS software and of course automation.

1 Answer 1

4

if you don't already have lat long coordinates in you table you can compute them using the -sql option and the sqlite dialect:

ogr2ogr centroid.shp input.shp -dialect sqlite -sql "SELECT ST_X(ST_Centroid(geometry)) as long, ST_Y(ST_Centroid(geometry)) as lat, ST_Centroid(geometry) FROM input"

If they aready exist and you just want to rename them or add them, use a similar SQL statement and alter the column name.

eg:

ogr2ogr centroid.shp input.shp -dialect sqlite -sql "SELECT easting_col as long, northing_col as lat, geometry FROM input"
6
  • Hi Thomas. I was just going to provide my own workaround which is not as neat as yours. My workaround involved transforming the CRS to Lat/Long and then using GEOMETRY=AS_XY to populate the lat/long. Yours looks far more sleek and efficient! Thanks.
    – VGu
    Commented Feb 15, 2016 at 0:11
  • @Thomas- Sorry to bother but I just tried running this command and it gives me an error message which reads unrecognised token ":". I guess this has to do with handling spaces. I tried double quotes but that doesn't work. Any ideas?
    – VGu
    Commented Feb 15, 2016 at 0:28
  • Not quite sure. I tested the first statement and it works for me. I only replaced the input file name with the generic input.shp name. Can you post the statement you are trying to use. This will make it easier to understand what is going wrong
    – Thomas
    Commented Feb 15, 2016 at 0:37
  • This is what I'm trying to use ogr2ogr -f "ESRI Shapefile" "H:\Mapping Projects\Karen Smith\SHP Files\Existing Community Facililties Points centroids.shp" "H:\Mapping Projects\Karen Smith\SHP Files\Existing Community Facililties Points.shp" -dialect sqlite -sql "SELECT ST_X(ST_Centroid(geometry)) as long, ST_Y(ST_Centroid(geometry)) as lat, ST_Centroid(geometry) FROM H:\Mapping Projects\Karen Smith\SHP Files\Existing Community Facililties Points.shp"
    – VGu
    Commented Feb 15, 2016 at 1:19
  • 1
    In the SQL Statement try to replace the full path of the shape file with the shp name without file extension eg. FROM "Existing Community Facilities Points". The whitespaces in your filename might also cause some issues. Better to rename your shapefile and replace whitespaces with underscores (existing_community_facilities_points)
    – Thomas
    Commented Feb 15, 2016 at 1:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.