3

My goal is to plot circles on QGIS canvas by taking distances in kilometers from MySQL base. This is code I use:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from math import *

def funkcijaCrtanje(iface):

import MySQLdb
import sys

connection = MySQLdb.connect(host = "127.0.0.1", user = "root", passwd = "********", db = "mydb")
cursor = connection.cursor()
cursor.execute("select column from table")
data = cursor.fetchall()

edge = data[0]


def drawCircle(rb, center, edgePoint, N):
    r = sqrt(center.sqrDist(edgePoint))
    for itheta in range(N+1):
        theta = itheta*(2.0 * (pi)/N)
        # addPoint finkcija dodaje tocke QgsGeometry objektu i povezuje ih linijom
        rb.addPoint(QgsPoint(center.x()+r*(cos(theta)),center.y()+r*(sin(theta))))

class Kruznice(QgsMapTool):
    def __init__(self, canvas):
        QgsMapTool.__init__(self, canvas)
        self.canvas = canvas

        self.segment = 100

    def canvasPressEvent(self, e):
        point = self.toMapCoordinates(e.pos())
        n = 10
        color = [Qt.red, Qt.blue, Qt.green, Qt.yellow]
        for i, edge in enumerate(data):
            rb = QgsRubberBand(self.canvas)
            rb.setColor(color[i])
            rb.setWidth(2)
            ed = QgsPoint(point.x()+float(edge[0])*n*100, point.y()+1000)
            drawCircle(rb, point, ed, self.segment)


krug = Kruznice(iface.mapCanvas())
iface.mapCanvas().setMapTool(krug)

I'm not sure that I plot real distances on my map by using this. How can I solve this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.