Python处理Point, MultiPolygon, Polygon, LineString等Geo地理形状数据

本文主要是介绍Python处理Point, MultiPolygon, Polygon, LineString等Geo地理形状数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

依赖库

from shapely import Polygon, Point, LineString
import re

自编写函数

1. 将 字符串Polygon/MultiPolygon数据 转换为 坐标数据

输入输出

调用方式fork_MULTIPOLYGON_POLYGON_to_POLYGONLIST(str_polygon)

输入字符串形式的Polygon/MultiPolygon数据(支持三种格式)

输出:一个双层嵌套列表,顶层列表的元素是一个个POLYGON,底层列表的元素是一个POLYGON里的一个个坐标点

功能描述:将 Polygon/MultiPolygon 转化为 一个列表,这个列表里有多个POLYGON列表,每个POLYGON列表中的经纬度坐标用元组表示

样例

格式一:

MULTIPOLYGON (((-73.97535722895489 40.79358795668606, -73.97550491442216 40.7932206693501, -73.97571880885164 40.79329900902139, -73.97557038329896 40.793666024933465, -73.97535722895489 40.79358795668606)), ((-73.97552582561218 40.79316866412968, -73.97558333155439 40.79302564935169, -73.97579761926521 40.79310413236238, -73.97573982501896 40.793247041771274, -73.97552582561218 40.79316866412968)))

输出

[
[(-73.97535722895489, 40.79358795668606), (-73.97550491442216, 40.7932206693501), (-73.97571880885164, 40.79329900902139), (-73.97557038329896, 40.793666024933465), (-73.97535722895489, 40.79358795668606)],
[(-73.97552582561218, 40.79316866412968), (-73.97558333155439, 40.79302564935169), (-73.97579761926521, 40.79310413236238), (-73.97573982501896, 40.793247041771274), (-73.97552582561218, 40.79316866412968)]
]

格式二:

POLYGON ((-73.97759524402524 40.7618905, -73.97760683658178 40.76183484508208, -73.9776404794912 40.76178463805529, -73.97769287955111 40.76174479353323, -73.97775890747857 40.76171921177534, -73.9778321 40.761710396902316, -73.97790529252143 40.76171921177534, -73.97797132044889 40.76174479353323, -73.9780237205088 40.76178463805529, -73.97805736341822 40.76183484508208, -73.97806895597476 40.7618905, -73.97805736341822 40.76194615491792, -73.9780237205088 40.76199636194471, -73.97797132044889 40.76203620646677, -73.97790529252143 40.762061788224656, -73.9778321 40.76207060309768, -73.97775890747857 40.762061788224656, -73.97769287955111 40.76203620646677, -73.9776404794912 40.76199636194471, -73.97760683658178 40.76194615491792, -73.97759524402524 40.7618905))

输出

[
[(-73.97759524402524, 40.7618905), (-73.97760683658178, 40.76183484508208), (-73.9776404794912, 40.76178463805529), (-73.97769287955111, 40.76174479353323), (-73.97775890747857, 40.76171921177534), (-73.9778321, 40.761710396902316), (-73.97790529252143, 40.76171921177534), (-73.97797132044889, 40.76174479353323), (-73.9780237205088, 40.76178463805529), (-73.97805736341822, 40.76183484508208), (-73.97806895597476, 40.7618905), (-73.97805736341822, 40.76194615491792), (-73.9780237205088, 40.76199636194471), (-73.97797132044889, 40.76203620646677), (-73.97790529252143, 40.762061788224656), (-73.9778321, 40.76207060309768), (-73.97775890747857, 40.762061788224656), (-73.97769287955111, 40.76203620646677), (-73.9776404794912, 40.76199636194471), (-73.97760683658178, 40.76194615491792), (-73.97759524402524, 40.7618905)]
]

格式三:

POLYGON ((-73.98635378499995 40.764514924000025, -73.98637627999994 40.76448596900008, -73.98622909499994 40.764422067000055, -73.98624709199999 40.76439890300003, -73.98608441399995 40.764328275000025, -73.98613390299994 40.76426457500003, -73.98599446599997 40.764204035000034, -73.98598546799997 40.76421561700005, -73.98560588899994 40.764050816000065, -73.98557889499995 40.76408556100006, -73.98553241599996 40.76406538100008, -73.98555491099995 40.764036427000065, -73.98557815099997 40.764046517000054, -73.98560064599997 40.76401756200005, -73.98557740699994 40.76400747300005, -73.98567188699997 40.76388586200005, -73.98639231199996 40.76419864800005, -73.98637881499997 40.76421602000005, -73.98653374599996 40.76428328600008, -73.98654724199997 40.76426591300003, -73.98671766799998 40.76433990400005, -73.98672666499994 40.76432832300003, -73.98679638399994 40.764358591000075, -73.98678288799994 40.76437596400007, -73.98689133999994 40.76442305000006, -73.98690483699994 40.76440567700007, -73.98709075499994 40.76448639400007, -73.98707275899994 40.76450955800004, -73.98712698499997 40.76453310000005, -73.98711348899997 40.76455047300004, -73.98733039399997 40.764644643000054, -73.98736188599997 40.764604106000036, -73.98740836599995 40.764624285000025, -73.98742186299995 40.76460691200003, -73.98777046199996 40.76475825500006, -73.98740154699993 40.765233118000026, -73.98737055999999 40.76521966400003, -73.98736156199993 40.76523124600004, -73.98676506899994 40.76497227900006, -73.98678756299995 40.76494332400006, -73.98646995199994 40.76480543100007, -73.98646095399994 40.76481701300003, -73.98620531599994 40.764706025000066, -73.98619181899994 40.76472339800006, -73.98589744899994 40.76459559400007, -73.98605041699994 40.76439870100006, -73.98625182799998 40.764486146000024, -73.98624282899993 40.76449772900003, -73.98635128199999 40.76454481400003, -73.98634678299999 40.76455060600006, -73.98630804999993 40.76453378900004, -73.98624506299996 40.76461486200003, -73.98626055599993 40.76462158900006, -73.98622006499994 40.76467370700004, -73.98628203699997 40.76470061300006, -73.98630003399995 40.76467744900003, -73.98626904799994 40.76466399700007, -73.98630953899993 40.76461187800004, -73.98636376499996 40.76463542100004, -73.98631427599997 40.764699122000025, -73.98638399499998 40.76472939100006, -73.98652796399995 40.764544080000064, -73.98650472399999 40.764533990000075, -73.98647772999993 40.76456873600006, -73.98635378499995 40.764514924000025), (-73.98698650299997 40.764820600000064, -73.98700449999995 40.76479743600004, -73.98715943199994 40.76486470000003, -73.98712793899995 40.76490523700005, -73.98715117899997 40.76491532700004, -73.98711518699997 40.76496165400005, -73.98714617299999 40.764975108000044, -73.98725864899995 40.76483033300008, -73.98698751699999 40.76471262100006, -73.98697401999993 40.76472999400005, -73.98678035399996 40.76464591300004, -73.98676685699996 40.76466328600003, -73.98680095699996 40.76475940500006, -73.98683156999994 40.76475333500008, -73.98698650299997 40.764820600000064))

输出

/home/lizhicheng/UrbanKG/venv/bin/python /home/lizhicheng/UrbanKG/src/test.py 
[
[(-73.98635378499995, 40.764514924000025), (-73.98637627999994, 40.76448596900008), (-73.98622909499994, 40.764422067000055), (-73.98624709199999, 40.76439890300003), (-73.98608441399995, 40.764328275000025), (-73.98613390299994, 40.76426457500003), (-73.98599446599997, 40.764204035000034), (-73.98598546799997, 40.76421561700005), (-73.98560588899994, 40.764050816000065), (-73.98557889499995, 40.76408556100006), (-73.98553241599996, 40.76406538100008), (-73.98555491099995, 40.764036427000065), (-73.98557815099997, 40.764046517000054), (-73.98560064599997, 40.76401756200005), (-73.98557740699994, 40.76400747300005), (-73.98567188699997, 40.76388586200005), (-73.98639231199996, 40.76419864800005), (-73.98637881499997, 40.76421602000005), (-73.98653374599996, 40.76428328600008), (-73.98654724199997, 40.76426591300003), (-73.98671766799998, 40.76433990400005), (-73.98672666499994, 40.76432832300003), (-73.98679638399994, 40.764358591000075), (-73.98678288799994, 40.76437596400007), (-73.98689133999994, 40.76442305000006), (-73.98690483699994, 40.76440567700007), (-73.98709075499994, 40.76448639400007), (-73.98707275899994, 40.76450955800004), (-73.98712698499997, 40.76453310000005), (-73.98711348899997, 40.76455047300004), (-73.98733039399997, 40.764644643000054), (-73.98736188599997, 40.764604106000036), (-73.98740836599995, 40.764624285000025), (-73.98742186299995, 40.76460691200003), (-73.98777046199996, 40.76475825500006), (-73.98740154699993, 40.765233118000026), (-73.98737055999999, 40.76521966400003), (-73.98736156199993, 40.76523124600004), (-73.98676506899994, 40.76497227900006), (-73.98678756299995, 40.76494332400006), (-73.98646995199994, 40.76480543100007), (-73.98646095399994, 40.76481701300003), (-73.98620531599994, 40.764706025000066), (-73.98619181899994, 40.76472339800006), (-73.98589744899994, 40.76459559400007), (-73.98605041699994, 40.76439870100006), (-73.98625182799998, 40.764486146000024), (-73.98624282899993, 40.76449772900003), (-73.98635128199999, 40.76454481400003), (-73.98634678299999, 40.76455060600006), (-73.98630804999993, 40.76453378900004), (-73.98624506299996, 40.76461486200003), (-73.98626055599993, 40.76462158900006), (-73.98622006499994, 40.76467370700004), (-73.98628203699997, 40.76470061300006), (-73.98630003399995, 40.76467744900003), (-73.98626904799994, 40.76466399700007), (-73.98630953899993, 40.76461187800004), (-73.98636376499996, 40.76463542100004), (-73.98631427599997, 40.764699122000025), (-73.98638399499998, 40.76472939100006), (-73.98652796399995, 40.764544080000064), (-73.98650472399999, 40.764533990000075), (-73.98647772999993, 40.76456873600006), (-73.98635378499995, 40.764514924000025)], 
[(-73.98698650299997, 40.764820600000064), (-73.98700449999995, 40.76479743600004), (-73.98715943199994, 40.76486470000003), (-73.98712793899995, 40.76490523700005), (-73.98715117899997, 40.76491532700004), (-73.98711518699997, 40.76496165400005), (-73.98714617299999, 40.764975108000044), (-73.98725864899995, 40.76483033300008), (-73.98698751699999, 40.76471262100006), (-73.98697401999993, 40.76472999400005), (-73.98678035399996, 40.76464591300004), (-73.98676685699996, 40.76466328600003), (-73.98680095699996, 40.76475940500006), (-73.98683156999994, 40.76475333500008), (-73.98698650299997, 40.764820600000064)]
]

