Geo-pandas data frame to GEE feature collection using Python

Published on Apr 27, 2020 | Bikesh bade | 1622 Views

The simplest way to convert shapefile, CSV it into ee.FeatureCollection is to infuse the Geopandas data frame with GEE python API. To create the ee.FeatureCollection using python API, first you have to set up a python environment in your local computer. 

 

Set up Python API for GEE and continue following 

 

Step 1

 

The Python API package is called ee. It must also be initialized for every new session and script. and import other necessary modules such as Geopandas, Shapely, Numpy

 

# import Google earth engine module
import ee

#Authenticate the Google earth engine with google account
ee.Initialize()


#Pandas modules to interact with spatial data
import geopandas as gpd
import pandas as pd
from geopandas import GeoDataFrame
from shapely.geometry import Point,Polygon


#import numpy
import numpy as np
from functools import reduce

Step 2 

 

Create a Python function to convert Shapefile and CSV to ee.FeaturedCollection:

 

    
#Feature to EE
def feature2ee(file):
    
    #Exception handler
    try:
        
        #check if the file is shapefile or CSV
        if file.endswith('.shp'):
            gdf = gpd.read_file(file, encoding="utf-8")
            g = [i for i in gdf.geometry]
            features=[]
            
            
            #for Polygon geo data type
            if (gdf.geom_type[0] == 'Polygon'):
                for i in range(len(g)):
                    g = [i for i in gdf.geometry]
                    x,y = g[i].exterior.coords.xy
                    cords = np.dstack((x,y)).tolist()

                    g=ee.Geometry.Polygon(cords)
                    feature = ee.Feature(g)
                    features.append(feature)
                print("done")

                ee_object = ee.FeatureCollection(features)
                
                return ee_object
            
                
            #for LineString geo data type   
            elif (gdf.geom_type[0] == 'LineString'):
                for i in range(len(g)):
                    g = [i for i in gdf.geometry]
                    x,y = g[i].exterior.coords.xy
                    cords = np.dstack((x,y)).tolist()
                    double_list = reduce(lambda x,y: x+y, cords)

                    g=ee.Geometry.LineString(double_list)
                    feature = ee.Feature(g)
                    features.append(feature)
                print("done")

                ee_object = ee.FeatureCollection(features)
                
                return ee_object
            
            
            #for Point geo data type
            elif (gdf.geom_type[0] == 'Point'):
                for i in range(len(g)):
                    g = [i for i in gdf.geometry]
                    x,y = g[i].exterior.coords.xy
                    cords = np.dstack((x,y)).tolist()
                    double_list = reduce(lambda x,y: x+y, cords)
                    single_list = reduce(lambda x,y: x+y, double_list)
                    
                    g=ee.Geometry.Point(single_list)
                    feature = ee.Feature(g)
                    features.append(feature)
                print("done")

                ee_object = ee.FeatureCollection(features)
                
                return ee_object
            
            
        #check if the file is shapefile or CSV
        #for CSV we need to have file with X and Y
        elif file.endswith('.csv'):
            df = pd.read_csv(file)
            features=[]
            for i in range(df.shape[0]):
                x,y = df.x[i],df.y[i]
                latlong =[x,y]
                g=ee.Geometry.Point(latlong) 
                feature = ee.Feature(g)
                features.append(feature)
            print("done")
                
            ee_object = ee.FeatureCollection(features)
            return ee_object
            
        
    except:
        
        print("Wrong file format")


Step 3 

 

The final step is to call the function with the file name

 

#shapefile
fp =  "../province.shp"

#call function
banke = feature2ee(fp)

Responses

Leave your comment