I am fairly new to Django and have been stuck on this for over a week. I am teaching myself how to code and trying to make a personal site with mapping data. I am trying to use the data I have pulled from a javascript map on html, which is formatted into a kml/xml string and stored in a text box.
I have trouble using the mapping resources (geodjango, postgis) to take the kml string and placing it in the database. Documentation seems to be very limited and searched high and low on the internet.
Here is the break down of what I have:
Html:
(map)
Button to place kml string into text area
<input name="mapdata" type="button" value="Generate KML" onclick="convert_to_kml"/>
<textarea id="kmlString" style="blahblah"></textarea>
Models:
class JobMap(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=200)
mpoly = models.GeometryCollectionField(srid=4326)
objects = models.GeoManager()
def __str__(self):
return self.name`
Views:
def map_import(request):
if request.method == 'POST':
map_data = request.POST.get('kmlString')
#Create a temp file of the string (I think it needs to be a temp file in order for ogr2ogr can read it)
f = request.FILES[map_data]
temp = tempfile.NamedTemporaryFile(delete=False)
temp.write(f.read())
temp.close()
JobMap.objects.raw('''ogr2ogr - f "PostgreSQL"
PG:"host=localhost user=user dbname=database password=password"
temp.name''')
I want to convert all of the kml data into postgis- Trying to preserve all the kml data such as styles, name and description.
GDAL offers ogr2ogr to do queries, but I cannot understand how to properly implement this into views to process. Is this the right direction? I have heard of GDAL driver named libkml (processes kml into postgis)- but still not sure how to use this properly.
OR am I doing this all wrong?!?! Is there no way to preserve the styles with input and output of postgis?
I hope this is clear as to what I am trying to achieve.
I will APPRECIATE any help!!! And, if it takes too much of your valuable time, I can fund for your time. This shows my desperation. Haha!
Here is the example kml string in text box (I have tested and works):
<?xml version="1.0" encoding="UTF-8" ?> <kml >xmlns="http://www.opengis.net/kml/2.2"> <Document> <Style id="117025122580"> <PolyStyle> <color>4c262ED3</color> </PolyStyle> <LineStyle> <color>4c262ED3</color> <width><![CDATA[3]]></width> </LineStyle> </Style> <Placemark> <name><![CDATA[]]></name> <description><![CDATA[]]></description> <styleUrl>#117025122580</styleUrl> <Polygon> <outerBoundaryIs> <LinearRing> <coordinates> -113.54164123535156,53.58616198075974,0 -113.40980529785156,53.57678609766098,0 -113.42559814453125,53.50823829699392,0 -113.58283996582031,53.53436483388852,0 -113.59451293945312,53.5559886770827,0 </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </Placemark> <Style id="117025122581"> <PolyStyle> <color>4c262ED3</color> </PolyStyle> <LineStyle> <color>4c262ED3</color> <width><![CDATA[3]]></width> </LineStyle> </Style> <Placemark> <name><![CDATA[]]></name> <description><![CDATA[]]></description> <styleUrl>#117025122581</styleUrl> <Polygon> <outerBoundaryIs> <LinearRing> <coordinates> -113.642578125,53.47678344938643,0 -113.55400085449219,53.47678344938643,0 -113.55400085449219,53.5282428724319,0 -113.642578125,53.5282428724319,0 </coordinates> </LinearRing> </outerBoundaryIs> </Polygon> </Placemark> <Style id="117025122582"> <IconStyle> <scale>1</scale> </IconStyle> </Style> <Placemark> <name><![CDATA[]]></name> <description><![CDATA[]]></description> <styleUrl>#117025122582</styleUrl> <Point> <coordinates>-113.47984313964844,53.61264779961176,0</coordinates> </Point> </Placemark> </Document> </kml>
You can use DataSource to read a KML file, and extract geoms from it:
from django.contrib.gis.gdal.datasource import DataSource
ds = DataSource("data.kml")
geoms = ds[0].get_geoms(geos=True)
However, this does not work with your KML above since the polygons are not closed properly (This might be OK with KML, but not with GEOS. Removing geos=True will load it as GDAL geometries correctly). Try some other KML examples here.
Style info is lost with DataSource. You might be able to extract it using libkml or lxml.
If your data contains a mixture of points, lines and polygons, consider using a GeometryCollection field instead of a MultiPolygon field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With