简体   繁体   中英

WKTReadingError: Could not create geometry because of errors while reading input

I have a pandas DataFrame that I am trying to convert to geopandas. I tried a few different methods to convert and also attempted to fix the invalid geometries. Code below:

import pandas as pd
import geopandas as gpd
import numpy as np
import shapely 

data = [['A', 'POLYGON((-97.173931 32.726155, -97.173803 32.725959, -97.17345 32.725588, -97.173399 32.725379, -97.17337 32.72517, -97.173329 32.724661, -97.173479 32.723889, -97.17353 32.723683, -97.173634 32.723318, -97.173715 32.723098, -97.173732 32.722997, -97.17372 32.722926, -97.173686 32.722885, -97.173506 32.722779, -97.173341 32.722711, -97.173295 32.722625, -97.173295 32.722525, -97.173317 32.722359, -97.173479 32.722017, -97.17383 32.721455, -97.173855 32.721359, -97.173867 32.721189, -97.173815 32.720703, -97.173794 32.720644, -97.173702 32.720573, -97.17305 32.720311, -97.17295 32.720261, -97.172914 32.720204, -97.172875 32.720084, -97.172852 32.719879, -97.173069 32.718307, -97.173059 32.718203, -97.173031 32.718091, -97.172995 32.717938, -97.172977 32.717902, -97.17234 32.716737, -97.172278 32.716512, -97.172249 32.716309, -97.172251 32.716119, -97.172294 32.715916, -97.172353 32.715668, -97.17246 32.715465, -97.17256 32.715229, -97.172678 32.714879, -97.172698 32.714528, -97.172658 32.713959, -97.172674 32.713279, -97.172762 32.712791, -97.172859 32.712541, -97.172891 32.712303, -97.172884 32.71225, -97.172859 32.712052, -97.172778 32.711855, -97.172658 32.711605, -97.172585 32.711483, -97.172433 32.711349, -97.17224 32.711098, -97.172103 32.710853, -97.172033 32.710797, -97.171893 32.710684, -97.17174 32.710535, -97.171659 32.710399, -97.171619 32.710182, -97.171482 32.709924, -97.17124 32.709612, -97.171039 32.709435, -97.170829 32.709327, -97.170644 32.709164, -97.170498 32.708967, -97.170474 32.708886, -97.170474 32.708818, -97.170539 32.708762, -97.170847 32.708534, -97.17095 32.708472, -97.171193 32.708303, -97.171241 32.708261, -97.171265 32.708167, -97.171241 32.708086, -97.171152 32.707855, -97.171176 32.707752, -97.171209 32.70767, -97.171289 32.707569, -97.171628 32.707352, -97.171806 32.707257, -97.171951 32.707161, -97.172137 32.70706, -97.172225 32.706952, -97.172143 32.706878, -97.171991 32.706767, -97.169788 32.706812, -97.167465 32.706795, -97.166478 32.706786, -97.166371 32.706786, -97.164836 32.706789, -97.163938 32.706772, -97.162994 32.706766, -97.162556 32.706775, -97.162485 32.706809, -97.161086 32.706818, -97.160792 32.70682, -97.159668 32.706803, -97.158873 32.706792, -97.158161 32.706781, -97.157229 32.706795, -97.1559 32.706809, -97.15441 32.706783, -97.151904 32.7068, -97.151302 32.706809, -97.149196 32.706792, -97.147339 32.706749, -97.145999 32.706733, -97.145147 32.706732, -97.14489 32.706733, -97.143997 32.706708, -97.14398 32.708101, -97.143979 32.709505, -97.144706 32.709555, -97.146127 32.709598, -97.147016 32.709605, -97.149187 32.709622, -97.149186 32.709707, -97.149186 32.709794, -97.149181 32.710583, -97.149176 32.711112, -97.149155 32.713005, -97.149145 32.714031, -97.149138 32.714695, -97.149129 32.715643, -97.149122 32.716448, -97.14913 32.717495, -97.149136 32.717934, -97.149134 32.719369, -97.149132 32.719638, -97.149128 32.720087, -97.149127 32.721344, -97.150177 32.721376, -97.152005 32.72139, -97.152301 32.721389, -97.153162 32.721393, -97.154283 32.721398, -97.154741 32.721418, -97.155423 32.721407, -97.156737 32.721421, -97.159907 32.721417, -97.161164 32.721415, -97.16153 32.721414, -97.162343 32.721412, -97.162531 32.721418, -97.163111 32.721437, -97.163581 32.721429, -97.164519 32.721418, -97.166019 32.721425, -97.166269 32.721444, -97.166297 32.722749, -97.166303 32.723678, -97.166295 32.724169, -97.166278 32.72554, -97.166276 32.725706, -97.166272 32.726416, -97.166238 32.727725, -97.166225 32.72855, -97.166231 32.72921, -97.16625 32.729717, -97.166281 32.73042, -97.166303 32.731019, -97.166322 32.731099, -97.166352 32.731136, -97.166395 32.731155, -97.166997 32.731135, -97.167793 32.731135, -97.16784 32.731147, -97.167973 32.731254, -97.16808 32.731389, -97.168102 32.731495, -97.168349 32.731495, -97.169424 32.731498, -97.169445 32.731445, -97.16952 32.731327, -97.169956 32.730763, -97.170038 32.730691, -97.170282 32.730576, -97.170419 32.730537, -97.170523 32.730522, -97.17067 32.730527, -97.17082 32.730559, -97.171007 32.730627, -97.171019 32.730629, -97.171228 32.730674, -97.171306 32.730674, -97.171364 32.73066, -97.17145 32.730593, -97.171468 32.730545, -97.171468 32.730485, -97.171446 32.730435, -97.171396 32.730367, -97.171303 32.730274, -97.17121 32.730213, -97.171095 32.730173, -97.170915 32.73008, -97.170844 32.730027, -97.170811 32.729979, -97.170753 32.729663, -97.170734 32.729562, -97.170752 32.729501, -97.170788 32.729433, -97.170903 32.729301, -97.171061 32.729197, -97.17128 32.7291, -97.171358 32.729028, -97.171742 32.728459, -97.171857 32.72838, -97.172015 32.728322, -97.172144 32.728308, -97.172395 32.728243, -97.172497 32.728205, -97.172536 32.728183, -97.172574 32.72815, -97.172599 32.728119, -97.172599 32.728087, -97.172586 32.728023, -97.172558 32.727993, -97.172536 32.72797, -97.172372 32.727854, -97.172334 32.727758, -97.172334 32.727705, -97.172359 32.727631, -97.172422 32.727545, -97.172752 32.727285, -97.173718 32.726516, -97.173843 32.72641, -97.173916 32.726333, -97.173944 32.726261, -97.173931 32.726155))'],
        ['B', 'POLYGON ((-83.230504 42.242044, -83.228256 42.242185, -83.222044 42.242297, -83.221459 42.242307, -83.21916399999999 42.242432, -83.21682 42.242508, -83.21650799999999 42.23718, -83.216279 42.232113, -83.216138 42.227846, -83.219548 42.227773, -83.229843 42.227545, -83.229902 42.228622, -83.230504 42.242044))']]
