Uncomment the following line to install geemap if needed.
# !pip install geemap
import ee
import geemap
Map = geemap.Map(center=[40, -100], zoom=4)
Map
If you have shapefiles on your computer, you can load shapefiles onto the map using the following script. Note that this will work with small shapefiles. If you shapefiles have complicated geometries and numerous vertices, they might exceed the direct upload limit. In that case, you will have to ingest shapefiles to your GEE account. See instructions here.
shp_path = "../data/us_states.shp"
states_shp = geemap.shp_to_ee(shp_path)
Map.addLayer(states_shp, {}, "US States SHP")
layer = Map.find_layer("US States SHP")
print(layer)
Map.remove_layer(layer)
layer.interact(opacity=(0.0, 1.0, 0.1))
# del layer
Map.add_layer(layer)
Let's remove the layer
Map.remove_layer(Map.layers[-1])
You can also directly load a FeatureCollection from the Earth Engine Data Catalog or your GEE account. For example:
Map.layers
states = ee.FeatureCollection("TIGER/2018/States")
Map.addLayer(states, {}, "TIGER/2018/States")
Map.ee_layers
https://developers.google.com/earth-engine/datasets/catalog/TIGER_2018_States
Map = geemap.Map(center=[40, -100], zoom=4)
states = ee.FeatureCollection("TIGER/2018/States")
Map.centerObject(states, 4)
Map.addLayer(states, {}, "US States")
Map.addLayerControl() # This line is not needed for ipyleaflet-based Map
Map
Map = geemap.Map(center=[40, -100], zoom=4)
states = ee.FeatureCollection("TIGER/2018/States")
image = ee.Image().paint(states, 0, 2)
Map.centerObject(states, 4)
Map.addLayer(image, {}, "US States")
Map.addLayerControl()
Map
Map = geemap.Map(center=[40, -100], zoom=4)
tn = ee.FeatureCollection("TIGER/2018/States").filter(ee.Filter.eq("NAME", "Tennessee"))
Map.centerObject(tn, 6)
Map.addLayer(tn, {}, "Tennessee")
Map.addLayerControl()
Map
tn = (
ee.FeatureCollection("TIGER/2018/States")
.filter(ee.Filter.eq("NAME", "Tennessee"))
.first()
)
props = tn.toDictionary().getInfo()
print(props)
Map = geemap.Map(center=[40, -100], zoom=4)
selected = ee.FeatureCollection("TIGER/2018/States").filter(
ee.Filter.inList("NAME", ["Tennessee", "Alabama", "Georgia"])
)
Map.centerObject(selected, 6)
Map.addLayer(selected, {}, "Selected states")
Map.addLayerControl()
Map
states = ee.FeatureCollection("TIGER/2018/States").sort("ALAND", False)
names = states.aggregate_array("STUSPS").getInfo()
print(names)
areas = states.aggregate_array("ALAND").getInfo()
print(areas)
import matplotlib.pyplot as plt
%matplotlib notebook
plt.bar(names, areas)
plt.show()
For example, we can calculate the total land area of all states:
states = ee.FeatureCollection("TIGER/2018/States")
area_m2 = states.aggregate_sum("ALAND").getInfo()
area_km2 = area_m2 / 1000000
print("Total land area: ", area_km2, " km2")
states = ee.FeatureCollection("TIGER/2018/States")
stats = states.aggregate_stats("ALAND").getInfo()
print(stats)
states = ee.FeatureCollection("TIGER/2018/States").sort("ALAND", False)
states = states.map(lambda x: x.set("AreaKm2", x.area().divide(1000000).toLong()))
first = states.first().toDictionary().getInfo()
print(first)
Map = geemap.Map(center=[40, -100], zoom=4)
states = ee.FeatureCollection("TIGER/2018/States")
visParams = {
"palette": ["purple", "blue", "green", "yellow", "orange", "red"],
"min": 500000000.0,
"max": 5e11,
"opacity": 0.8,
}
image = ee.Image().float().paint(states, "ALAND")
Map.addLayer(image, visParams, "TIGER/2018/States")
Map.addLayerControl()
Map
states = ee.FeatureCollection("TIGER/2018/States")
url = states.getDownloadURL(
filetype="csv",
selectors=["NAME", "ALAND", "REGION", "STATEFP", "STUSPS"],
filename="states",
)
print(url)
first = states.first()
props = first.propertyNames().getInfo()
print(props)
props = states.first().toDictionary(props).getInfo()
print(props)
for key, value in props.items():
print("{}: {}".format(key, value))
# function for converting GeometryCollection to Polygon/MultiPolygon
def filter_polygons(ftr):
geometries = ftr.geometry().geometries()
geometries = geometries.map(
lambda geo: ee.Feature(ee.Geometry(geo)).set("geoType", ee.Geometry(geo).type())
)
polygons = (
ee.FeatureCollection(geometries)
.filter(ee.Filter.eq("geoType", "Polygon"))
.geometry()
)
return ee.Feature(polygons).copyProperties(ftr)
states = ee.FeatureCollection("TIGER/2018/States")
new_states = states.map(filter_polygons)
col_names = states.first().propertyNames().getInfo()
print("Column names: ", col_names)
url = new_states.getDownloadURL("shp", col_names, "states")
print(url)
desc = "states"
# Set configuration parameters for output vector
task_config = {
"folder": "gee-data", # output Google Drive folder
"fileFormat": "SHP",
"selectors": col_names, # a list of properties/attributes to be exported
}
print("Exporting {}".format(desc))
task = ee.batch.Export.table.toDrive(new_states, desc, **task_config)
task.start()
https://developers.google.com/earth-engine/datasets/catalog/TIGER_2010_Blocks
Map = geemap.Map(center=[40, -100], zoom=4)
dataset = ee.FeatureCollection("TIGER/2010/Blocks").filter(
ee.Filter.eq("statefp10", "47")
)
pop = dataset.aggregate_sum("pop10")
print("The number of census blocks: ", dataset.size().getInfo())
print("Total population: ", pop.getInfo())
Map.setCenter(-86.79, 35.87, 6)
Map.addLayer(dataset, {}, "Census Block", False)
visParams = {
"min": 0.0,
"max": 700.0,
"palette": ["black", "brown", "yellow", "orange", "red"],
}
image = ee.Image().float().paint(dataset, "pop10")
Map.setCenter(-73.99172, 40.74101, 13)
Map.addLayer(image, visParams, "TIGER/2010/Blocks")
Map.addLayerControl()
Map
https://developers.google.com/earth-engine/datasets/catalog/TIGER_2018_Counties
Map = geemap.Map(center=[40, -100], zoom=4)
Map.setCenter(-110, 40, 5)
states = ee.FeatureCollection("TIGER/2018/States")
# .filter(ee.Filter.eq('STUSPS', 'TN'))
# // Turn the strings into numbers
states = states.map(lambda f: f.set("STATEFP", ee.Number.parse(f.get("STATEFP"))))
state_image = ee.Image().float().paint(states, "STATEFP")
visParams = {
"palette": ["purple", "blue", "green", "yellow", "orange", "red"],
"min": 0,
"max": 50,
"opacity": 0.8,
}
counties = ee.FeatureCollection("TIGER/2016/Counties")
# print(counties.first().propertyNames().getInfo())
image = ee.Image().paint(states, 0, 2)
# Map.setCenter(-99.844, 37.649, 4)
# Map.addLayer(image, {'palette': 'FF0000'}, 'TIGER/2018/States')
Map.addLayer(state_image, visParams, "TIGER/2016/States")
Map.addLayer(ee.Image().paint(counties, 0, 1), {}, "TIGER/2016/Counties")
Map.addLayerControl()
Map
Map = geemap.Map(center=[40, -100], zoom=4)
dataset = ee.FeatureCollection("TIGER/2010/Tracts_DP1")
visParams = {
"min": 0,
"max": 4000,
"opacity": 0.8,
"palette": [
"#ece7f2",
"#d0d1e6",
"#a6bddb",
"#74a9cf",
"#3690c0",
"#0570b0",
"#045a8d",
"#023858",
],
}
# print(dataset.first().propertyNames().getInfo())
# Turn the strings into numbers
dataset = dataset.map(
lambda f: f.set("shape_area", ee.Number.parse(f.get("dp0010001")))
)
# Map.setCenter(-103.882, 43.036, 8)
image = ee.Image().float().paint(dataset, "dp0010001")
Map.addLayer(image, visParams, "TIGER/2010/Tracts_DP1")
Map.addLayerControl()
Map
https://developers.google.com/earth-engine/datasets/catalog/TIGER_2016_Roads
Map = geemap.Map(center=[40, -100], zoom=4)
fc = ee.FeatureCollection("TIGER/2016/Roads")
Map.setCenter(-73.9596, 40.7688, 12)
Map.addLayer(fc, {}, "Census roads")
Map.addLayerControl()
Map