0

I'm working with meteorological data and I want to insert the grid points and also their polygon in a GeoDataFrame. They are generated by ERA5 and in a GRIB format.

When I try to create the GeoDataFrame, it prompts the following error:

TypeError: Input must be valid geometry objects: p_std

I did some debug and some search on the internet, but I still don't get where is wrong. I don't know either if it is some theoretical or code mistake.

Code:

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point, Polygon

wkt_grib = 'GEOGCS["Coordinate System imported from GRIB file",DATUM["unnamed",SPHEROID["Sphere",6367470,0]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST]]'

#latitude | -90 a 90
lat = [i*0.01 for i in range(-9000,9001,25)]
#longitude | -180 a 179.75
long = [(i*0.01)-180 for i in range(0,36000,25)]

#the list size is 1038240 | from 0 to 1038239
lat_list = [i for i in lat for x in range(len(long))]
long_list = [x for i in range(len(lat)) for x in long]
name_list = [ str(lat_list[i])+"_"+str(long_list[i]) for i in range(len(long_list))]

p_std_list = gpd.points_from_xy(x=long_list, y=lat_list, crs=wkt_grib)
p_central_list = gpd.points_from_xy(x=[i+0.125 for i in long_list], y=[i+0.125 for i in lat_list], crs=wkt_grib)
grid_list = [Polygon([(long_list[i], lat_list[i]), \
                         (long_list[i]+0.25, lat_list[i]), \
                         (long_list[i]+0.25, lat_list[i]+0.25), \
                         (long_list[i], lat_list[i]+0.25)]) for i in range(len(long_list))]

dict_df = {'name':name_list,'lat':lat_list, 'long':long_list, 'p_std':p_std_list, 'p_central': p_central_list, 'grid':grid_list}
geodf = gpd.GeoDataFrame(dict_df, crs = wkt_grib, geometry = ['p_std','p_central','grid']) 

1 Answer 1

0

In the last line, you're trying to set multiple geometry columns.

geodf = gpd.GeoDataFrame(dict_df, crs = wkt_grib, geometry = ['p_std','p_central','grid']) 

As far as I know, a GeoDataFrame only has one geometry column. You can still have the others as full GeoSeries columns in the dataframe. But there can only one geometry column.

So you need to pick one.

geodf = gpd.GeoDataFrame(dict_df, crs = wkt_grib, geometry='p_std') 

When you pass an array, the GeoDataFrame constructor thinks you want that array itself to be the geometry column. So for example, you could pass a list of geometry objects. A list of strings will throw an error.

From the docstring:

geometry : str or array (optional)
    If str, column to use as geometry. If array, will be set as 'geometry'
1
  • Thank you @JeremiahEngland for your answer! I misunderstood completly the geometry concept in the GeoDataFrame constructor, my bad. Sometimes we spend more time on silly mistakes than on serious ones.
    – Jno Zrc
    Oct 6, 2021 at 3:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.