简体   繁体   English

在 python 中将一列 MULTIPOLYGON 数据转换为 lat 和 long

[英]Converting a column of MULTIPOLYGON data to lat and long in python

My dataset includes a column of MULTIPOLYGON like below.我的数据集包括一列 MULTIPOLYGON,如下所示。 I am searching a lot to extract the lat and long of data, but I am not able to do that.我正在搜索很多以提取经纬度数据,但我无法做到这一点。 can anyone help me please?谁能帮帮我?

    name                wkt
0   place_1 McDonald's  POLYGON ((13.4611920000000005 52.4709870000000009, 13.4610299999999992 52.4706600000000023, 13.4611959999999993 52.4706290000000024, 13.4612090000000002 52.4706550000000007, 13.4613589999999999 52.4709560000000010, 13.4611920000000005 52.4709870000000009))
1   place_2 McDonald's  POLYGON ((13.4683480000000007 52.5471599999999981, 13.4684080000000002 52.5471560000000011, 13.4684589999999993 52.5471360000000018, 13.4684910000000002 52.5471050000000020, 13.4684960000000000 52.5470729999999975, 13.4685500000000005 52.5470740000000021, 13.4685430000000004 52.5470449999999971, 13.4685269999999999 52.5470180000000013, 13.4684939999999997 52.5469920000000030, 13.4684449999999991 52.5469719999999967, 13.4684080000000002 52.5469629999999981, 13.4683630000000001 52.5469599999999986, 13.4683650000000004 52.5469960000000000, 13.4683159999999997 52.5469989999999996, 13.4682650000000006 52.5470189999999988, 13.4682329999999997 52.5470510000000033, 13.4682270000000006 52.5470880000000022, 13.4682469999999999 52.5471220000000017, 13.4682899999999997 52.5471480000000000, 13.4683480000000007 52.5471599999999981))

the desired out put will be like this:所需的输出将是这样的:

    name                long.                       lat.   
0   place_1 McDonald's  13.4611920000000005.       52.4709870000000009
...

We can use itertools.takewhile in this case:在这种情况下,我们可以使用itertools.takewhile

def get_long_lat(s):
    it = iter(s[10:])  # Get the iterable of the string, not including the first 10 characters.
    long = ''.join(takewhile(lambda c: c.isnumeric() or c == '.', it))  # Take from the iterable as lomg as it's a part of a number. This we also take the next character after the number but won't include it in the output.
    lat = ''.join(takewhile(lambda c: c.isnumeric() or c == '.', it))  # Do it again.
    return long, lat

Use case:用例:

>>> get_long_lat('POLYGON ((13.4611920000000005 52.4709870000000009, 13.4610299999999992 52.4706600000000023, 13.4611959999999993 52.4706290000000024, 13.4612090000000002 52.4706550000000007, 13.4613589999999999 52.4709560000000010, 13.4611920000000005 52.4709870000000009))')
('13.4611920000000005', '52.4709870000000009')  # Note that those are strings, not floats.

Then to add the data to your dataframe ( source ):然后将数据添加到您的 dataframe ( 来源):

df[['long.', 'lat.']] = pd.DataFrame(df['wkt'].apply(get_long_lat).tolist())

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

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