4. Using Local Geospatial Data#

4.1. Introduction#

4.2. Technical requirements#

conda create -n gee python
conda activate gee
conda install -c conda-forge mamba
mamba install -c conda-forge geemap pygis
jupyter lab

Open in Colab

# pip install pygis
import ee
import geemap
geemap.ee_initialize()

4.3. Local raster datasets#

4.3.1. Single-band imagery#

url = 'https://github.com/giswqs/data/raw/main/raster/srtm90.tif'
filename = 'srtm90.tif'
geemap.download_file(url, filename)
Map = geemap.Map()

Map.add_raster(filename, palette='terrain', layer_name="DEM")

vis_params = {'min': 0, 'max': 4000, 'palette': 'terrain'}

Map.add_colorbar(vis_params, label='Elevation (m)')
Map

4.3.2. Multi-band imagery#

url = 'https://github.com/giswqs/leafmap/raw/master/examples/data/cog.tif'
filename = 'cog.tif'
geemap.download_file(url, filename)
Map = geemap.Map()
Map.add_raster(filename, band=[4, 1, 2], layer_name="Color infrared")
Map

4.3.3. Interactive raster GUI#

4.4. Cloud Optimized GeoTIFF (COG)#

4.4.1. Visualizing COG#

url = 'https://tinyurl.com/24bo8umr'
geemap.cog_bounds(url)
geemap.cog_center(url)
geemap.cog_bands(url)
geemap.cog_tile(url)
Map = geemap.Map()
Map.add_cog_layer(url, name="Fire (pre-event)")
Map
url2 = 'https://tinyurl.com/2awjl66w'
Map.add_cog_layer(url2, name="Fire (post-event)")
Map

4.4.2. Creating COG#

url = "https://github.com/giswqs/data/raw/main/raster/srtm90.tif"
geemap.cog_validate(url)
geemap.cog_validate(url, verbose=True)
out_cog = "cog.tif"
geemap.image_to_cog(url, out_cog)
geemap.cog_validate(out_cog)
Map = geemap.Map()
Map.add_raster(out_cog, palette="terrain", layer_name="Local COG")
Map.add_cog_layer(url, palette="gist_earth", name="Remote COG")

vis_params = {'min': 0, 'max': 4000, 'palette': 'gist_earth'}
Map.add_colorbar(vis_params, label='Elevation (m)')
Map

4.4.3. Converting NumPy arrays to COG#

url = 'https://github.com/giswqs/leafmap/raw/master/examples/data/cog.tif'
in_cog = 'cog.tif'
out_cog = "ndvi.tif"
geemap.download_file(url, in_cog)
arr = geemap.image_to_numpy(in_cog)
arr.shape
ndvi = (arr[3] - arr[0]) / (arr[3] + arr[0])
ndvi.shape
geemap.numpy_to_cog(ndvi, out_cog, profile=in_cog)
Map = geemap.Map()
Map.add_raster(in_cog, band=[4, 1, 2], layer_name="Color infrared")
Map.add_raster(out_cog, palette="Greens", layer_name="NDVI")
Map

4.4.4. Clipping image by mask#

url = 'https://github.com/giswqs/data/raw/main/raster/srtm90.tif'
dem = 'dem.tif'
geemap.download_file(url, dem)
Map = geemap.Map()
Map.add_raster(dem, palette='terrain', layer_name="DEM")
Map
mask = (
    'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/mask.geojson'
)
mask = Map.user_roi
mask = [
    [-119.679565, 37.256566],
    [-119.679565, 38.061067],
    [-118.24585, 38.061067],
    [-118.24585, 37.256566],
    [-119.679565, 37.256566],
]
output = 'clip.tif'
geemap.clip_image(dem, mask, output)
Map.add_raster(output, palette='coolwarm', layer_name="Clip Image")
Map

4.5. SpatioTemporal Asset Catalog (STAC)#

url = 'https://tinyurl.com/22vptbws'
geemap.stac_bounds(url)
geemap.stac_center(url)
geemap.stac_bands(url)
geemap.stac_tile(url, bands=['B3', 'B2', 'B1'])
Map = geemap.Map()
Map.add_stac_layer(url, bands=['pan'], name='Panchromatic')
Map.add_stac_layer(url, bands=['B3', 'B2', 'B1'], name='False color')
Map

4.6. Vector datasets#

