Skip to content

Instantly share code, notes, and snippets.

@mlaloux
Created March 7, 2013 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlaloux/5109683 to your computer and use it in GitHub Desktop.
Save mlaloux/5109683 to your computer and use it in GitHub Desktop.
Création d'une ligne à partir d'une direction (en degrés) et d'une distance dans QGIS - Creating a line from a direction (in degrees) and a distance in QGIS (QGIS 2.8I)
'''For QGIS 2.8
Martin Laloux, 2012'''
from PyQt4.QtCore import *
from numpy import *
class distance(object):
def __init__(self,a,angle,distance):
self.a = a.asPoint()
self.xori = self.a[0]
self.yori = self.a[1]
self.angle = angle
self.distance = distance
self.xfinal =0
self.yfinal = 0
self.dist_x = 0
self.dist_y = 0
self.fet = QgsFeature()
self.vl = QgsVectorLayer("LineString", "temporary_lines", "memory")
self.pr = self.vl.dataProvider()
@property
def resultat(self):
self.dist_x, self.dist_x = (self.distance * sin(radians(self.angle)),self.distance * cos(radians(self.angle)))
self.xfinal, self.yfinal = (self.xori + self.dist_x, self.yori + self.dist_x)
return self.xfinal, self.yfinal
@property
def x(self):
return self.resultat[0]
@property
def y(self):
return self.resultat[1]
@property
def final(self):
return QgsGeometry.fromPoint(QgsPoint(self.x,self.y))
@property
def trace(self):
fields = { 0 : QgsField("first", QVariant.Int) }
self.pr.addAttributes( [ QgsField("first", QVariant.Int)])
self.fet.setGeometry(QgsGeometry.fromPolyline( [ QgsPoint(self.xori , self.yori ), QgsPoint(self.x, self.y) ] ))
self.pr.addFeatures( [ self.fet ] )
self.vl.updateExtents()
self.vl.updateFieldMap()
QgsMapLayerRegistry.instance().addMapLayer(self.vl)
>>> point_dep = QgsGeometry.fromPoint(QgsPoint(231009.737,110767.821))
>>> point_arriv = distance(point_dep, 20, 100)
>>> point_arriv.final
<qgis.core.QgsGeometry object at 0x12afd9200>
>>> point_arriv.resultat
(231043.93901433257, 110861.79026207859)
>>> point_arriv.x
231043.93901433257
>>> point_arriv.y
110861.79026207859
>>> point_arriv.trace
>>> point_arriv2 = distance(point_arriv.final, 80, 200)
>>> point_arriv2.x
231240.900564935
>>> point_arriv2.y
110896.51989761197
>>> point_arriv2.trace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment