diff --git a/Join/inner_joins.py b/Join/inner_joins.py new file mode 100644 index 0000000..f772ecd --- /dev/null +++ b/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()) + diff --git a/Join/intersect.py b/Join/intersect.py new file mode 100644 index 0000000..a757a70 --- /dev/null +++ b/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) diff --git a/Join/inverted_joins.py b/Join/inverted_joins.py new file mode 100644 index 0000000..5ef38b5 --- /dev/null +++ b/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()) + diff --git a/Join/save_all_joins.py b/Join/save_all_joins.py new file mode 100644 index 0000000..7b915dc --- /dev/null +++ b/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()) + diff --git a/Join/save_best_joins.py b/Join/save_best_joins.py new file mode 100644 index 0000000..61db512 --- /dev/null +++ b/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()) + diff --git a/Join/simple_joins.py b/Join/simple_joins.py new file mode 100644 index 0000000..05fe8f3 --- /dev/null +++ b/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()) + diff --git a/Join/spatial_joins.py b/Join/spatial_joins.py new file mode 100644 index 0000000..4f64c93 --- /dev/null +++ b/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') diff --git a/convert_js_to_python.py b/convert_js_to_python.py index 7cbc5bb..d1f5fc5 100644 --- a/convert_js_to_python.py +++ b/convert_js_to_python.py @@ -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 + "':")