4.6.1. GeoJSON#

in_geojson = (
    'https://github.com/giswqs/geemap/blob/master/examples/data/cable_geo.geojson'
)
Map = geemap.Map()
Map.add_geojson(in_geojson, layer_name="Cable lines", info_mode="on_hover")
Map
Map = geemap.Map()
Map.add_basemap("CartoDB.DarkMatter")
callback = lambda feat: {"color": "#" + feat["properties"]["color"], "weight": 2}
Map.add_geojson(in_geojson, layer_name="Cable lines", style_callback=callback)
Map
url = "https://github.com/giswqs/geemap/blob/master/examples/data/countries.geojson"
Map = geemap.Map()
Map.add_geojson(
    url, layer_name="Countries", fill_colors=['red', 'yellow', 'green', 'orange']
)
Map
import random

Map = geemap.Map()


def random_color(feature):
    return {
        'color': 'black',
        'weight': 3,
        'fillColor': random.choice(['red', 'yellow', 'green', 'orange']),
    }


Map.add_geojson(url, layer_name="Countries", style_callback=random_color)
Map
Map = geemap.Map()

style = {
    "stroke": True,
    "color": "#0000ff",
    "weight": 2,
    "opacity": 1,
    "fill": True,
    "fillColor": "#0000ff",
    "fillOpacity": 0.1,
}

hover_style = {"fillOpacity": 0.7}

Map.add_geojson(url, layer_name="Countries", style=style, hover_style=hover_style)
Map

4.6.2. Shapefile#

url = "https://github.com/giswqs/geemap/blob/master/examples/data/countries.zip"
geemap.download_file(url)
Map = geemap.Map()
in_shp = "countries.shp"
Map.add_shp(in_shp, layer_name="Countries")
Map

4.6.3. KML#

in_kml = "https://github.com/giswqs/geemap/blob/master/examples/data/us_states.kml"
Map = geemap.Map(center=[40, -100], zoom=4)
Map.add_kml(in_kml, layer_name="US States")
Map

4.6.4. GeoDataFrame#

import geopandas as gpd
Map = geemap.Map(center=[40, -100], zoom=4)
gdf = gpd.read_file('countries.shp')
Map.add_gdf(gdf, layer_name="US States")
Map

4.6.5. Other vector formats#

Map = geemap.Map()
data = 'https://github.com/giswqs/geemap/blob/master/examples/data/countries.gpkg'
Map.add_vector(data, layer_name="Countries")
Map

4.7. Creating points from XY#

4.7.1. CSV to vector#

data = 'https://github.com/giswqs/geemap/blob/master/examples/data/us_cities.csv'
geemap.csv_to_df(data)
geemap.csv_to_geojson(
    data, 'cities.geojson', latitude="latitude", longitude='longitude'
)
geemap.csv_to_shp(data, 'cities.shp', latitude="latitude", longitude='longitude')
geemap.csv_to_gdf(data, latitude="latitude", longitude='longitude')
geemap.csv_to_vector(data, 'cities.gpkg', latitude="latitude", longitude='longitude')

4.7.2. Adding points from XY#

cities = 'https://github.com/giswqs/geemap/blob/master/examples/data/us_cities.csv'
regions = (
    'https://github.com/giswqs/geemap/blob/master/examples/data/us_regions.geojson'
)
Map = geemap.Map(center=[40, -100], zoom=4)
Map.add_points_from_xy(data, x="longitude", y="latitude")
Map
Map = geemap.Map(center=[40, -100], zoom=4)

Map.add_geojson(regions, layer_name='US Regions')

Map.add_points_from_xy(
    data,
    x='longitude',
    y='latitude',
    layer_name='US Cities',
    color_column='region',
    icon_names=['gear', 'map', 'leaf', 'globe'],
    spin=True,
    add_legend=True,
)
Map

4.7.3. Circle markers from points#

data = 'https://github.com/giswqs/geemap/blob/master/examples/data/us_cities.csv'
Map = geemap.Map(center=[40, -100], zoom=4)
Map.add_circle_markers_from_xy(
    data,
    x="longitude",
    y="latitude",
    radius=10,
    color="blue",
    fill_color="black",
    fill_opacity=0.5,
)
Map

4.8. Vector data to Earth Engine#

