Created
March 7, 2013 16:58
-
-
Save mlaloux/5109682 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 master with the new Python API)
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
'''QGIS master with the new Python API | |
Martin Laloux 2013''' | |
from PyQt4.QtCore import * | |
from numpy import * | |
from PyQt4.QtCore import * | |
from numpy import * | |
a = QgsGeometry.fromPoint(QgsPoint(122.989235,13679.083853)) | |
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() | |
self.v2 = QgsVectorLayer("Point", "temporary_points", "memory") | |
@property | |
def resultat(self): | |
self.dist_x, self.dist_y = (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_y) | |
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): | |
#self.vl | |
fields = { 0 : QgsField("first", QVariant.Int) } | |
#pr = vl.dataProvider() | |
self.pr.addAttributes( [ QgsField("first", QVariant.Int)]) | |
self.fet.setGeometry(QgsGeometry.fromPolyline( [ QgsPoint(self.xori , self.yori ), QgsPoint(self.x, self.y) ] )) | |
self.fet.setAttributeMap( { 0 : QVariant(1)} ) | |
self.pr.addFeatures( [ self.fet ] ) | |
self.vl.updateExtents() | |
self.vl.updateFieldMap() | |
QgsMapLayerRegistry.instance().addMapLayers([self.vl]) | |
@property | |
def origine(self): | |
self.pr = self.v2.dataProvider() | |
self.fet = QgsFeature() | |
self.pr.addAttributes( [ QgsField("statut", QVariant.String)] ) | |
self.fet.setGeometry( QgsGeometry.fromPoint(QgsPoint(self.xori,self.yori)) ) | |
self.fet.setAttributeMap( { 0 : QVariant("origine")} ) | |
self.pr.addFeatures( [ self.fet ] ) | |
self.v2.updateExtents() | |
self.v2.updateFieldMap() | |
QgsMapLayerRegistry.instance().addMapLayers([self.v2]) | |
@property | |
def arrive(self): | |
self.pr = self.v2.dataProvider() | |
self.fet = QgsFeature() | |
#self.pr.addAttributes( [ QgsField("statut", QVariant.String)] ) sinon 2 col | |
self.fet.setGeometry( QgsGeometry.fromPoint(QgsPoint(self.x,self.y)) ) | |
self.fet.setAttributeMap( { 0 : QVariant("arrive")} ) | |
self.pr.addFeatures( [ self.fet ] ) | |
self.v2.updateExtents() | |
self.v2.updateFieldMap() | |
#QgsMapLayerRegistry.instance().addMapLayer(self.v2) # il faut chipoter pour arriver au résultat | |
>>> 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