Uncomment the following line to install geemap if needed.
# !pip install geemap
How to select features by location and attribute?¶
Contents
- 1 Install geemap
- 2 Create an interactive map
- 3 Add data to the map
- 4 TIGER: US Census States
- 4.1 Displaying data
- 4.2 Displaying vector as raster
- 4.3 Select by attribute
- 4.3.1 Select one single state
- 4.3.2 Select multiple states
- 4.3.3 Printing all values of a column
- 4.3.4 Discriptive statistics of a column
- 4.3.5 Add a new column to the attribute table
- 4.3.6 Set symbology based on column values
- 4.3.7 Download attribute table as a CSV
- 4.3.8 Formatting the output
- 4.3.9 Download data as shapefile to Google Drive
- 5 TIGER: US Census Blocks
- 6 TIGER: US Census Counties 2018
- 7 TIGER: US Census Tracts
- 8 TIGER: US Census Roads
Create an interactive map¶
import ee
import geemap
Map = geemap.Map(center=[40, -100], zoom=4)
Map
Add data to the 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_layer_dict
TIGER: US Census States¶
https://developers.google.com/earth-engine/datasets/catalog/TIGER_2018_States
Displaying data¶
Map = emap.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
Displaying vector as raster¶
Map = emap.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 = emap.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)
Select multiple states¶
Map = emap.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
Printing all values of a column¶
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()
Discriptive statistics of a column¶
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)
Add a new column to the attribute table¶
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)
Set symbology based on column values¶
Map = emap.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
Download attribute table as a CSV¶
states = ee.FeatureCollection('TIGER/2018/States')
url = states.getDownloadURL(
filetype="csv",
selectors=['NAME', 'ALAND', 'REGION', 'STATEFP', 'STUSPS'],
filename="states",
)
print(url)
Formatting the output¶
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))
Download data as shapefile to Google Drive¶
# 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()
TIGER: US Census Blocks¶
https://developers.google.com/earth-engine/datasets/catalog/TIGER_2010_Blocks
Map = emap.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
TIGER: US Census Counties 2018¶
https://developers.google.com/earth-engine/datasets/catalog/TIGER_2018_Counties
Map = emap.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 = emap.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
Map = emap.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