Skip to content

Instantly share code, notes, and snippets.

@AndreLester
Last active September 15, 2015 21:29
Show Gist options
  • Save AndreLester/36bc44fd703b53c53496 to your computer and use it in GitHub Desktop.
Save AndreLester/36bc44fd703b53c53496 to your computer and use it in GitHub Desktop.
Create lines using a point from a shapefile and getting the ID's and angles from a CSV file
import math
from qgis.core import QgsFeature
pointLayerName = "point" #change to whatever name of point layer is
lineLayerName = "lines" #change to whatever name of line layer is
#be sure to draw one point, leave line layer empty
#find layer objects by name
layers = QgsMapLayerRegistry.instance().mapLayers()
for name, layer in layers.iteritems():
print "%s %s" % (layer.name(),layer)
if layer.name() == lineLayerName:
lineLayer = layer
if layer.name() == pointLayerName:
pointLayer = layer
assert pointLayer != None
assert lineLayer != None
#find point by ID
expr = QgsExpression( "id=1" )
it = pointLayer.getFeatures( QgsFeatureRequest( expr ) )
for f in it:
geompoint = f.geometry()
pivot = geompoint.asPoint()
break
#generate lines from provided degrees (LOBs)
lineLayer.beginEditCommand("Add Spokes")
provider = lineLayer.dataProvider()
lobfeat = lineLayer.getFeatures()
linefeatures=[]
for x in lobfeat:
lobvals = x.attributes()
lob = lobvals[1] #searches column 2 of line feature for LOBs
geomspoke = QgsGeometry.fromPolyline([QgsPoint(0.0,0.0),QgsPoint(0.0,10.0)])
geomspoke.rotate(float(ang),QgsPoint(0.0,0.0))
geomspoke.translate(pivot.x(),pivot.y())
print geomspoke.exportToWkt(6)
spokefeat = QgsFeature()
spokefeat.setGeometry(geomspoke)
linefeatures.append(spokefeat)
provider.addFeatures(linefeatures)
lineLayer.commitChanges()
lineLayer.endEditCommand()
print "Done!"
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment