【问题标题】:Converting KML into Postgis- Django/Python将 KML 转换为 Postgis - Django/Python
【发布时间】:2017-01-25 19:39:19
【问题描述】:

我对 Django 还很陌生,并且已经坚持了一个多星期。我正在自学如何编码并尝试使用地图数据制作个人网站。我正在尝试使用从 html 上的 javascript 地图中提取的数据,这些数据被格式化为 kml/xml 字符串并存储在文本框中。

我在使用映射资源(geodjango、postgis)获取 kml 字符串并将其放入数据库时​​遇到问题。文档似乎非常有限,并且在互联网上到处搜索。

以下是我所拥有的细分:

HTML:

(地图)

将 kml 字符串放入文本区域的按钮

<input name="mapdata" type="button" value="Generate KML" onclick="convert_to_kml"/>
<textarea id="kmlString" style="blahblah"></textarea>

型号:

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`

观看次数:

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''')

我想将所有的 kml 数据转换为 postgis- 试图保留所有的 kml 数据,例如样式、名称和描述。

GDAL 提供 ogr2ogr 来进行查询,但我不明白如何正确地将其实现到要处理的视图中。这是正确的方向吗?我听说过名为 libkml 的 GDAL 驱动程序(将 kml 处理成 postgis)——但仍然不确定如何正确使用它。

或 我做这一切都错了吗?!?!有没有办法通过postgis的输入输出来保留样式?

我希望这很清楚我想要达到的目标。

我会感谢任何帮助!!!而且,如果这占用了您太多宝贵的时间,我可以为您提供资金。这显示了我的绝望。哈哈!

这是文本框中的示例 kml 字符串(我已经测试并工作):

<?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>

【问题讨论】:

    标签: django postgis kml geodjango


    【解决方案1】:

    您可以使用DataSource 读取 KML 文件,并从中提取几何图形:

    from django.contrib.gis.gdal.datasource import DataSource
    
    ds = DataSource("data.kml")
    geoms = ds[0].get_geoms(geos=True)
    

    但是,这不适用于您上面的 KML,因为多边形未正确关闭(这对于 KML 可能没问题,但对于 GEOS 则不行。删除 geos=True 将正确地将其加载为 GDAL 几何图形)。试试其他KML examples here

    样式信息随 DataSource 丢失。您也许可以使用 libkml 或 lxml 提取它。

    如果您的数据包含点、线和多边形的混合,请考虑使用GeometryCollection 字段而不是MultiPolygon 字段。

    【讨论】:

    • 感谢您的回复!我会在接下来的几天内试一试,然后再回复你!
    猜你喜欢
    • 2023-03-04
    • 2018-01-22
    • 2013-04-09
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    相关资源
    最近更新 更多