简体   繁体   中英

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. 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

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

The geopandasdocs covers importing data in WKT format using shapely . In their example, given your dataframe df , you could try:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM