Using GeoJSON and GeoPandas data

[1]:
import geopandas as gpd

import earthkit.data as ekd

Load a GeoJSON object as a “file” source using from_source(). Default representation is a GeoPandas dataframe

[2]:
# Test the geojson reader
ekd.download_example_file("NUTS_RG_20M_2021_3035.geojson")
geojson_source = ekd.from_source("file", "NUTS_RG_20M_2021_3035.geojson")
geojson_source
[2]:

GeojsonReader(represented as a geopandas object):

id NUTS_ID LEVL_CODE CNTR_CODE NAME_LATN NUTS_NAME MOUNT_TYPE URBN_TYPE COAST_TYPE FID geometry
0 FR FR 0 FR France France 0.0 0 0 FR MULTIPOLYGON (((9954236.116 -3059379.316, 9961...
1 HR HR 0 HR Hrvatska Hrvatska 0.0 0 0 HR MULTIPOLYGON (((4827385.889 2618351.326, 48483...
2 HU HU 0 HU Magyarország Magyarország 0.0 0 0 HU POLYGON ((5214660.069 2880853.832, 5216710.220...
3 AL AL 0 AL Shqipëria Shqipëria 0.0 0 0 AL POLYGON ((5129579.170 2204098.752, 5148385.473...
4 AT AT 0 AT Österreich Österreich 0.0 0 0 AT POLYGON ((4742889.368 2876362.725, 4783217.798...
... ... ... ... ... ... ... ... ... ... ... ...
2005 TRC21 TRC21 3 TR Şanlıurfa Şanlıurfa 4.0 2 3 TRC21 POLYGON ((6904684.585 2120354.802, 6938677.828...
2006 TRC22 TRC22 3 TR Diyarbakır Diyarbakır 4.0 2 3 TRC22 POLYGON ((6989716.599 2273670.524, 6982786.486...
2007 NO0B2 NO0B2 3 NO Svalbard Svalbard 3.0 3 1 NO0B2 MULTIPOLYGON (((4754167.335 6382461.409, 47465...
2008 NO0B NO0B 2 NO Jan Mayen and Svalbard Jan Mayen and Svalbard NaN 0 0 NO0B MULTIPOLYGON (((4754167.335 6382461.409, 47465...
2009 NO0B1 NO0B1 3 NO Jan Mayen Jan Mayen 3.0 3 1 NO0B1 POLYGON ((3642785.362 5404637.884, 3624291.510...

2010 rows × 11 columns

Iterating over geojson/geopandas objects iterates over rows (feature) instead of the pandas default (columns). This is more useful for geopandas.

[3]:
for thing in geojson_source[:2]:
    print(thing)
id                                                           FR
NUTS_ID                                                      FR
LEVL_CODE                                                     0
CNTR_CODE                                                    FR
NAME_LATN                                                France
NUTS_NAME                                                France
MOUNT_TYPE                                                  0.0
URBN_TYPE                                                     0
COAST_TYPE                                                    0
FID                                                          FR
geometry      MULTIPOLYGON (((9954236.1162 -3059379.3164, 99...
Name: 0, dtype: object
id                                                           HR
NUTS_ID                                                      HR
LEVL_CODE                                                     0
CNTR_CODE                                                    HR
NAME_LATN                                              Hrvatska
NUTS_NAME                                              Hrvatska
MOUNT_TYPE                                                  0.0
URBN_TYPE                                                     0
COAST_TYPE                                                    0
FID                                                          HR
geometry      MULTIPOLYGON (((4827385.8894 2618351.326199999...
Name: 1, dtype: object

It is also possible to create an earthkit-data object from an already instantiated geopandas dataframe

[4]:
gpd_df = gpd.read_file(geojson_source.path)
ek_gpd_df = ekd.from_object(gpd_df)
ek_gpd_df.describe()
[4]:
id NUTS_ID LEVL_CODE CNTR_CODE NAME_LATN NUTS_NAME MOUNT_TYPE URBN_TYPE COAST_TYPE FID geometry
0 FR FR 0 FR France France 0.0 0 0 FR MULTIPOLYGON (((9954236.116 -3059379.316, 9961...
1 HR HR 0 HR Hrvatska Hrvatska 0.0 0 0 HR MULTIPOLYGON (((4827385.889 2618351.326, 48483...
2 HU HU 0 HU Magyarország Magyarország 0.0 0 0 HU POLYGON ((5214660.069 2880853.832, 5216710.220...
3 AL AL 0 AL Shqipëria Shqipëria 0.0 0 0 AL POLYGON ((5129579.170 2204098.752, 5148385.473...
4 AT AT 0 AT Österreich Österreich 0.0 0 0 AT POLYGON ((4742889.368 2876362.725, 4783217.798...
... ... ... ... ... ... ... ... ... ... ... ...
2005 TRC21 TRC21 3 TR Şanlıurfa Şanlıurfa 4.0 2 3 TRC21 POLYGON ((6904684.585 2120354.802, 6938677.828...
2006 TRC22 TRC22 3 TR Diyarbakır Diyarbakır 4.0 2 3 TRC22 POLYGON ((6989716.599 2273670.524, 6982786.486...
2007 NO0B2 NO0B2 3 NO Svalbard Svalbard 3.0 3 1 NO0B2 MULTIPOLYGON (((4754167.335 6382461.409, 47465...
2008 NO0B NO0B 2 NO Jan Mayen and Svalbard Jan Mayen and Svalbard NaN 0 0 NO0B MULTIPOLYGON (((4754167.335 6382461.409, 47465...
2009 NO0B1 NO0B1 3 NO Jan Mayen Jan Mayen 3.0 3 1 NO0B1 POLYGON ((3642785.362 5404637.884, 3624291.510...

2010 rows × 11 columns

[5]:
print(f"Number of polygons in geopandas: {len(ek_gpd_df)}")

print("Iterate of polygons:")
for thing in ek_gpd_df[:2]:
    print(thing)
Number of polygons in geopandas: 2010
Iterate of polygons:
id                                                           FR
NUTS_ID                                                      FR
LEVL_CODE                                                     0
CNTR_CODE                                                    FR
NAME_LATN                                                France
NUTS_NAME                                                France
MOUNT_TYPE                                                  0.0
URBN_TYPE                                                     0
COAST_TYPE                                                    0
FID                                                          FR
geometry      MULTIPOLYGON (((9954236.1162 -3059379.3164, 99...
Name: 0, dtype: object
id                                                           HR
NUTS_ID                                                      HR
LEVL_CODE                                                     0
CNTR_CODE                                                    HR
NAME_LATN                                              Hrvatska
NUTS_NAME                                              Hrvatska
MOUNT_TYPE                                                  0.0
URBN_TYPE                                                     0
COAST_TYPE                                                    0
FID                                                          HR
geometry      MULTIPOLYGON (((4827385.8894 2618351.326199999...
Name: 1, dtype: object
[ ]: