[Qgis-developer] python plugin - layer intersection ?

Michaël Douchin michael.douchin at laposte.net
Tue Jan 15 03:32:39 EST 2008


Hi list
I am playing with the python bindings.... I would like to know if we can 
use python to develop a layer intersection plugin.
For example, I have 2 shp layers :
* mypoly = polygon layer (id_poly, name, area, valuea, valueb, etc.)
* mypoint = point layer (id_point, name, value1, value2, etc.)

Is it possible with python to test for each point if it intersects one 
of the polygons, and then give a point layer like this:
myresult(id_result, id_point, id_poly, valuea, value2)
In this case, I keep the id of each source layer, and choose to keep 
also one value of each layer. Each new point has the same geometry of 
the source point

For those who know postgis, it would be a query like this
SELECT p1.id_poly, p1.valuea, p2.id_point, p2.value2, p2.the_geom
FROM mypoly p1, mypoint p2
WHERE INTERSECTS (p1.the_geom, p2.the_geom)

Thanks in advance for answers.

PS: I can use the geometric algorytm like this instead, but it would be 
useless if Qgis core has intersection capabilities:
*****
  # is target point inside a 2D polygon?
  #   poly = 2 dimenson list with all the coords of each polygon,
  #   x (horizontal)=  X of the point to test
  #   y (vertical) = Y of the point to test
  def inpoly( poly, xt,  yt):
    inside = False #valeur que retournera la fonction
    npoints = len(poly)  #calcul du nombre de lignes du tableau = nombre 
de sommets du polygone
    if (npoints < 3): #si il y a moins que 3 sommets, ce n'est pas un 
polygone --> on arrete
      return(0)
    xold=poly[npoints-1][0]
    yold=poly[npoints-1][1]

    for i in range(0,npoints):
      xnew=poly[i][0]
      ynew=poly[i][1]
      if (xnew > xold) :
        x1=xold
        x2=xnew
        y1=yold
        y2=ynew
      else :
        x1=xnew
        x2=xold
        y1=ynew
        y2=yold
      # edge "open" at one end
      if ( (xnew < xt) == (xt <= xold) and (yt-y1)*(x2-x1)  < 
(y2-y1)*(xt-x1) ):
        inside= not inside

      xold=xnew
      yold=ynew
    return inside
*****



More information about the Qgis-developer mailing list