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

相关文章

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http