I have files in ESRI Shapefile format. May I know how can I use QGIS or python gdal library to extract the latitude and longitude? I have installed QGIS and add layer of the shapefile into it, I can view the (lat, long) on the map but I do not know how to extract the (lat,long) out.. Do advice me how to extract the (lat,long) out. TQ
How to convert ESRI shapefile format using QGIS or python gdal library to extract lat lon
1k views Asked by Sam At
2
There are 2 answers
0
On
If you want to do it with python, you don't need gdal for that. I find geopandas (with Shapely) a much simpler library for working with shapefiles. Though at first it might take a few minutes longer than clicking through QGIS interface, learning how to manipulate shapefiles in Python is very powerful knowledge in a long run. Here is a sample of your task, performed with python:
import geopandas as gpd
# read shapefile (attributes and geometry) into geodataframe
shp = gpd.read_file("sample_shapefile.shp")
# convert "geometry" field into separate X and Y fields
shp["X"]=shp["geometry"].apply(lambda geom: geom.x)
shp["Y"]=shp["geometry"].apply(lambda geom: geom.y)
#save X,Y into csv file
shp[["X","Y"]].to_csv("coords.csv",header=True,index=False,sep=",")
Whick will produce the desired output in coords.csv:
X,Y
425070.0,80330.0
408390.0,81150.0
405370.0,85860.0
410880.0,82850.0
415310.0,80630.0
418610.0,80350.0
Right click your layer, select "Save As". The dialog "Save vector layer as..." will open. Select the following options:
Format: Comma Separated Value [CSV]
File name: Select a file name
CRS: EPSG:4326, WGS84
In the lower part, expand "Select fields to export" and click "Deselect All" if you just want the coordinates, or check the available items (columns from the attribute table) if you need place names, etc. By choosing WGS84 as the output CRS you'll receive your coordinates in decimal degrees (latitude, longitude).
Example output: