如何使用Python将XML编码为ESRI Shapefile?

时间:2014-04-11 12:57:11

标签: python xml encoding shapefile esri

我将以下Python脚本准备到this XML到ESRI Shapefiles中。该脚本的起点是this post

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Requires pyshp: https://pypi.python.org/pypi/pyshp
#
# Conversion for http://daten.berlin.de/datensaetze/liste-der-gedenktafeln-berlin
# File: http://gedenktafeln-in-berlin.de/index.php?id=31&type=123
#

from xml.etree import ElementTree
from datetime import datetime
import shapefile
import os


def get_value(list, index, default):
    value = list[index]
    if value is None:
        value = default
    else:
        value = value.text
        if value is None:
            value = default
        else:
            # value = value.replace(u'\xdf', u' ')
            value = value.encode("utf-8")
    return value



def add_shape(writer, attributes):
    uid = int(get_value(attributes, 0, 0))
    url = get_value(attributes, 1, "")
    tstamp = get_value(attributes, 2, None)
    if tstamp is not None:
        tstamp = datetime.strptime(tstamp, '%d.%m.%Y')
    ortsteil = get_value(attributes, 3, "")
    strasse = get_value(attributes, 4, "")
    longitude = get_value(attributes, 5, None)
    latitude = get_value(attributes, 6, None)
    Name = get_value(attributes, 7, "")
    inhalt = get_value(attributes, 8, "")
    erlauterung = get_value(attributes, 9, "")
    swo = get_value(attributes, 10, "")
    literatur = get_value(attributes, 11, "")
    personen = get_value(attributes, 12, "")
    entfernt = int(get_value(attributes, 13, 0))

    if longitude is not None or latitude is not None:
        longitude = float(longitude)
        latitude = float(latitude)

        # Fix interchanged coordinates
        temp = 0
        if longitude > latitude:
            temp = latitude
            latitude = longitude
            longitude = temp

        # Add coordinates
        writer.point(longitude, latitude)

    # Add attributes
    writer.record(uid, url, tstamp, ortsteil, strasse, Name, inhalt, erlauterung, swo, literatur, personen, entfernt)




xml_file = 'gedenktafeln.xml'
shape_file = 'gedenktafeln.shp'
projection = 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'

tree = ElementTree.parse(xml_file)
writer = shapefile.Writer(shapefile.POINT)

writer.field('uid', fieldType = 'N', size = 5, decimal = 0)
writer.field('url', fieldType = 'C', size = 255)
writer.field('tstamp', fieldType = 'C', size = 19) # Type 'D' seems to be not working here.
writer.field('ortsteil', fieldType = 'C', size = 200)
writer.field('strasse', fieldType = 'C', size = 200)
writer.field('Name', fieldType = 'C', size = 255)
writer.field('inhalt', fieldType = 'C', size = 255)
writer.field('erlauterung', fieldType = 'C', size = 255)
writer.field('swo', fieldType = 'C', size = 255)
writer.field('literatur', fieldType = 'C', size = 255)
writer.field('personen', fieldType = 'C', size = 255)
writer.field('entfernt', fieldType = 'N', size = 1, decimal = 0)

root = tree.getroot()
shapes = root.getchildren()

for shape in shapes:
    attributes = shape.getchildren()
    add_shape(writer, attributes)

try:
    writer.save(shape_file)
except Exception, e:
    print "ortsteil: " + ortsteil
    print "strasse: " + strasse
    print "Name: " + Name
    print "inhalt: " + inhalt
    print "erlauterung: " + erlauterung
    print "swo: " + swo
    print "literatur: " + literatur
    print "personen: " + personen
    print "entfernt: " + entfernt
    raise

# create the PRJ file
with open(os.path.splitext(shape_file)[0] + os.extsep + 'prj', 'w') as prj:
    prj.write(projection)

还有一些问题:

  1. 特殊字符未按预期编码。我不相信value = value.encode("utf-8") is correct - 请对此发表评论。 // Thanks to ptrv
  2. url已被切断。 // Resolved by blah238
  3. 坐标不在柏林,而是在波茨坦。这可能是原始数据中的错误。或者我使用了错误的projection设置。
  4. 为了检查数据,我将它们转换为GeoJSON you can view them online

2 个答案:

答案 0 :(得分:1)

我只能对您的第二个问题发表评论,即您的url值被切断。您需要指定字段长度,例如writer.field('url', size=255)

答案 1 :(得分:1)

对于第一个问题,请尝试删除行value = value.replace(u'\xdf', u' ')

这对我有用,我成功地生成了包含特殊字符属性的shapefile。