Hello,
Is it possible to develop a tool to import KML files and automatically create geofences?
Exemple:
https://gis.stackexchange.com/questions/114932/converting-kml-to-wkt-using-geodjango-or-python
import tempfile, json
from StringIO import StringIO
from osgeo import ogr
from django.contrib.gis.geos import GEOSGeometry
testdata = """<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Simple placemark</name>
<description>Attached to the ground. Intelligently places itself
at the height of the underlying terrain.</description>
<Point>
<coordinates>-122.0822035425683,37.42228990140251</coordinates>
</Point>
</Placemark>
</kml>"""
temp = tempfile.NamedTemporaryFile()
temp.write(testdata)
temp.flush()
driver = ogr.GetDriverByName('KML')
datasource = driver.Open(temp.name)
layer = datasource.GetLayer()
feat = layer.GetNextFeature()
feat_json = json.loads(feat.ExportToJson())
feat_json['geometry']['coordinates'] = feat_json['geometry']['coordinates'][0:2]
geom = json.dumps(feat_json['geometry'])
pnt = GEOSGeometry(geom)
print pnt
POINT (-122.0822035425682941 37.4222899014025074)
I'm sure that it's possible. You can make a script to do pretty much anything.
Hello,
Is it possible to develop a tool to import KML files and automatically create geofences?
Exemple:
https://gis.stackexchange.com/questions/114932/converting-kml-to-wkt-using-geodjango-or-python
import tempfile, json from StringIO import StringIO from osgeo import ogr from django.contrib.gis.geos import GEOSGeometry # Set testdata string testdata = """<?xml version="1.0" encoding="UTF-8"?> <kml xmlns="http://www.opengis.net/kml/2.2"> <Placemark> <name>Simple placemark</name> <description>Attached to the ground. Intelligently places itself at the height of the underlying terrain.</description> <Point> <coordinates>-122.0822035425683,37.42228990140251</coordinates> </Point> </Placemark> </kml>""" # Create tempfile for ogr driver read temp = tempfile.NamedTemporaryFile() temp.write(testdata) temp.flush() # Read XML file into python driver = ogr.GetDriverByName('KML') datasource = driver.Open(temp.name) layer = datasource.GetLayer() feat = layer.GetNextFeature() # Convert ogr feature to GEOSGeometry feat_json = json.loads(feat.ExportToJson()) feat_json['geometry']['coordinates'] = feat_json['geometry']['coordinates'][0:2] geom = json.dumps(feat_json['geometry']) pnt = GEOSGeometry(geom) print pnt POINT (-122.0822035425682941 37.4222899014025074)