Skip to content

Commit

Permalink
Add spatial join examples
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Dec 25, 2019
1 parent 9857c8e commit 9ddf955
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 1 deletion.
35 changes: 35 additions & 0 deletions Join/inner_joins.py
@@ -0,0 +1,35 @@
import ee
from ee_plugin import Map

# Make a date filter to get images in this date range.
dateFilter = ee.Filter.date('2014-01-01', '2014-02-01')

# Load a MODIS collection with EVI data.
mcd43a4 = ee.ImageCollection('MODIS/MCD43A4_006_EVI') \
.filter(dateFilter)

# Load a MODIS collection with quality data.
mcd43a2 = ee.ImageCollection('MODIS/006/MCD43A2') \
.filter(dateFilter)

# Define an inner join.
innerJoin = ee.Join.inner()

# Specify an equals filter for image timestamps.
filterTimeEq = ee.Filter.equals(**{
'leftField': 'system:time_start',
'rightField': 'system:time_start'
})

# Apply the join.
innerJoinedMODIS = innerJoin.apply(mcd43a4, mcd43a2, filterTimeEq)

# Display the join result: a FeatureCollection.
print('Inner join output:', innerJoinedMODIS)

# Map a function to merge the results in the output FeatureCollection.
joinedMODIS = innerJoinedMODIS.map(lambda feature: ee.Image.cat(feature.get('primary'), feature.get('secondary')))

# Print the result of merging.
print('Inner join, merged bands:', joinedMODIS.getInfo())

51 changes: 51 additions & 0 deletions Join/intersect.py
@@ -0,0 +1,51 @@
import ee
from ee_plugin import Map

def intersect(state):
nPowerPlants = ee.List(state.get('power_plants')).size()
# Return the state feature with a new property: power plant count.
return state.set('n_power_plants', nPowerPlants)

# Load the primary 'collection': US state boundaries.
states = ee.FeatureCollection('TIGER/2018/States')

# Load the secondary 'collection': power plants.
powerPlants = ee.FeatureCollection('WRI/GPPD/power_plants')

# Define a spatial filter as geometries that intersect.
spatialFilter = ee.Filter.intersects(**{
'leftField': '.geo',
'rightField': '.geo',
'maxError': 10
})

# Define a save all join.
saveAllJoin = ee.Join.saveAll(**{
'matchesKey': 'power_plants',
})

# Apply the join.
intersectJoined = saveAllJoin.apply(states, powerPlants, spatialFilter)

# Add power plant count per state as a property.
intersectJoined = intersectJoined.map(intersect)
# intersectJoined = intersectJoined.map(function(state) {
# # Get "power_plant" intersection list, count how many intersected this state.
# nPowerPlants = ee.List(state.get('power_plants')).size()
# # Return the state feature with a new property: power plant count.
# return state.set('n_power_plants', nPowerPlants)
# })

print(intersectJoined.getInfo())

# # Make a bar chart for the number of power plants per state.
# chart = ui.Chart.feature.byFeature(intersectJoined, 'NAME', 'n_power_plants') \
# .setChartType('ColumnChart') \
# .setSeriesNames({n_power_plants: 'Power plants'}) \
# .setOptions({
# title: 'Power plants per state',
# hAxis: {title: 'State'},
# vAxis: {title: 'Frequency'}})

# # Print the chart to the console.
# print(chart)
34 changes: 34 additions & 0 deletions Join/inverted_joins.py
@@ -0,0 +1,34 @@
import ee
from ee_plugin import Map

# Load a Landsat 8 image collection at a point of interest.
collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \
.filterBounds(ee.Geometry.Point(-122.09, 37.42))

# Define start and end dates with which to filter the collections.
april = '2014-04-01'
may = '2014-05-01'
june = '2014-06-01'
july = '2014-07-01'

# The primary collection is Landsat images from April to June.
primary = collection.filterDate(april, june)

# The secondary collection is Landsat images from May to July.
secondary = collection.filterDate(may, july)

# Use an equals filter to define how the collections match.
filter = ee.Filter.equals(**{
'leftField': 'system:index',
'rightField': 'system:index'
})

# Define the join.
invertedJoin = ee.Join.inverted()

# Apply the join.
invertedJoined = invertedJoin.apply(primary, secondary, filter)

# Display the result.
print('Inverted join: ', invertedJoined.getInfo())

42 changes: 42 additions & 0 deletions Join/save_all_joins.py
@@ -0,0 +1,42 @@
import ee
from ee_plugin import Map

# Load a primary 'collection': Landsat imagery.
primary = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \
.filterDate('2014-04-01', '2014-06-01') \
.filterBounds(ee.Geometry.Point(-122.092, 37.42))

# Load a secondary 'collection': MODIS imagery.
modSecondary = ee.ImageCollection('MODIS/006/MOD09GA') \
.filterDate('2014-03-01', '2014-07-01')

# Define an allowable time difference: two days in milliseconds.
twoDaysMillis = 2 * 24 * 60 * 60 * 1000

# Create a time filter to define a match as overlapping timestamps.
timeFilter = ee.Filter.Or(
ee.Filter.maxDifference(**{
'difference': twoDaysMillis,
'leftField': 'system:time_start',
'rightField': 'system:time_end'
}),
ee.Filter.maxDifference(**{
'difference': twoDaysMillis,
'leftField': 'system:time_end',
'rightField': 'system:time_start'
})
)

# Define the join.
saveAllJoin = ee.Join.saveAll(**{
'matchesKey': 'terra',
'ordering': 'system:time_start',
'ascending': True
})

# Apply the join.
landsatModis = saveAllJoin.apply(primary, modSecondary, timeFilter)

# Display the result.
print('Join.saveAll:', landsatModis.getInfo())

30 changes: 30 additions & 0 deletions Join/save_best_joins.py
@@ -0,0 +1,30 @@
import ee
from ee_plugin import Map

# Load a primary 'collection': Landsat imagery.
primary = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \
.filterDate('2014-04-01', '2014-06-01') \
.filterBounds(ee.Geometry.Point(-122.092, 37.42))

# Load a secondary 'collection': GRIDMET meteorological data
gridmet = ee.ImageCollection('IDAHO_EPSCOR/GRIDMET')

# Define a max difference filter to compare timestamps.
maxDiffFilter = ee.Filter.maxDifference(**{
'difference': 2 * 24 * 60 * 60 * 1000,
'leftField': 'system:time_start',
'rightField': 'system:time_start'
})

# Define the join.
saveBestJoin = ee.Join.saveBest(**{
'matchKey': 'bestImage',
'measureKey': 'timeDiff'
})

# Apply the join.
landsatMet = saveBestJoin.apply(primary, gridmet, maxDiffFilter)

# Print the result.
print(landsatMet.getInfo())

34 changes: 34 additions & 0 deletions Join/simple_joins.py
@@ -0,0 +1,34 @@
import ee
from ee_plugin import Map

# Load a Landsat 8 image collection at a point of interest.
collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \
.filterBounds(ee.Geometry.Point(-122.09, 37.42))

# Define start and end dates with which to filter the collections.
april = '2014-04-01'
may = '2014-05-01'
june = '2014-06-01'
july = '2014-07-01'

# The primary collection is Landsat images from April to June.
primary = collection.filterDate(april, june)

# The secondary collection is Landsat images from May to July.
secondary = collection.filterDate(may, july)

# Use an equals filter to define how the collections match.
filter = ee.Filter.equals(**{
'leftField': 'system:index',
'rightField': 'system:index'
})

# Create the join.
simpleJoin = ee.Join.simple()

# Apply the join.
simpleJoined = simpleJoin.apply(primary, secondary, filter)

# Display the result.
print('Simple join: ', simpleJoined.getInfo())

31 changes: 31 additions & 0 deletions Join/spatial_joins.py
@@ -0,0 +1,31 @@
import ee
from ee_plugin import Map

# Load a primary 'collection': protected areas (Yosemite National Park).
primary = ee.FeatureCollection("WCMC/WDPA/current/polygons") \
.filter(ee.Filter.eq('NAME', 'Yosemite National Park'))

# Load a secondary 'collection': power plants.
powerPlants = ee.FeatureCollection('WRI/GPPD/power_plants')

# Define a spatial filter, with distance 100 km.
distFilter = ee.Filter.withinDistance(**{
'distance': 100000,
'leftField': '.geo',
'rightField': '.geo',
'maxError': 10
})

# Define a saveAll join.
distSaveAll = ee.Join.saveAll(**{
'matchesKey': 'points',
'measureKey': 'distance'
})

# Apply the join.
spatialJoined = distSaveAll.apply(primary, powerPlants, distFilter)

# Print the result.
# print(spatialJoined.getInfo())
Map.centerObject(spatialJoined, 10)
Map.addLayer(ee.Image().paint(spatialJoined, 1, 3), {}, 'Spatial Joined')
2 changes: 1 addition & 1 deletion convert_js_to_python.py
Expand Up @@ -14,7 +14,7 @@ def dict_key_str(line):
'sigma', 'magnitude', 'size', 'connectedness', 'maxSize', 'eightConnected',
'reducer', 'labelBand', 'color', 'source', 'maxDistance', 'referenceImage',
'maxOffset', 'patchWidth', 'strokeWidth', 'width', 'geometry', 'scale', 'maxPixels',
'collection', 'selectors']
'collection', 'selectors', 'leftField', 'rightField']
for key in keys:
if ":" in line and key in line:
line = line.replace(key + ":", "'" + key + "':")
Expand Down

0 comments on commit 9ddf955

Please sign in to comment.