Extracting Point Coordinates From Layers In Qgis

Viewing Layer Attribute Tables

The first step in extracting coordinates from a vector layer in QGIS is to view the layer’s attribute table. The attribute table displays the data attributes associated with each feature in the layer, including any coordinate fields.

To view the attribute table for a layer:

  1. Right click on the layer name in the Layers Panel and select Open Attribute Table
  2. Alternatively, with the layer selected, click the Open Attribute Table button in the main toolbar

This will open a new window displaying the layer’s attribute table. Here you can see all the attribute fields or columns associated with the features in that layer.

Identifying Coordinate Columns

Once the attribute table is open, you need to identify which fields contain the X and Y coordinate values for each point feature. Common names include:

  • x and y
  • lon and lat – for longitude and latitude values
  • easting and northing – Cartesian coordinate system

The data type of coordinate fields is usually decimal or real numbers. Scan through the attribute table for fields matching these parameters.

Exporting the Attribute Table

With the target coordinate fields identified, the next step is exporting the full attribute table to a file for processing.

Export options include:

  • Comma Separated Values (.csv)
  • HTML Table (.html)
  • MS Excel Format (.xls)

.csv format works well for ease of access and filtering of the coordinate data.

To export the full attribute table:

  1. In the attribute table window, click the Export button
  2. Select file type, name and location to save
  3. Check to export the full table with all fields
  4. Click OK to export

This will save a copy of the attribute table external to QGIS for the next stage.

Filtering to Extract Coordinates

Once the full attribute table is available as an external file, the coordinate extraction process begins.

Open the exported .csv in a spreadsheet editor or text application. Scan through again to verify the target coordinate fields.

Options for filtering out the coordinates include:

  • Manually copying the x and y columns into a new file
  • Using find/replace to remove all columns except the coordinates
  • Importing the table into an external program and filtering there

The key outcome is a new .csv file with only the feature id and x, y coordinate columns intact.

As the coordinate values are now accessible independently from QGIS, a number of applications and workflows are possible.

Example Code for Extracting Coordinates

The steps covered enable simple coordinate extraction through the QGIS user interface.

More complex automated coordinate harvesting requires writing some custom Python scripts.

Key libraries and functions include:

  • PyQGIS – accessing QGIS layers and attribute data from Python
  • re – regular expressions module
  • csv – functionality for reading and writing csv files

Here is some example code to extract x,y coordinates:


import csv

# Open csv file for writing 
csvfile = open('coordinates.csv','w')
writer = csv.writer(csvfile)

# Load vector layer
layer = QgsVectorLayer('/path/to/layer.shp','layer','ogr') 

# Iterate through each feature
features = layer.getFeatures()
for f in features:

  # Access attribute map
  attributes = f.attributes()

  # Write x,y values to csv 
  row = [attributes[0], attributes[1]] 
  writer.writerow(row)

csvfile.close()  

This iterates the layer features, accessing the attribute record for each and specifically the x and y coordinate columns (indexes 0 and 1). These are written row-by-row into the output csv.

Many variations are possible – calculating geometry centroids, filtering features, handling different coordinate formats etc.

Using PyQGIS to Programmatically Access Coordinates

Expanding on the Python approach, the PyQGIS library contains deeper functionality for interrogating vector layer coordinates and geometries.

Key classes and methods include:

  • QgsFeature
  • QgsGeometry
  • QgsPoint
  • .asPoint()

Example extracting easting/northing and writing centroids to csv:


import csv 

layer = QgsVectorLayer('/path','layer','ogr')

outfile = open('centroids.csv','w')
writer = csv.writer(outfile)

feats = layer.getFeatures()
for f in feats:
  
  # Get centroid point 
  centroid = f.geometry().centroid().asPoint()  
  
  # Access easting/northing coordinates
  easting = centroid.x()
  northing = centroid.y()  

  # Write to csv
  row = [easting,northing]
  writer.writerow(row)

outfile.close()

This provides direct access to the underlying coordinate instances for advanced geospatial calculations and measurements.

Real-World Applications of Extracted Coordinates

Some examples of applying harvested QGIS coordinate data include:

  • Feasibility analysis – load coordinates into mathematical models
  • Asset tracking – synchronize locations with GPS monitoring systems
  • Spatial joins – combine attributes from other datasets based on proximity calculations
  • Route optimization – sequence coordinates to generate efficient routes
  • Geofencing – trigger actions when coordinates enter/exit defined regions

The core workflow enables conversion of rich QGIS geometries into tangible coordinate pairs for reuse across information systems.

Leave a Reply

Your email address will not be published. Required fields are marked *