Chloropleth Map in earth engine¶
In [1]:
Copied!
# !pip install geohub
# !pip install geohub
In [2]:
Copied!
import pandas as pd
import geohub.foliumap as geohub
import numpy as np
import ee
ee.Initialize()
ee.Authenticate()
import pandas as pd
import geohub.foliumap as geohub
import numpy as np
import ee
ee.Initialize()
ee.Authenticate()
Out[2]:
True
In [3]:
Copied!
# URL of the CSV file containing population data
url = "https://www2.census.gov/programs-surveys/popest/datasets/2010-2019/state/detail/SCPRC-EST2019-18+POP-RES.csv"
# Read the CSV file into a pandas DataFrame
population = pd.read_csv(url)
# Rename the columns to match the ones expected by the add_choropleth method
population = population.rename(columns={'NAME': 'State', 'POPESTIMATE2019': 'Population'})
# Define the bin boundaries
bins = [0, 1000000, 5000000, 10000000, 20000000, 30000000, 40000000]
# Bin the population data
population['Population_bin'] = pd.cut(population['Population'], bins=bins, labels=False, include_lowest=True)
# Create an instance of the FoliumMap class from the geohub package
m = geohub.Map()
# Add a choropleth layer to the map
m.add_choropleth(
geo_data='https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json',
data=population,
columns=['State', 'Population_bin'],
key_on='feature.properties.name',
fill_color='YlOrRd', # You can change the color scheme
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Population'
)
# Display the map
m
# URL of the CSV file containing population data
url = "https://www2.census.gov/programs-surveys/popest/datasets/2010-2019/state/detail/SCPRC-EST2019-18+POP-RES.csv"
# Read the CSV file into a pandas DataFrame
population = pd.read_csv(url)
# Rename the columns to match the ones expected by the add_choropleth method
population = population.rename(columns={'NAME': 'State', 'POPESTIMATE2019': 'Population'})
# Define the bin boundaries
bins = [0, 1000000, 5000000, 10000000, 20000000, 30000000, 40000000]
# Bin the population data
population['Population_bin'] = pd.cut(population['Population'], bins=bins, labels=False, include_lowest=True)
# Create an instance of the FoliumMap class from the geohub package
m = geohub.Map()
# Add a choropleth layer to the map
m.add_choropleth(
geo_data='https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json',
data=population,
columns=['State', 'Population_bin'],
key_on='feature.properties.name',
fill_color='YlOrRd', # You can change the color scheme
fill_opacity=0.7,
line_opacity=0.2,
legend_name='Population'
)
# Display the map
m
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook