Monday, February 26, 2018

Export Shapefile Attribute to Text File using PyQGIS

Hello there,

Lets use QGIS python scripting (PyQGIS) to create a text file of attributes from a shapefile.

Here I loaded onto QGIS a point layer representing capitals of Nigeria states with four attributes namely: "State_name", "Capital", "Area" and "Population".




Open the python console and with the point layer selected on the layers panel, enter the code below on the console to create an active layer object.
layer = iface.activeLayer()





Use a "for loop" to get the layers feature by calling the "getFeatures()" method on the layer object as seen below...
for f in layer.getFeatures():
  print f
This will print all the feature object within the layer (each feature will be a point representing a state capital).



As seen in the output, each line contains a reference to a feature within the layer. The reference to the feature is stored in the f variable. We can use the f variable to access the attributes of each feature. Type the following to print the "State_name", "Capital", "Area" and "Population" for each point feature.
for f in layer.getFeatures():
 print f["State_name"], f["Capital"], f["Area"], f["Population"]


That is how to access the attribute of an active layer.

Now, lets access the coordinates of the point features. This can be accessed by calling the "geometry()" method which returns a geometry object that we can store in the variable geom. You can run "asPoint()" method on the geometry object to get the x and y coordinates of the point. If your feature is a line or a polygon, you can use "asPolyline()" or "asPolygon()" methods respectively.

for f in layer.getFeatures():
  geom = f.geometry()
  print geom.asPoint()



To get only the x cordinate of the feature, you can call the "x()" function on the point object and get its x coordinate. Similarly for the x cordinate of the feature, you can call the "y()" function on the point object and get its y coordinate.

for f in layer.getFeatures():
  geom = f.geometry()
  print geom.asPoint().x()

From the above codes, we have all that is required to export our shapefile layer attributes to a text file. Putting the code together would look like this...

for f in layer.getFeatures():
  geom = f.geometry()
  print '%s, %s, %s, %s, %f, %f' % (f["State_name"], f["Capital"], f["Area"], f["Population"],
         geom.asPoint().y(), geom.asPoint().x())

The %s and %f are just the normal python string formatting placeholders for strings and floats.


To write the output to a text file, we need to add few lines of code to open a text file and write to it as seen below...

output_file = open('C:\\Users\\user\\Desktop\\Terra img\\State_capital.txt', 'w')
for f in layer.getFeatures():
  geom = f.geometry()
  line = '%s, %s, %s, %s, %f, %f\n' % (f["State_name"], f["Capital"], f["Area"], f["Population"], geom.asPoint().y(), geom.asPoint().x())
  unicode_line = line.encode('utf-8')
  output_file.write(unicode_line)
output_file.close()

The text file will look like below...




That is it!

Note: You can do this manually by editing the attribute with x and y columns and then save the layer as a CSV file. However, above is an automated procedure which is more effective if you have to do the same task for a large number of files/layers.

No comments:

Post a Comment