df = pd.DataFrame.from_records(data=data, columns=['name','g'])

invalid = []

def invalid_geos(gdf):

    for ix in gdf.index:
        try:
           shapely.wkt.loads(gdf.iloc[ix].tract_geom) 
        except:
           invalid.append(ix)

    return gdf.drop(invalid)

 g = invalid_geos(df)

 g['geom'] = g['g'].apply(lambda x: wkt.loads(x))


/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/shapely/geos.py in read(self, text)
    330         geom = self._lgeos.GEOSWKTReader_read(self._reader, c_string)
    331         if not geom:
--> 332             raise WKTReadingError(
    333                 "Could not create geometry because of errors "
    334                 "while reading input.")

WKTReadingError: Could not create geometry because of errors while reading input.


g1 = gpd.GeoDataFrame(g, geometry='g')

TypeError                                 Traceback (most recent call last)

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/geopandas/geodataframe.py in __init__(self, data, geometry, crs, *args, **kwargs)
    182                 _crs_mismatch_warning()
    183                 # TODO: raise error in 0.9 or 0.10.
--> 184             self.set_geometry(geometry, inplace=True)
    185 
    186         if geometry is None and crs:

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/geopandas/geodataframe.py in set_geometry(self, col, drop, inplace, crs)
    316 
    317         # Check that we are using a listlike of geometries
--> 318         level = _ensure_geometry(level, crs=crs)
    319         index = frame.index
    320         frame[geo_column_name] = level

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/geopandas/geodataframe.py in _ensure_geometry(data, crs)
     41     else:
     42         if isinstance(data, Series):
---> 43             out = from_shapely(np.asarray(data), crs=crs)
     44             return GeoSeries(out, index=data.index, name=data.name)
     45         else:

/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/geopandas/array.py in from_shapely(data, crs)
    149 
    150     """
--> 151     return GeometryArray(vectorized.from_shapely(data), crs=crs)
    152 
    153 

    /Applications/Anaconda/anaconda3/lib/python3.9/site-packages/geopandas/_vectorized.py in from_shapely(data)
        134             out.append(None)
        135         else:
    --> 136             raise TypeError("Input must be valid geometry objects: {0}".format(geom))
        137 
        138     if compat.USE_PYGEOS:
TypeError: Input must be valid geometry objects: POLYGON((-97.173931 32.726155, -97.173803 32.725959, -97.17345 32.725588, -97.173399 32.725379, -97.17337 32.72517, -97.173329 32.724661, -97.173479 32.723889, -97.17353 32.723683, -97.173634 32.723318, -97.173715 32.723098, -97.173732 32.722997, -97.17372 32.722926, -97.173686 32.722885, -97.173506 32.722779, -97.173341 32.722711, -97.173295 32.722625, -97.173295 32.722525, -97.173317 32.722359, -97.173479 32.722017, -97.17383 32.721455, -97.173855 32.721359, -97.173867 32.721189, -97.173815 32.720703, -97.173794 32.720644, -97.173702 32.720573, -97.17305 32.720311, -97.17295 32.720261, -97.172914 32.720204, -97.172875 32.720084, -97.172852 32.719879, -97.173069 32.718307, -97.173059 32.718203, -97.173031 32.718091, -97.172995 32.717938, -97.172977 32.717902, -97.17234 32.716737, -97.172278 32.716512, -97.172249 32.716309, -97.172251 32.716119, -97.172294 32.715916, -97.172353 32.715668, -97.17246 32.715465, -97.17256 32.715229, -97.172678 32.714879, -97.172698 32.714528, -97.172658 32.713959, -97.172674 32.713279, -97.172762 32.712791, -97.172859 32.712541, -97.172891 32.712303, -97.172884 32.71225, -97.172859 32.712052, -97.172778 32.711855, -97.172658 32.711605, -97.172585 32.711483, -97.172433 32.711349, -97.17224 32.711098, -97.172103 32.710853, -97.172033 32.710797, -97.171893 32.710684, -97.17174 32.710535, -97.171659 32.710399, -97.171619 32.710182, -97.171482 32.709924, -97.17124 32.709612, -97.171039 32.709435, -97.170829 32.709327, -97.170644 32.709164, -97.170498 32.708967, -97.170474 32.708886, -97.170474 32.708818, -97.170539 32.708762, -97.170847 32.708534, -97.17095 32.708472, -97.171193 32.708303, -97.171241 32.708261, -97.171265 32.708167, -97.171241 32.708086, -97.171152 32.707855, -97.171176 32.707752, -97.171209 32.70767, -97.171289 32.707569, -97.171628 32.707352, -97.171806 32.707257, -97.171951 32.707161, -97.172137 32.70706, -97.172225 32.706952, -97.172143 32.706878, -97.171991 32.706767, -97.169788 32.706812, -97.167465 32.706795, -97.166478 32.706786, -97.166371 32.706786, -97.164836 32.706789, -97.163938 32.706772, -97.162994 32.706766, -97.162556 32.706775, -97.162485 32.706809, -97.161086 32.706818, -97.160792 32.70682, -97.159668 32.706803, -97.158873 32.706792, -97.158161 32.706781, -97.157229 32.706795, -97.1559 32.706809, -97.15441 32.706783, -97.151904 32.7068, -97.151302 32.706809, -97.149196 32.706792, -97.147339 32.706749, -97.145999 32.706733, -97.145147 32.706732, -97.14489 32.706733, -97.143997 32.706708, -97.14398 32.708101, -97.143979 32.709505, -97.144706 32.709555, -97.146127 32.709598, -97.147016 32.709605, -97.149187 32.709622, -97.149186 32.709707, -97.149186 32.709794, -97.149181 32.710583, -97.149176 32.711112, -97.149155 32.713005, -97.149145 32.714031, -97.149138 32.714695, -97.149129 32.715643, -97.149122 32.716448, -97.14913 32.717495, -97.149136 32.717934, -97.149134 32.719369, -97.149132 32.719638, -97.149128 32.720087, -97.149127 32.721344, -97.150177 32.721376, -97.152005 32.72139, -97.152301 32.721389, -97.153162 32.721393, -97.154283 32.721398, -97.154741 32.721418, -97.155423 32.721407, -97.156737 32.721421, -97.159907 32.721417, -97.161164 32.721415, -97.16153 32.721414, -97.162343 32.721412, -97.162531 32.721418, -97.163111 32.721437, -97.163581 32.721429, -97.164519 32.721418, -97.166019 32.721425, -97.166269 32.721444, -97.166297 32.722749, -97.166303 32.723678, -97.166295 32.724169, -97.166278 32.72554, -97.166276 32.725706, -97.166272 32.726416, -97.166238 32.727725, -97.166225 32.72855, -97.166231 32.72921, -97.16625 32.729717, -97.166281 32.73042, -97.166303 32.731019, -97.166322 32.731099, -97.166352 32.731136, -97.166395 32.731155, -97.166997 32.731135, -97.167793 32.731135, -97.16784 32.731147, -97.167973 32.731254, -97.16808 32.731389, -97.168102 32.731495, -97.168349 32.731495, -97.169424 32.731498, -97.169445 32.731445, -97.16952 32.731327, -97.169956 32.730763, -97.170038 32.730691, -97.170282 32.730576, -97.170419 32.730537, -97.170523 32.730522, -97.17067 32.730527, -97.17082 32.730559, -97.171007 32.730627, -97.171019 32.730629, -97.171228 32.730674, -97.171306 32.730674, -97.171364 32.73066, -97.17145 32.730593, -97.171468 32.730545, -97.171468 32.730485, -97.171446 32.730435, -97.171396 32.730367, -97.171303 32.730274, -97.17121 32.730213, -97.171095 32.730173, -97.170915 32.73008, -97.170844 32.730027, -97.170811 32.729979, -97.170753 32.729663, -97.170734 32.729562, -97.170752 32.729501, -97.170788 32.729433, -97.170903 32.729301, -97.171061 32.729197, -97.17128 32.7291, -97.171358 32.729028, -97.171742 32.728459, -97.171857 32.72838, -97.172015 32.728322, -97.172144 32.728308, -97.172395 32.728243, -97.172497 32.728205, -97.172536 32.728183, -97.172574 32.72815, -97.172599 32.728119, -97.172599 32.728087, -97.172586 32.728023, -97.172558 32.727993, -97.172536 32.72797, -97.172372 32.727854, -97.172334 32.727758, -97.172334 32.727705, -97.172359 32.727631, -97.172422 32.727545, -97.172752 32.727285, -97.173718 32.726516, -97.173843 32.72641, -97.173916 32.726333, -97.173944 32.726261, -97.173931 32.726155))

Change gdf.iloc[ix].tract_geom to gdf.iloc[ix][1]

def invalid_geos(gdf):
    for ix in gdf.index:
        try:
            shapely.wkt.loads(gdf.iloc[ix][1])
            #                             ^^^
        except:
            invalid.append(ix)

    return gdf.drop(invalid)

print(g)
print("\ninvalid=", invalid)

Output:

     name                                                  g                                               geom
0    A  POLYGON((-97.173931 32.726155, -97.173803 32.7...  POLYGON ((-97.173931 32.726155, -97.173803 32....
1    B  POLYGON ((-83.230504 42.242044, -83.228256 42....  POLYGON ((-83.230504 42.242044, -83.228256 42....

invalid= []

If all the geometries are valid then can just do following to convert Pandas DataFrame to GeoDataFrame.

df['geometry'] = df['g'].apply(shapely.wkt.loads)
gdf = gpd.GeoDataFrame(df, crs='epsg:4326')

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