简体   繁体   English

如何将 WKT 格式的坐标转换为纬度和经度列

[英]How do I convert coordinates in WKT format to columns of Latitude and Longitude

I have extracted co-ordinates data from Twitter. The points I got are in the WKT format.我从Twitter中提取坐标数据。我得到的点是WKT格式。 I want to split them into two columns of Latitude and Longitude.我想将它们分成纬度和经度两列。 The format of the coordinates is坐标格式为

{'type': 'Point', 'coordinates': [77.58168, 12.8952]}
{'type': 'Point', 'coordinates': [77.64363, 12.9739]}
{'type': 'Point', 'coordinates': [75.9372318, 12.44627712]}
{'type': 'Point', 'coordinates': [77.5945627, 12.9715987]}
{'type': 'Point', 'coordinates': [77.53584257, 13.05144109]}
{'type': 'Point', 'coordinates': [77.5945627, 12.9715987]}
{'type': 'Point', 'coordinates': [77.58721, 12.96643]}

I removed all the punctuations and unwanted text using str.replace我使用 str.replace 删除了所有标点符号和不需要的文本

df['coordinates'] = df.coordinates.str.replace('type,?' , '')
df['coordinates'] = df.coordinates.str.replace('Point,?' , '')
df['coordinates'] = df.coordinates.str.replace('coordinates,?' , '')
df['coordinates'] = df.coordinates.str.replace('{,?' , '')
df['coordinates'] = df.coordinates.str.replace(',,?' , '')
df['coordinates'] = df.coordinates.str.replace(':,?' , '')
df['coordinates'] = df.coordinates.str.replace('],?' , '')
df['coordinates'] = df.coordinates.str.replace(',?' , '')
df['coordinates'] = df.coordinates.str.replace('},?' , '')
df['coordinates'] = df.coordinates.str.replace("'',?" , "")

I tried to split the column using我尝试使用拆分列

df = pd.DataFrame(df.coordinates.str.split(' ',1).tolist(),
                             columns = ['Long','Lat'])

But it's not working.但它不起作用。 Please let me know what can be done to convert WKT to Columns of Co-ordinates请让我知道如何将 WKT 转换为坐标列

The geopandasdocs covers importing data in WKT format using shapely . geopandas文档介绍了使用shapely以 WKT 格式导入数据。 In their example, given your dataframe df , you could try:在他们的示例中,给定您的 dataframe df ,您可以尝试:

from shapely import wkt
import geopandas as gpd

df['coordinates'] = df['coordinates'].apply(wkt.loads)

gdf = gpd.GeoDataFrame(df, geometry='coordinates')

If you want, you can get the latitude and longtitude and assign back to your original dataframe by doing the following.如果需要,您可以通过执行以下操作获取纬度和经度并将其分配回原始 dataframe。

df['lat'] = gdf.geometry.x
df['long'] = gdf.geometry.y

Since you didn't provide a complete minimum working example, I haven't tested this code, but I think it should work.由于您没有提供完整的最低工作示例,我还没有测试这段代码,但我认为它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将经度、纬度、高程转换为笛卡尔坐标? - How to convert Longitude,Latitude, Elevation to Cartesian coordinates? 如何将地理列转换为纬度和经度? - How do I convert a geography column to latitude and longitude? 试图将经度/纬度转换为笛卡尔坐标 - Trying to convert longitude/latitude to Cartesian coordinates 在Python中将SRS坐标转换为纬度和经度 - Convert from SRS coordinates to Latitude and Longitude in Python 如何将纬度和经度的两个浮点数转换为 Google Maps latlng? - How do I convert two floats of latitude and longitude into Google Maps latlng? 如何将字符串坐标分成 2 个列表:纬度和经度 - How to separate string coordinates into 2 lists: Latitude and Longitude 如何从 python 的经度和纬度获取邮政编码? - How do I get zipcodes from longitude and latitude on python? 如何遍历包含地理坐标文本的列并提取纬度和经度以及 append 作为单独的列? - How do I loop through a column containing geo-coordinate text and extract latitude and longitude, and append as separate columns? 如何查询给定坐标(字符串类型的纬度和经度)中最近的记录? - How can I query the nearest record in a given coordinates(latitude and longitude of string type)? 如何将HEXEWKB转换为Latitude,Longitude(在python中)? - How to convert HEXEWKB to Latitude, Longitude (in python)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM