【问题标题】:How to reduce the coordinates decimal in a local kml file using pykml?如何使用pykml减少本地kml文件中的坐标小数?
【发布时间】:2014-03-18 14:44:15
【问题描述】:

我有一个包含超过 100 万个字符的单元格的 kml 文件。我想将小数位数从 12 减少到 3。我导入了 lxml 和 pykml。

import pykml
from pykml.helpers import set_max_decimal_places
file1=open('\United States divisions. Level 2.kml')
from os import path
#set_max_decimal_places(file1, max_decimals={'longitude':3,'latitude':3,})

我得到了这个错误:

     39         index_no = 0 # longitude is in the first position
     40         # modify <longitude>
---> 41         for el in doc.findall(".//{http://www.opengis.net/kml/2.2}longitude"):
     42             new_val = round(float(el.text), max_decimals[data_type])
     43             el.getparent().longitude = K.longitude(new_val)

AttributeError: 'file' 对象没有属性 'findall'

【问题讨论】:

    标签: python-2.7 lxml kml findall pykml


    【解决方案1】:

    这是因为您将 kml 作为一个文件加载,并且需要先对其进行解析。来自:http://pythonhosted.org/pykml/tutorial.html

    In [29]: from pykml import parser
    
    ...
    
    In [40]: kml_file = path.join( \
       ....:      '../src/pykml/test', \
       ....:      'testfiles/google_kml_developers_guide', \
       ....:      'complete_tour_example.kml')
    
    In [44]: with open(kml_file) as f:
       ....:      doc = parser.parse(f)
    

    然后你可以调用:

       ....:      set_max_decimal_places(doc, max_decimals={'longitude':3,'latitude':3,})
    

    更新:

    使用上面的代码,我认为这也应该有效:(from here)

    file1=open('\United States divisions. Level 2.kml')
    doc = fromstring(file1.read(), schema=Schema("ogckml22.xsd"))
    set_max_decimal_places(doc, max_decimals=3)
    

    更新:2

    只需从评论中提取您使用的最终代码:

    from lxml import etree 
    from pykml.helpers import set_max_decimal_places 
    from pykml import parser 
    
    with open('\United States divisions. Level 2.kml') as f: 
        doc=parser.parse(f) 
        set_max_decimal_places(doc, max_decimals={'longitude':3,'latitude':3,}) 
    
    print etree.tostring(doc, pretty_print=True) 
    outfile = file(file.rstrip('.py')+'.kml','w') 
    outfile.write(etree.tostring(doc, pretty_print=True))
    

    【讨论】:

    • 所以 - 在第一种情况下,我希望您只需将文件写回磁盘?在第二个中,我希望您有一个无效的 KML 属性集 - 尝试验证您的文档。如果是这种情况,只需使用第一种方法,因为您不应该需要拥有有效的 KML 来进行这些更新或使用它。
    • 我在您最后一条评论之前的评论是在您删除/重新添加之前解决此评论。
    • 我建议您从链接文档中的示例开始,只需使用您在脚本中设置的字符串即可。一旦工作正常,请尝试加载文件的摘录(来自巨大文档的新测试文件中的一些记录)并确保其按预期工作。然后继续处理整个事情。
    • 谢谢你,马修,我正在努力实现你的观点。我正在使用nsmap,但仍然没有成功。
    • 终于成功了。 from lxml import etree from pykml.helpers import set_max_decimal_places from pykml import parser with open('C:\Users\Mehrnoosh Oghbaie\Desktop\Python\United States Divisions.Level 2.kml') as f: doc=parser.parse(f ) set_max_decimal_places(doc, max_decimals={'longitude':3,'latitude':3,}) print etree.tostring(doc, pretty_print=True) outfile = file(file.rstrip('. py')+'.kml','w') outfile.write(etree.tostring(doc, pretty_print=True))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多