in_geojson = (
    'https://github.com/giswqs/geemap/blob/master/examples/data/countries.geojson'
)
Map = geemap.Map()
fc = geemap.geojson_to_ee(in_geojson)
Map.addLayer(fc.style(**{'color': 'ff0000', 'fillColor': '00000000'}), {}, 'Countries')
Map
url = "https://github.com/giswqs/geemap/blob/master/examples/data/countries.zip"
geemap.download_file(url)
in_shp = "countries.shp"
fc = geemap.shp_to_ee(in_shp)
import geopandas as gpd

gdf = gpd.read_file(in_shp)
fc = geemap.gdf_to_ee(gdf)
fc = geemap.vector_to_ee(url)

4.9. Joining attribute tables#

Map = geemap.Map()
countries = ee.FeatureCollection(geemap.examples.get_ee_path('countries'))
Map.addLayer(countries, {}, 'Countries')
Map
geemap.ee_to_df(countries)
data = (
    'https://github.com/giswqs/geemap/blob/master/examples/data/country_centroids.csv'
)
df = geemap.csv_to_df(data)
df
fc = geemap.ee_join_table(countries, data, src_key='ISO_A2', dst_key='country')
geemap.ee_to_df(fc)
Map.addLayer(fc, {}, 'Countries with attr')
Map

4.10. Converting NetCDF to ee.Image#

import os
url = 'https://github.com/giswqs/geemap/blob/master/examples/data/wind_global.nc'
nc_file = 'wind_global.nc'
if not os.path.exists(nc_file):
    geemap.download_file(url)
Map = geemap.Map()
img = geemap.netcdf_to_ee(nc_file=nc_file, var_names='u_wind')
vis_params = {'min': -20, 'max': 25, 'palette': 'YlOrRd', 'opacity': 0.6}
Map.addLayer(img, vis_params, "u_wind")
Map
Map = geemap.Map()
img = geemap.netcdf_to_ee(nc_file=nc_file, var_names=['u_wind', 'v_wind'])
Map.addLayer(
    img,
    {'bands': ['v_wind'], 'min': -20, 'max': 25, 'palette': 'coolwarm', 'opacity': 0.8},
    "v_wind",
)
Map

4.11. OpenStreetMap data#

4.11.1. OSM to GeoDataFrame#

gdf = geemap.osm_to_gdf("Knoxville, Tennessee")
gdf

4.11.2. OSM to ee.FeatureCollection#

Map = geemap.Map()
fc = geemap.osm_to_ee("Knoxville, Tennessee")
Map.addLayer(fc, {}, "Knoxville")
Map.centerObject(fc, 11)
Map

4.11.3. Downloading OSM data#

Map = geemap.Map()
gdf = geemap.osm_gdf_from_geocode("New York City")
Map.add_gdf(gdf, layer_name="NYC")
Map
place = "Bunker Hill, Los Angeles, California"
tags = {"building": True}
gdf = geemap.osm_gdf_from_place(place, tags)
gdf
Map = geemap.Map()
Map.add_gdf(gdf, layer_name="Los Angeles, CA")
Map
gdf = geemap.osm_gdf_from_address(
    address="New York City", tags={"amenity": "bar"}, dist=1500
)
gdf
Map = geemap.Map()
Map.add_gdf(gdf, layer_name="NYC bars")
Map
gdf = geemap.osm_gdf_from_point(
    center_point=(46.7808, -96.0156),
    tags={"natural": "water"},
    dist=10000,
)
gdf
Map = geemap.Map()
Map.add_gdf(gdf, layer_name="Lakes")
Map
Map = geemap.Map(center=[40.7500, -73.9854], zoom=16)
Map
Map.add_osm_from_view(tags={"amenity": "bar", "building": True})

4.12. Reading PostGIS data#

mamba install sqlalchemy psycopg2 -c conda-forge
con = geemap.connect_postgis(
    database="nyc", host="localhost", user=None, password=None, use_env_var=True
)
sql = 'SELECT * FROM nyc_neighborhoods'
gdf = geemap.read_postgis(sql, con)
gdf
Map = geemap.Map()
Map = geemap.gdf_to_ee(gdf)
Map.addLayer(fc, {}, "NYC EE")
Map.centerObject(fc)
Map
Map = geemap.Map()
Map.add_gdf_from_postgis(
    sql, con, layer_name="NYC Neighborhoods", fill_colors=["red", "green", "blue"]
)
Map

4.13. Summary#

4.14. References#