Python 的 Geodaisy 库转换带负号坐标的WKT时的Bug

Geodaisy是一个Python库,用于创建由类型和坐标表示的地理对象,并在各种标准和表示之间进行转换,包括GeoJSON、Well - Known Text和Python的__geo_interface__协议,它被其他地理库使用。源码地址:https://gitee.com/mzfly/geodaisy.git
在使用其 converters.wkt_to_geo_interface()方法时转换带符号坐标的wkt字符串时,发现其转换的结果跟预期的不一样:
例如:

python;gutter:true;</p> <blockquote> <p>wkt = "MULTILINESTRING ((3146.2134801800566 2556399.9823166174, -2611.53319329879 2564044.0076796883))" converters.wkt_to_geo_interface(wkt) {'type': 'MultiLineString', 'coordinates': (3146.2134801800566, 2556399.9823166174, -2611.53319329879, 2564044.0076796883)}</p> </blockquote> <pre><code> 我们看到 coordinates 的值并不是我们想要的结果: ;gutter:true;
{‘type’: ‘MultiLineString’, ‘coordinates’: [(3146.2134801800566, 2556399.9823166174), (-2611.53319329879, 2564044.0076796883)]}

原因是 wkt_to_geo_interface 方法源码采用正则匹配,并没有考虑带负数坐标的情况(或者根本就不考虑),但是在地方坐标系处理偏移的过程中是肯定有出现负值坐标的情况的。

解决方法:

修改wkt_to_geo_interface方法源码的正则匹配(匹配”-“号和数字):

1 def wkt_to_geo_interface(wkt):
 2     # type: (str) -> dict
 3     """Converts a WKT string to a geo_interface dictionary."""
 4     try:
 5         wkt_type, coords = re.split(r'(?', wkt)
 6
 7         geo_type = type_translations[wkt_type]
 8
 9         # Clean up the strings so they'll covert correctly
10         if geo_type in {'Polygon', 'MultiLineString', 'MultiPolygon'}:
11             # coords = re.sub(r'(?

12             coords = re.sub(r'(?', ')), ((', coords)   # modif by 举个栗子
13
14         # Pairs of coordinates must be enclosed in parentheses
15         # coords = re.sub(r'(?

16         coords = re.sub(r'(?', '), (', coords) # modif by 举个栗子
17
18         # Coordinates within parentheses must be separated by commas
19         # coords = re.sub(r'(?

20         coords = re.sub(r'(?', ', ', coords)    # # modif by 举个栗子
21
22         # Now we can turn the string into a tuple or a tuple of tuples
23         coords = literal_eval(coords)
24
25         coords = reformat_coordinates(coords, 'geo_interface')  # type: ignore  # noqa: E501
26
27         # If we only have a simple polygon (no hole), the coordinate array
28         # won't be deep enough to satisfy the GeoJSON/geo_interface spec, so
29         # we need to enclose it in a list.

30         numbers = {float, int}
31         if geo_type == 'Polygon' and type(coords[0][0]) in numbers:
32             coords = [coords]  # type: ignore
33         elif geo_type == 'MultiPolygon' and type(coords[0][0][0]) in numbers:
34             coords = [coords]  # type: ignore
35
36     except Exception:
37         raise ValueError('{} is not a WKT string'.format(wkt))
38
39     return {'type': geo_type, 'coordinates': coords}

Original: https://www.cnblogs.com/mzfly/p/15570909.html
Author: 橘个栗子
Title: Python 的 Geodaisy 库转换带负号坐标的WKT时的Bug

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/510732/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • pytest allure 生成报告过程

    allure 下载地址:Releases · allure-framework/allure2 · GitHub 下载好后配置环境变量执行: allure –versi…

    Python 2023年9月11日
    034
  • 手把手带你入门 API 开发

    引言 在本文中,您将学习如何使用Flask、SQLite 3(轻易数据库)和 JSON 创建用于数据通信的 REST API。 本文使用 4 个最常用的 HTTP 动词:GET、P…

    Python 2023年8月9日
    073
  • Web自动化之Pytest测试框架

    pytest是一个python的单元测试框架,可以用于自动化测试中。 用例规则 pytest命令执行测试时,如果我们不指定具体的文件,PyTest默认从当前路径及其所有子目录中搜索…

    Python 2023年9月10日
    056
  • Django基础

    Django诞生于2003年,06年加入BSD,成为开源Web框架 1.两点注意: 实际的项目开发中,最好使用带LTS的Django版本。带LTS的版本被官方长期支持的版本,官方会…

    Python 2023年5月24日
    077
  • Scrapy shell的使用【python爬虫入门进阶】(22)

    抵扣说明: 1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。 Original: https://blo…

    Python 2023年10月2日
    038
  • Pandas大数据清洗实战之二:牛刀小试

    1、数据准备 csv是以纯文本形式存储的表格数据,接下来讲述使用pandas读取和操作csv中的数据首先准备csv文件,内容如下: white,red,blue,pink,blac…

    Python 2023年8月18日
    052
  • python显示图像太大_matplotlib说图像太大了

    Original: https://blog.csdn.net/weixin_29056145/article/details/114438823Author: 李枝蔚Title:…

    Python 2023年9月4日
    043
  • 记录一次关于python内存泄露的排查!

    一般情况下只有需要长期运行的项目才会去关注内存的增长情况,即使是很小部分的内存泄露经过长期的运行仍然会产生很大的隐患。 python本身也是支持垃圾的自动回收的,但是在特定的情况下…

    Python 2023年11月5日
    037
  • matplotlib自定义风格

    使用matplotlib进行绘图时,经常遇到个问题,就是总是要花大量代码对绘图的格式进行设置。虽然可以将同一类绘图的代码保存以后使用,但是看着这么长一串用来设置格式就很不爽。 一、…

    Python 2023年9月3日
    084
  • pytest框架前置和后置方法

    pytest框架前置和后置方法 模块级别:setup_module() 和 teardown_module() 方法执行规则 函数级别:setup_function() 和 tea…

    Python 2023年9月13日
    055
  • python怎么选取不连续的列_python – Pandas从数据帧中选择不连续的列

    如果要连接df列的子选择,请使用pd.concat: pd.concat([comb.ix[:,0:1],comb.ix[:,17:342]], axis=1) 只要索引匹配,那么…

    Python 2023年8月17日
    068
  • [Clicknium]自动操作网页和应用

    Clicknium Clicknium是一个Python界面自动化库,可以非常简单直观地使用它操作浏览器和Windows平台APP。 官网:https://www.clickniu…

    Python 2023年5月24日
    082
  • 超详细Anaconda安装教程

    文章目录 * – 附Anaconda彻底卸载教程 – 一、Anaconda下载(官网和清华源) – + 1.1、Anaconda官网首页地址 +…

    Python 2023年7月31日
    043
  • 人体行为识别研究综述

    摘要 行为识别是计算机视觉领域意义重大的热点研究问题,它经历了从手工设计特征表征到深度学习特征表达的发展过程。从传统行为识别模型和深度学习模型两方面,对行为识别发展历程中产生的主流…

    Python 2023年9月30日
    045
  • pyenv安装及使用教程

    pyenv安装及使用教程 建议在 mac或者 linux这两种系统环境下使用 pyenv 安装 下载最新版的pyenv git clone https://github.com/p…

    Python 2023年5月24日
    0115
  • FCN网络解析

    1 FCN网络介绍 FCN(Fully Convolutional Networks,全卷积网络) 用于图像语义分割,它是首个端对端的针对像素级预测的全卷积网络,自从该网络提出后,…

    Python 2023年8月2日
    042
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球