函数代码
def extract_content_from_MULTIPOLYGON(string):pattern = r'\(\((.*?)\)\)'  # 定义正则表达式模式,使用非贪婪匹配matches = re.findall(pattern, string)  # 使用findall函数查找所有匹配项if not matches:  # 如果没有匹配项,返回空列表return []else:return matchesdef MULTIPOLYGON_to_POLYGONLIST(multpolygon):prefix = 'MULTIPOLYGON'multpolygon = multpolygon[len(prefix):]multpolygon = multpolygon[2:-1]list_polygon_str = extract_content_from_MULTIPOLYGON(multpolygon)if not list_polygon_str:return []else:list_POLYGON = []for polygon_str in list_polygon_str:list_point_str = polygon_str.split(',')POLYGON = []for point in list_point_str:if point[0] == ' ':point_str = point[1:]else:point_str = pointpoint_list = point_str.split(' ')# dest_str = point_list[0] + ',' + point_list[1]longitude = float(point_list[0])latitude = float(point_list[1])Tuple = (longitude, latitude)POLYGON.append(Tuple)# POLYGON.append(dest_str)list_POLYGON.append(POLYGON)return list_POLYGONdef fork_MULTIPOLYGON_POLYGON_to_POLYGONLIST(input):if input.startswith('MULTIPOLYGON'):return MULTIPOLYGON_to_POLYGONLIST(input)else:prefix = 'POLYGON'input = input[len(prefix):]input = input[3:-2]if '(' in input or ')' in input:input = '(' + input + ')'arr = input.split('), (')list_str = []for item in arr:new_item = itemif item[0] == '(':new_item = item[1:]if item[-1] == ')':new_item = item[:-1]list_str.append(new_item)list_POLYGON = []for polygon_str in list_str:list_point_str = polygon_str.split(',')POLYGON = []for point in list_point_str:if point[0] == ' ':point_str = point[1:]else:point_str = pointpoint_list = point_str.split(' ')# dest_str = point_list[0] + ',' + point_list[1]longitude = float(point_list[0])latitude = float(point_list[1])Tuple = (longitude, latitude)POLYGON.append(Tuple)# POLYGON.append(dest_str)list_POLYGON.append(POLYGON)return list_POLYGONelse:  # 只可能有一个polygon# 按 ,split 如果以‘ ’开头,把空格去掉arr_str_coordinate = input.split(',')str_coordinate = []for coordinate in arr_str_coordinate:if coordinate[0] == ' ':str_coordinate.append(coordinate[1:])  # 如果开头有空格,则去掉空格else:str_coordinate.append(coordinate)list_point = []for str_coord in str_coordinate:a, b = str_coord.split(' ')longitude = float(a)latitude = float(b)list_point.append((longitude, latitude))list_POLYGON = [list_point]return list_POLYGON

2. 将 Polygon/MultiPolygon坐标数据 转换为 shapely库中的Polygon类对象的列表

输入输出

调用方式list_polygon = list_POLYGON2POLYGONs(list_POLYGON)

输入:1函数的输出——“一个双层嵌套列表,顶层列表的元素是一个个POLYGON,底层列表的元素是一个POLYGON里的一个个坐标点”

输出:shapely库中的Polygon类对象的列表

功能描述:将 Polygon/MultiPolygon坐标数据 转换为 shapely库中的Polygon类对象的列表

样例
# input为1函数介绍中的格式一样例
input = 'MULTIPOLYGON (((-73.97535722895489 40.79358795668606, -73.97550491442216 40.7932206693501, -73.97571880885164 40.79329900902139, -73.97557038329896 40.793666024933465, -73.97535722895489 40.79358795668606)), ((-73.97552582561218 40.79316866412968, -73.97558333155439 40.79302564935169, -73.97579761926521 40.79310413236238, -73.97573982501896 40.793247041771274, -73.97552582561218 40.79316866412968)))'
list_POLYGON = fork_MULTIPOLYGON_POLYGON_to_POLYGONLIST(input)
list_polygon = list_POLYGON2POLYGONs(list_POLYGON)
print(list_polygon)

控制台输出的内容为:

[
<POLYGON ((-73.975 40.794, -73.976 40.793, -73.976 40.793, -73.976 40.794, -...>, 
<POLYGON ((-73.976 40.793, -73.976 40.793, -73.976 40.793, -73.976 40.793, -...>
]
函数代码
def list_POLYGON2POLYGONs(list_POLYGON):list_polygon = []for POLYGON in list_POLYGON:polygon = Polygon(POLYGON)list_polygon.append(polygon)return list_polygon

3. 将字符串形式的LineString 转化为 shapely库中的LineString类对象

输入输出

调用方式line_string = get_LINESTRING(geometry)

输入:字符串形式的LineString
形如:

LINESTRING (-73.9477940 40.7203470, -73.9478810 40.7209187, -73.9479402 40.7213083, -73.9479510 40.7213790)

输出:shapely库中的LineString类对象

样例
geometry = 'LINESTRING (-73.9477940 40.7203470, -73.9478810 40.7209187, -73.9479402 40.7213083, -73.9479510 40.7213790)'line_string = get_LINESTRING(geometry)print(line_string)
print(type(line_string))

控制台输出如下:

LINESTRING (-73.947794 40.720347, -73.947881 40.7209187, -73.9479402 40.7213083, -73.947951 40.721379)
<class 'shapely.geometry.linestring.LineString'>
函数代码
def get_LINESTRING(input):prefix = 'LINESTRING 'input = input[len(prefix):]input = input[1:-1]cord_arr = input.split(',')points = []for cord in cord_arr:point_str = cordif cord[0] == ' ':point_str = point_str[1:]x, y = point_str.split(' ')tup = (float(x), float(y))points.append(tup)line_string = LineString(points)return line_string

这篇关于Python处理Point, MultiPolygon, Polygon, LineString等Geo地理形状数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/456861

相关文章

大模型研发全揭秘:客服工单数据标注的完整攻略

在人工智能(AI)领域,数据标注是模型训练过程中至关重要的一步。无论你是新手还是有经验的从业者,掌握数据标注的技术细节和常见问题的解决方案都能为你的AI项目增添不少价值。在电信运营商的客服系统中,工单数据是客户问题和解决方案的重要记录。通过对这些工单数据进行有效标注,不仅能够帮助提升客服自动化系统的智能化水平,还能优化客户服务流程,提高客户满意度。本文将详细介绍如何在电信运营商客服工单的背景下进行

基于MySQL Binlog的Elasticsearch数据同步实践

一、为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品、订单等数据的多维度检索。 使用 Elasticsearch 存储业务数据可以很好的解决我们业务中的搜索需求。而数据进行异构存储后,随之而来的就是数据同步的问题。 二、现有方法及问题 对于数据同步,我们目前的解决方案是建立数据中间表。把需要检索的业务数据,统一放到一张M

关于数据埋点,你需要了解这些基本知识

产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

无人叉车3d激光slam多房间建图定位异常处理方案-墙体画线地图切分方案

墙体画线地图切分方案 针对问题:墙体两侧特征混淆误匹配,导致建图和定位偏差,表现为过门跳变、外月台走歪等 ·解决思路:预期的根治方案IGICP需要较长时间完成上线,先使用切分地图的工程化方案,即墙体两侧切分为不同地图,在某一侧只使用该侧地图进行定位 方案思路 切分原理:切分地图基于关键帧位置,而非点云。 理论基础:光照是直线的,一帧点云必定只能照射到墙的一侧,无法同时照到两侧实践考虑:关

python: 多模块(.py)中全局变量的导入

文章目录 global关键字可变类型和不可变类型数据的内存地址单模块(单个py文件)的全局变量示例总结 多模块(多个py文件)的全局变量from x import x导入全局变量示例 import x导入全局变量示例 总结 global关键字 global 的作用范围是模块(.py)级别: 当你在一个模块(文件)中使用 global 声明变量时,这个变量只在该模块的全局命名空

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

异构存储(冷热数据分离)

异构存储主要解决不同的数据,存储在不同类型的硬盘中,达到最佳性能的问题。 异构存储Shell操作 (1)查看当前有哪些存储策略可以用 [lytfly@hadoop102 hadoop-3.1.4]$ hdfs storagepolicies -listPolicies (2)为指定路径(数据存储目录)设置指定的存储策略 hdfs storagepolicies -setStoragePo

Hadoop集群数据均衡之磁盘间数据均衡

生产环境,由于硬盘空间不足,往往需要增加一块硬盘。刚加载的硬盘没有数据时,可以执行磁盘数据均衡命令。(Hadoop3.x新特性) plan后面带的节点的名字必须是已经存在的,并且是需要均衡的节点。 如果节点不存在,会报如下错误: 如果节点只有一个硬盘的话,不会创建均衡计划: (1)生成均衡计划 hdfs diskbalancer -plan hadoop102 (2)执行均衡计划 hd

三国地理揭秘:为何北伐之路如此艰难,为何诸葛亮无法攻克陇右小城?

俗话说:天时不如地利,不是随便说说,诸葛亮六出祁山,连关中陇右的几座小城都攻不下来,行军山高路险,无法携带和建造攻城器械,是最难的,所以在汉中,无论从哪一方进攻,防守方都是一夫当关,万夫莫开;再加上千里运粮,根本不需要打,司马懿只需要坚守城池拼消耗就能不战而屈人之兵。 另一边,洛阳的虎牢关,一旦突破,洛阳就无险可守,这样的进军路线,才是顺势而为的用兵之道。 读历史的时候我们常常看到某一方势

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi