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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/197991.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

软件平台架构设计与技术管理之道笔记

软件平台架构设计与技术管理之道笔记 认知 领导软件平台各方面的工作&#xff0c;对技术底蕴、思维模式、决策能力、工作风格、文化铸造等方面都有极高的要求&#xff0c;可以称之为“领域智慧”。认知盲区的代价是巨大的&#xff0c;“不知”比“不会”的后果更严重&#xf…

【Android】HttpURLConnection、OkHttp

WebView 已经在后台帮我们处理好了发送HTTP请求、接收服务器响应、解析返回数据&#xff0c;以及最终的页面展示这几步工作。只不过它封装得实在是太好了&#xff0c;反而使得我们不能那么直观地看出HTTP到底是如何工作的。因此&#xff0c;接下来我们通过手动发送HTTP请求的方…

探讨Unity中的动画融合技术(BlendTree)

动画在游戏和虚拟现实应用中扮演着关键的角色&#xff0c;而动画融合技术则是使角色动作更加流畅和逼真的核心。在Unity引擎中&#xff0c;我们可以使用动画混合树&#xff08;Blend Trees&#xff09;来实现这一目标。本篇技术博客将深入讨论动画融合技术的实现原理、在Unity中…

rust 包模块组织结构

一个包&#xff08;package&#xff09;可以拥有多个二进制单元包及一个可选的库单元包。随着包内代码规模的增长&#xff0c;你还可以将代码拆分到独立的单元包&#xff08;crate&#xff09;中&#xff0c;并将它作为外部依赖进行引用。 RUST提供了一系列的功能来帮助我们管…

做一个类似万师傅家政小程序需要有哪些功能?

现如今人们生活节奏不断加快&#xff0c;自然很少有时间去处理生活中的琐事&#xff0c;恰好家政维修保洁小程序开发则能给线下用户提供方便。 家政保洁小程序应该具备哪些功能&#xff1f; 1、提供家政行业资讯&#xff0c;方便用户在选择家政保洁前了解行业动态。 2、分类搜…

C 中的枚举

简要回顾 最简单的枚举是比宏稍微高级一点的东西。它们可以避免像这样做&#xff1a; #define COLOR_BLACK 0 #define COLOR_WHITE 1 #define COLOR_BLUE 2 #define COLOR_GREEN 3 #define COLOR_RED 4 你可以这样做&#xff1a; enum color {COLOR_BLACK,COLOR_WHITE,COLO…

Android wifi Enable之后扫描流程

流程框架图 通常我们在设备开启wifi之后&#xff0c;等会会自动扫描出周围的热点。 下面看下自动扫描周围热点的流程 代码流程 1. ClientModeManager.ClientModeStateMachine ClientModeStateMachine 由CMD_START 转换到StartedStateStartedState 状态机&#xff0c;在更新…

ubuntu内移除snap

ubuntu 移除snap snap 是 ubuntu 的集中式管理的软件商城&#xff0c;但在使用过程中软件更新没有很及时。ubuntu 20.04 在安装开始就有预装了 snap&#xff0c;所以我想把它移除掉&#xff0c;省出系统空间。 具体参考链接&#xff1a;https://cloud.tencent.com/developer/…

让代码变得优雅简洁的神器:Java8 Stream流式编程

原创/朱季谦 本文主要基于实际项目常用的Stream Api流式处理总结。 因笔者主要从事风控反欺诈相关工作&#xff0c;故而此文使用比较熟悉的三要素之一的【手机号】黑名单作代码案例说明。 我在项目当中&#xff0c;很早就开始使用Java 8的流特性进行开发了&#xff0c;但是一直…

关于区块链

一、区块链 区块链技术是一种去中心化、不可篡改、安全可靠的分布式账本技术&#xff0c;应用场景广泛&#xff0c;有以下几个方面的优势&#xff1a; 金融领域&#xff1a;区块链技术可以用于实现安全的数字货币交易&#xff0c;如比特币等。同时&#xff0c;区块链技术可以用…

流媒体方案之FFmpeg——实现物联网视频监控项目

目录 前言 一、FFmpeg介绍 二、FFmpeg简易理解 三、FFmpeg的重要概念 四、软硬件准备 五、移植、运行FFmpeg 六、运行FFmpeg 前言 最近想做一个安防相关的项目&#xff0c;所以跟着韦东山老师的视频来学习视频监控方案的相关知识&#xff0c;韦东山老师讲的课非常好&…

LaTex入门简明教程

文章目录 写在前面安装Texlive的安装TeXstudio 的安装 LaTex 的使用节指令图指令表指令公式指令参考文献指令引用指令TeXstudio 编译 LaTex 的 \label{} 写法建议最后 写在前面 这篇文章面向没有任何 LaTex 基础的小白&#xff0c;主要讲解了 LaTex 的安装和使用。读完文章之后…

enum 枚举类型

我们可以通过定义枚举IpAddrKind来表达这样的概念&#xff0c;声明该枚举需要列举出所有可能的IP地址种类&#xff0c;也就是所谓的枚举变体&#xff08;variant&#xff09;。 enum IpAddrKind {V4,V6, }枚举的变体全部位于其标识符的命名空间中&#xff0c;并使用两个冒号来…

android https 证书过期

有的时候 我们android https 证书过期 &#xff0c;或者使用明文等方式去访问服务器 可能会碰到类似的 问题 &#xff1a; javax.net.ssl.SSLHandshakeException: Chain validation failed java.security.cert.CertPathValidatorException: Response is unreliable: its validi…

通讯录管理系统(基于C语言)

模块设计 本通讯录管理系统功能模块共包括9个部分&#xff1a;1.输入数据、2.显示数据、 3.插入数据、4.删除数据、5.查看数据、6.修改数据、7.保存数据、 8.返回主菜单、9.退出系统. 一&#xff0e;总体设计 通讯录的每一条信息包括&#xff1a;姓名、性别、住址、联系电话…

西南科技大学模拟电子技术实验七(集成运算放大器的非线性应用)预习报告

一、计算/设计过程 说明:本实验是验证性实验,计算预测验证结果。是设计性实验一定要从系统指标计算出元件参数过程,越详细越好。用公式输入法完成相关公式内容,不得贴手写图片。(注意:从抽象公式直接得出结果,不得分,页数可根据内容调整) 预习计算内容根据运放的非线…

【MODBUS】Modbus主站云端服务器和边缘设备部署区别

Modbus主站作为云端服务器&#xff1a; 云端服务器作为主站&#xff1a; 在这种部署方式中&#xff0c;云端服务器充当Modbus通信的主站&#xff0c;负责向不同的Modbus从站发起请求&#xff0c;并处理响应。云端服务器通常与其他云服务一起运行&#xff0c;可以在云平台上实现…

【Linux下如何生成coredump文件】

一&#xff0c;什么是coredump 我们经常听到大家说到程序core掉了&#xff0c;需要定位解决&#xff0c;这里说的大部分是指对应程序由于各种异常或者bug导致在运行过程中异常退出或者中止&#xff0c;并且在满足一定条件下&#xff08;这里为什么说需要满足一定的条件呢&#…

QT使用SQLite(打开db数据库以及对数据库进行增删改查)

QTSQLite 在QT中使用sqlite数据库&#xff0c;有多种使用方法&#xff0c;在这里我只提供几种简单&#xff0c;代码简短的方法&#xff0c;包括一些特殊字符处理。 用SQlite建立一个简单学生管理数据库 数据库中有两个表一个是class和student。 class表结构 student表结果…

Java零基础-if条件语句

前言 条件语句是编程语言中最基础也是最常用的语句之一&#xff0c;对于初学者来说&#xff0c;掌握好条件语句是学习编程的第一步。本文将以Java开发语言为例&#xff0c;详细介绍Java中的if条件语句及其应用场景。 摘要 本文主要包含以下内容&#xff1a; Java中的if条件…