My question refers to an old solution which is provided in this post:How do I find out if a given lat lon is in a KML Linear ring using shapely?
Using python3 I needed to update the code somewhat to get this thing working altogether.
New source:
from urllib.request import urlopenfrom xml.etree import ElementTreeimport keytreefrom shapely.geometry import Point, shape# Parse the KML docdoc = urlopen("http://pleiades.stoa.org/places/638753/kml").read()#doc = open('d:/Helmond_wijken.kml', 'rb')#doc = open('d:/Helmond_wijken.kml').read()tree = ElementTree.fromstring(doc)kmlns = tree.tag.split('}')[0][1:]# Find all Polygon elements anywhere in the docelems = tree.findall(".//{%s}Polygon" % kmlns)# Here's our point of interestp = Point(28.722144580890763, 37.707799701548467)# Filter polygon elements using this lambda (anonymous function)# keytree.geometry() makes a GeoJSON-like geometry object from an# element and shape() makes a Shapely object of that.hits = filter( lambda e: shape(keytree.geometry(e)).contains(p), elems )print (list(hits))# Output: []Now the output i'm getting is as follows:
[http://www.opengis.net/kml/2.2}Polygon' at 0x000000000383EF48>]
I need some direction whether or not this is a correct output (and how to interpret this). If this is not the correct output how to improve it.
In the end I want to create a file containing points and the polygons they are found in e.g.
Point x1,y1, polygon name1
Point x1,y1, polygon name2
Point x2,y2, polygon name2
Point x3,y3, polygon name1
Point x3,y3, polygon name3
But this is a bit further down the road..;-)
Be gentle with me.. & thank you for your time!
Maurice
أكثر...
Using python3 I needed to update the code somewhat to get this thing working altogether.
New source:
from urllib.request import urlopenfrom xml.etree import ElementTreeimport keytreefrom shapely.geometry import Point, shape# Parse the KML docdoc = urlopen("http://pleiades.stoa.org/places/638753/kml").read()#doc = open('d:/Helmond_wijken.kml', 'rb')#doc = open('d:/Helmond_wijken.kml').read()tree = ElementTree.fromstring(doc)kmlns = tree.tag.split('}')[0][1:]# Find all Polygon elements anywhere in the docelems = tree.findall(".//{%s}Polygon" % kmlns)# Here's our point of interestp = Point(28.722144580890763, 37.707799701548467)# Filter polygon elements using this lambda (anonymous function)# keytree.geometry() makes a GeoJSON-like geometry object from an# element and shape() makes a Shapely object of that.hits = filter( lambda e: shape(keytree.geometry(e)).contains(p), elems )print (list(hits))# Output: []Now the output i'm getting is as follows:
[http://www.opengis.net/kml/2.2}Polygon' at 0x000000000383EF48>]
I need some direction whether or not this is a correct output (and how to interpret this). If this is not the correct output how to improve it.
In the end I want to create a file containing points and the polygons they are found in e.g.
Point x1,y1, polygon name1
Point x1,y1, polygon name2
Point x2,y2, polygon name2
Point x3,y3, polygon name1
Point x3,y3, polygon name3
But this is a bit further down the road..;-)
Be gentle with me.. & thank you for your time!
Maurice
أكثر...