Last active
September 15, 2015 21:29
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment