Advertisement
shubhamgoyal

Getting the ID of the polygon which circumscribes a point

May 30th, 2013
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. import sys
  2. import os
  3. from qgis.core import *
  4. import matplotlib.pyplot as plt
  5. from matplotlib.path import Path
  6. import matplotlib.patches as patches
  7.  
  8. LATITUDE = 1.29306
  9. LONGITUDE = 103.856
  10.  
  11. QgsApplication.setPrefixPath("/usr", True)
  12. QgsApplication.initQgis()
  13.  
  14. layer=QgsVectorLayer("/home/shubham/SMART/zones/mtz1092p.shp", "mtz1092p", "ogr")
  15. if not layer.isValid():
  16.   print "Layer failed to load!"
  17. provider = layer.dataProvider()
  18.  
  19. def printFeatures():
  20.   feat = QgsFeature()
  21.   #print feat
  22.   allAttrs = provider.attributeIndexes()
  23.   provider.select(allAttrs)
  24.   while provider.nextFeature(feat):
  25.     geom = feat.geometry()
  26.     #print "Feature ID %d: " % feat.id(),
  27.     x = geom.asPolygon()
  28.     numPts = 0
  29.     for ring in x:
  30.       numPts += len(ring)
  31.     #print "Polygon: %d rings with %d points" % (len(x), numPts)
  32.     #if len(x) > 1:
  33.     #  print "Feature ID %d has more than one ring" % feat.id()
  34.     if len(x) == 0:
  35.       print "Feature ID %d has no ring" % feat.id()
  36.     else:
  37.       print "Polygon for feature ID %d => " %(feat.id()), x[0]
  38.     attrs = feat.attributeMap()
  39.     #for (k, attr) in attrs.iteritems():
  40.     #  print "%d: %s" % (k, attr.toString())
  41.      
  42. #def findFeatureId(lat, long):
  43. def findFeatureId(point):
  44.   feat = QgsFeature()
  45.   allAttrs = provider.attributeIndexes()
  46.   provider.select(allAttrs)
  47.   while provider.nextFeature(feat):
  48.     geom = feat.geometry()
  49.     x = geom.asPolygon()
  50.     if len(x) == 0:
  51.       print "Feature ID %d has no ring" % feat.id()
  52.     else:
  53.     #  if x[0][0][0] != x[0][len(x) - 1][0] and x[0][0][1] != x[0][len(x) - 1][1]:
  54.     #    print "Polygon at feature ID %d violates the sameness rule\n" %(feat.id())
  55.     #  else:
  56.     #    print "First element => ", x[0][0], "\nLast  element => ", x[0][len(x) - 1], "\n"
  57.       codes = []
  58.       codes.append(Path.MOVETO)
  59.       for i in range (0, len(x[0]) - 2):
  60.         codes.append(Path.LINETO)
  61.       codes.append(Path.CLOSEPOLY)
  62.     #  print "Length = ", len(codes), codes
  63.       path = Path(x[0], codes)
  64.       if (path.contains_point(point, None, 0.0)):
  65.         print "Point contained in feature ID %d" %feat.id()
  66.     #  print "Polygon for feature ID %d => " %(feat.id()), x[0], "\n"
  67.      
  68.  
  69. if __name__ == "__main__":
  70.   crsSrc = QgsCoordinateReferenceSystem(4326) # WGS84
  71.   crsDest = QgsCoordinateReferenceSystem(3414)# SVY21
  72.   xform = QgsCoordinateTransform(crsSrc, crsDest)
  73.   #pt = xform.transform(QgsPoint(LATITUDE, LONGITUDE))
  74.   pt = xform.transform(QgsPoint(LONGITUDE, LATITUDE))
  75.   findFeatureId(pt)
  76.   #findFeatureId(1.275927800000000000, 103.804237500000000000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement