Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import os
- from qgis.core import *
- import matplotlib.pyplot as plt
- from matplotlib.path import Path
- import matplotlib.patches as patches
- LATITUDE = 1.29306
- LONGITUDE = 103.856
- QgsApplication.setPrefixPath("/usr", True)
- QgsApplication.initQgis()
- layer=QgsVectorLayer("/home/shubham/SMART/zones/mtz1092p.shp", "mtz1092p", "ogr")
- if not layer.isValid():
- print "Layer failed to load!"
- provider = layer.dataProvider()
- def printFeatures():
- feat = QgsFeature()
- #print feat
- allAttrs = provider.attributeIndexes()
- provider.select(allAttrs)
- while provider.nextFeature(feat):
- geom = feat.geometry()
- #print "Feature ID %d: " % feat.id(),
- x = geom.asPolygon()
- numPts = 0
- for ring in x:
- numPts += len(ring)
- #print "Polygon: %d rings with %d points" % (len(x), numPts)
- #if len(x) > 1:
- # print "Feature ID %d has more than one ring" % feat.id()
- if len(x) == 0:
- print "Feature ID %d has no ring" % feat.id()
- else:
- print "Polygon for feature ID %d => " %(feat.id()), x[0]
- attrs = feat.attributeMap()
- #for (k, attr) in attrs.iteritems():
- # print "%d: %s" % (k, attr.toString())
- #def findFeatureId(lat, long):
- def findFeatureId(point):
- feat = QgsFeature()
- allAttrs = provider.attributeIndexes()
- provider.select(allAttrs)
- while provider.nextFeature(feat):
- geom = feat.geometry()
- x = geom.asPolygon()
- if len(x) == 0:
- print "Feature ID %d has no ring" % feat.id()
- else:
- # if x[0][0][0] != x[0][len(x) - 1][0] and x[0][0][1] != x[0][len(x) - 1][1]:
- # print "Polygon at feature ID %d violates the sameness rule\n" %(feat.id())
- # else:
- # print "First element => ", x[0][0], "\nLast element => ", x[0][len(x) - 1], "\n"
- codes = []
- codes.append(Path.MOVETO)
- for i in range (0, len(x[0]) - 2):
- codes.append(Path.LINETO)
- codes.append(Path.CLOSEPOLY)
- # print "Length = ", len(codes), codes
- path = Path(x[0], codes)
- if (path.contains_point(point, None, 0.0)):
- print "Point contained in feature ID %d" %feat.id()
- # print "Polygon for feature ID %d => " %(feat.id()), x[0], "\n"
- if __name__ == "__main__":
- crsSrc = QgsCoordinateReferenceSystem(4326) # WGS84
- crsDest = QgsCoordinateReferenceSystem(3414)# SVY21
- xform = QgsCoordinateTransform(crsSrc, crsDest)
- #pt = xform.transform(QgsPoint(LATITUDE, LONGITUDE))
- pt = xform.transform(QgsPoint(LONGITUDE, LATITUDE))
- findFeatureId(pt)
- #findFeatureId(1.275927800000000000, 103.804237500000000000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement