├── .github └── workflows │ └── lint_python.yml ├── .gitignore ├── .vscode └── settings.json ├── Algorithms ├── CloudMasking │ ├── landsat457_surface_reflectance.py │ ├── landsat8_surface_reflectance-copy.py │ ├── landsat8_toa_reflectance_qa_band.py │ ├── modis_surface_reflectance_qa_band.py │ └── sentinel-2.py ├── Segmentation │ └── segmentation_snic.py ├── center_pivot_irrigation_detector.py ├── landsat_cloud_score.py ├── landsat_path_row_limit.py ├── landsat_radiance.py ├── landsat_simple_composite.py ├── landsat_surface_reflectance.py ├── ntl_linear_fit.py ├── reduce_resolution.py ├── resampling.py └── sentinel-1_filtering.py ├── Array ├── array_images.py ├── array_sorting.py ├── array_transformations.py ├── decorrelation_stretch.py ├── eigen_analysis.py ├── linear_regression.py ├── quality_mosaic.py └── spectral_unmixing.py ├── AssetManagement ├── export_FeatureCollection.py ├── export_ImageCollection.py ├── export_TimeSeries.py ├── export_TimeSeries2.py ├── export_csv.py ├── export_raster.py ├── export_table.py ├── export_vector.py └── exporting_data.py ├── Basemaps └── qgis_basemaps.py ├── Datasets ├── Terrain │ ├── alos_chili.py │ ├── alos_global_dsm.py │ ├── alos_landforms.py │ ├── alos_mtpi.py │ ├── alos_topo_diversity.py │ ├── canada_dem.py │ ├── hydrosheds_dem.py │ ├── srtm.py │ ├── srtm_chili.py │ ├── srtm_landforms.py │ ├── srtm_mtpi.py │ ├── srtm_topo_diversity.py │ ├── us_lithology.py │ ├── us_ned.py │ ├── us_ned_chili.py │ ├── us_ned_landforms.py │ ├── us_ned_mtpi.py │ ├── us_ned_physio_diversity.py │ ├── us_ned_topo_diversity.py │ └── us_physiography.py ├── Vectors │ ├── global_land_ice_measurements.py │ ├── global_power_plant_database.py │ ├── international_boundary.py │ ├── landsat_wrs2_grid.py │ ├── resolve_ecoregions.py │ ├── us_census_blocks.py │ ├── us_census_counties.py │ ├── us_census_datasets.py │ ├── us_census_roads.py │ ├── us_census_states.py │ ├── us_census_tracts.py │ ├── us_census_zip_code.py │ ├── us_epa_ecoregions.py │ ├── usgs_watershed_boundary.py │ └── world_database_on_protected_areas.py ├── Water │ ├── glcf_global_inland_water.py │ ├── jrc_global_surface_water.py │ ├── jrc_metadata.py │ ├── jrc_monthly_history.py │ ├── jrc_monthly_recurrence.py │ ├── jrc_yearly_history.py │ └── usgs_watersheds.py ├── naip_imagery.py ├── sentinel-2.py ├── us_cropland.py ├── usda_naip.py └── usgs_nlcd.py ├── FeatureCollection ├── add_area_column.py ├── add_new_attribute.py ├── add_random_value_column.py ├── buffer.py ├── centroid.py ├── clipping.py ├── column_info.py ├── column_statistics.py ├── column_statistics_by_group.py ├── column_statistics_multiple.py ├── computed_area_filter.py ├── count_features.py ├── creating_feature.py ├── creating_feature_collection.py ├── distance.py ├── extract_image_by_polygon.py ├── filtering_feature_collection.py ├── from_ee_asset.py ├── from_polygons.py ├── geometric_operations.py ├── get_properties.py ├── idw_interpolation.py ├── join.py ├── map_function.py ├── merge_feature_collections.py ├── metadata_aggregation.py ├── minimum_bounding_geometry.py ├── random_samples.py ├── reduce_to_image.py ├── reducing_feature_collection.py ├── reverse_mask.py ├── search_by_buffer_distance.py ├── select_by_attributes.py ├── select_by_location.py ├── select_by_strings.py ├── select_columns.py ├── set_properties.py ├── simplify_polygons.py └── vector_symbology.py ├── Filter ├── filter_eq.py ├── filter_in_list.py ├── filter_neq.py ├── filter_range_contains.py ├── filter_string_contains.py ├── filter_string_ends_with.py └── filter_string_starts_with.py ├── Folium ├── README.md ├── ee-api-folium-setup.ipynb └── export-ee-data.ipynb ├── Gena ├── Sentinel-2.qgs ├── basic_image.py ├── contrib │ ├── palettes-test-crameri-dem.py │ └── utils-hillshadeRgb.py ├── hillshade.py ├── hillshade_and_water.py ├── landsat_median.py ├── map_add_features.py ├── map_center_object.py ├── map_center_object_no_zoom.py ├── map_get_bounds.py ├── map_get_center.py ├── map_set_center.py ├── map_set_zoom.py ├── palettes_crameri_oleron_dem.py └── test_sentinel2.py ├── GetStarted ├── 01_hello_world.py ├── 02_adding_data_to_qgis.py ├── 03_finding_images.py ├── 04_band_math.py ├── 05_map_function.py ├── 06_reducing.py ├── 07_image_statistics.py ├── 08_masking.py └── 09_a_complete_example.py ├── HowEarthEngineWorks ├── ClientVsServer.py ├── DeferredExecution.py └── Projections.py ├── Image ├── band_math.py ├── band_stats.py ├── canny_edge_detector.py ├── cell_statistics.py ├── center_pivot_irrigation_detector.py ├── clamp.py ├── clipping.py ├── composite_bands.py ├── conditional_operations.py ├── connected_pixel_count.py ├── convert_bands_to_image_collection.py ├── convolutions.py ├── cumulative_cost_mapping.py ├── download.py ├── edge_detection.py ├── expressions.py ├── extract_value_to_points.py ├── find_image_by_path_row.py ├── from_name.py ├── get_band_name_and_type.py ├── get_image_extent.py ├── get_image_id.py ├── get_image_resolution.py ├── gradients.py ├── hdr_landsat.py ├── hillshade.py ├── hough_transform.py ├── hsv_pan_sharpen.py ├── image_displacement.py ├── image_metadata.py ├── image_overview.py ├── image_patch_area.py ├── image_smoothing.py ├── image_stats_by_band.py ├── image_vis.py ├── landcover_cleanup.py ├── morphological_operations.py ├── normalized_difference.py ├── object_based.py ├── pansharpen.py ├── pixel_area.py ├── pixel_lon_lat.py ├── polynomial.py ├── reclassify.py ├── reduce_region.py ├── relational_operators.py ├── rename_bands.py ├── select_bands.py ├── set_image_properties.py ├── spectral_unmixing.py ├── symbology.py ├── texture.py ├── where.py ├── where_operators.py └── zero_crossing.py ├── ImageCollection ├── clipped_composite.py ├── convert_imagecollection_to_image.py ├── creating_monthly_imagery.py ├── expression_map.py ├── filtered_composite.py ├── filtering_by_band_names.py ├── filtering_by_calendar_range.py ├── filtering_by_metadata.py ├── filtering_collection.py ├── get_image_centroid.py ├── landsat_filtering.py ├── landsat_simple_composite.py ├── linear_fit.py ├── map_function.py ├── metadata.py ├── mosaicking.py ├── overview.py ├── reducing_collection.py ├── select_image_by_index.py └── sort_by_cloud_and_date.py ├── JavaScripts ├── ClippedComposite.js ├── ClippedComposite.py ├── FromName.js ├── FromName.py ├── NormalizedDifference.js └── NormalizedDifference.py ├── Join ├── inner_joins.py ├── intersect.py ├── inverted_joins.py ├── save_all_joins.py ├── save_best_joins.py ├── simple_joins.py └── spatial_joins.py ├── LICENSE ├── MachineLearning ├── cart_classifier.py ├── clustering.py ├── confusion_matrix.py └── svm_classifier.py ├── NAIP ├── filter_poly.py ├── filtering.py ├── from_name.py ├── loop_FeatureCollection.py ├── metadata.py ├── ndwi.py ├── ndwi_map.py ├── ndwi_single.py └── ndwi_timeseries.py ├── README.md ├── Reducer ├── convert_raster_to_vector.py ├── convert_vector_to_raster.py ├── image_reductions.py ├── linear_regression.py ├── linear_regression_more.py ├── mean_std_image.py ├── median_reducer.py ├── min_max_reducer.py ├── stats_by_group.py ├── stats_of_an_image_region.py ├── stats_of_columns.py ├── stats_of_image_neighborhoods.py ├── stats_of_image_regions.py ├── using_weights.py ├── weighted_reductions.py └── zonal_statistics.py ├── Tutorials ├── GlobalSurfaceWater │ ├── 1_water_occurrence.py │ ├── 2_water_occurrence_change_intensity.py │ └── 3_water_class_transition.py └── Keiko │ ├── fire_australia.py │ ├── glad_alert.py │ └── remove_colors.py ├── Visualization ├── color_by_attribute.py ├── hillshade.py ├── image_clipping.py ├── image_color_palettes.py ├── image_color_ramp.py ├── image_mosaicking.py ├── image_rgb_composite.py ├── image_stretch.py ├── image_thumbnail.py ├── landsat_symbology.py ├── ndvi_symbology.py ├── ndwi_symbology.py ├── nlcd_land_cover.py ├── nwi_wetlands_symbology.py ├── random_color_visualizer.py ├── rendering_categorical_maps.py ├── styled_layer_descriptors.py ├── terrain_visualization.py ├── us_counties.py ├── visualizing_feature_collection.py └── visualizing_geometries.py └── convert_js_to_python.py /.github/workflows/lint_python.yml: -------------------------------------------------------------------------------- 1 | name: lint_python 2 | on: [push, pull_request] 3 | jobs: 4 | lint_python: 5 | runs-on: ubuntu-latest 6 | steps: 7 | - uses: actions/checkout@v2 8 | - uses: actions/setup-python@v1 9 | - run: pip install flake8 10 | - run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics 11 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.pythonPath": "/home/qiusheng/.conda/envs/dl/bin/python" 3 | } -------------------------------------------------------------------------------- /Algorithms/CloudMasking/landsat457_surface_reflectance.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This example demonstrates the use of the Landsat 4, 5 or 7 5 | # surface reflectance QA band to mask clouds. 6 | 7 | # cloudMaskL457 = function(image) { 8 | def cloudMaskL457(image): 9 | qa = image.select('pixel_qa') 10 | # If the cloud bit (5) is set and the cloud confidence (7) is high 11 | # or the cloud shadow bit is set (3), then it's a bad pixel. 12 | cloud = qa.bitwiseAnd(1 << 5) \ 13 | .And(qa.bitwiseAnd(1 << 7)) \ 14 | .Or(qa.bitwiseAnd(1 << 3)) 15 | # Remove edge pixels that don't occur in all bands 16 | mask2 = image.mask().reduce(ee.Reducer.min()) 17 | return image.updateMask(cloud.Not()).updateMask(mask2) 18 | # } 19 | 20 | # Map the function over the collection and take the median. 21 | collection = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR') \ 22 | .filterDate('2010-04-01', '2010-07-30') 23 | 24 | composite = collection \ 25 | .map(cloudMaskL457) \ 26 | .median() 27 | 28 | # Display the results in a cloudy place. 29 | Map.setCenter(-6.2622, 53.3473, 12) 30 | Map.addLayer(composite, {'bands': ['B3', 'B2', 'B1'], 'min': 0, 'max': 3000}) 31 | -------------------------------------------------------------------------------- /Algorithms/CloudMasking/landsat8_surface_reflectance-copy.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This example demonstrates the use of the pixel QA band to mask 5 | # clouds in surface reflectance (SR) data. It is suitable 6 | # for use with any of the Landsat SR datasets. 7 | 8 | # Function to cloud mask from the pixel_qa band of Landsat 8 SR data. 9 | # function maskL8sr(image) { 10 | def maskL8sr(image): 11 | # Bits 3 and 5 are cloud shadow and cloud, respectively. 12 | cloudShadowBitMask = 1 << 3 13 | cloudsBitMask = 1 << 5 14 | 15 | # Get the pixel QA band. 16 | qa = image.select('pixel_qa') 17 | 18 | # Both flags should be set to zero, indicating clear conditions. 19 | mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0) \ 20 | .And(qa.bitwiseAnd(cloudsBitMask).eq(0)) 21 | 22 | # Return the masked image, scaled to reflectance, without the QA bands. 23 | return image.updateMask(mask).divide(10000) \ 24 | .select("B[0-9]*") \ 25 | .copyProperties(image, ["system:time_start"]) 26 | # } 27 | 28 | # Map the function over one year of data. 29 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR') \ 30 | .filterDate('2016-01-01', '2016-12-31') \ 31 | .map(maskL8sr) 32 | 33 | composite = collection.median() 34 | 35 | # Display the results. 36 | Map.addLayer(composite, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 0.3}, 'Image') 37 | -------------------------------------------------------------------------------- /Algorithms/CloudMasking/landsat8_toa_reflectance_qa_band.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This example demonstrates the use of the Landsat 8 QA band to mask clouds. 5 | 6 | # Function to mask clouds using the quality band of Landsat 8. 7 | # maskL8 = function(image) { 8 | def maskL8(image): 9 | qa = image.select('BQA') 10 | #/ Check that the cloud bit is off. 11 | # See https:#www.usgs.gov/land-resources/nli/landsat/landsat-collection-1-level-1-quality-assessment-band 12 | mask = qa.bitwiseAnd(1 << 4).eq(0) 13 | return image.updateMask(mask) 14 | # } 15 | 16 | # Map the function over one year of Landsat 8 TOA data and take the median. 17 | composite = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \ 18 | .filterDate('2016-01-01', '2016-12-31') \ 19 | .map(maskL8) \ 20 | .median() 21 | 22 | # Display the results in a cloudy place. 23 | Map.setCenter(114.1689, 22.2986, 12) 24 | Map.addLayer(composite, {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}, "Image") 25 | -------------------------------------------------------------------------------- /Algorithms/CloudMasking/sentinel-2.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This example uses the Sentinel-2 QA band to cloud mask 5 | # the collection. The Sentinel-2 cloud flags are less 6 | # selective, so the collection is also pre-filtered by the 7 | # CLOUDY_PIXEL_PERCENTAGE flag, to use only relatively 8 | # cloud-free granule. 9 | 10 | # Function to mask clouds using the Sentinel-2 QA band. 11 | # function maskS2clouds(image) { 12 | def maskS2clouds(image): 13 | qa = image.select('QA60') 14 | 15 | # Bits 10 and 11 are clouds and cirrus, respectively. 16 | cloudBitMask = 1 << 10 17 | cirrusBitMask = 1 << 11 18 | 19 | # Both flags should be set to zero, indicating clear conditions. 20 | mask = qa.bitwiseAnd(cloudBitMask).eq(0).And( 21 | qa.bitwiseAnd(cirrusBitMask).eq(0)) 22 | 23 | # Return the masked and scaled data, without the QA bands. 24 | return image.updateMask(mask).divide(10000) \ 25 | .select("B.*") \ 26 | .copyProperties(image, ["system:time_start"]) 27 | # } 28 | 29 | # Map the function over one year of data and take the median. 30 | # Load Sentinel-2 TOA reflectance data. 31 | collection = ee.ImageCollection('COPERNICUS/S2') \ 32 | .filterDate('2016-01-01', '2016-12-31') \ 33 | .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20)) \ 34 | .map(maskS2clouds) 35 | 36 | composite = collection.median() 37 | 38 | # Display the results. 39 | Map.addLayer(composite, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 0.3}, 'RGB') 40 | -------------------------------------------------------------------------------- /Algorithms/landsat_cloud_score.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a cloudy Landsat scene and display it. 5 | cloudy_scene = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140926') 6 | Map.centerObject(cloudy_scene) 7 | Map.addLayer(cloudy_scene, {'bands': ['B4', 'B3', 'B2'], 'max': 0.4}, 'TOA', False) 8 | 9 | # Add a cloud score band. It is automatically called 'cloud'. 10 | scored = ee.Algorithms.Landsat.simpleCloudScore(cloudy_scene) 11 | 12 | # Create a mask from the cloud score and combine it with the image mask. 13 | mask = scored.select(['cloud']).lte(20) 14 | 15 | # Apply the mask to the image and display the result. 16 | masked = cloudy_scene.updateMask(mask) 17 | Map.addLayer(masked, {'bands': ['B4', 'B3', 'B2'], 'max': 0.4}, 'masked') 18 | 19 | # Load a Landsat 8 composite and set the SENSOR_ID property. 20 | mosaic = ee.Image(ee.ImageCollection('LANDSAT/LC8_L1T_8DAY_TOA').first()) \ 21 | .set('SENSOR_ID', 'OLI_TIRS') 22 | 23 | # Cloud score the mosaic and display the result. 24 | scored_mosaic = ee.Algorithms.Landsat.simpleCloudScore(mosaic) 25 | Map.addLayer(scored_mosaic, {'bands': ['B4', 'B3', 'B2'], 'max': 0.4}, 26 | 'TOA mosaic', False) 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /Algorithms/landsat_path_row_limit.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Algorithms/landsat_path_row_limit.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | point = ee.Geometry.Point(-98.7011, 47.2624) 7 | collection = ee.ImageCollection('LANDSAT/LC8_L1T_TOA') \ 8 | .filterBounds(point) \ 9 | .filterDate('2016-01-01', '2018-12-31') 10 | 11 | # print(collection) 12 | 13 | new_col = ee.Algorithms.Landsat.pathRowLimit(collection, 15, 100) 14 | # print(new_col) 15 | 16 | median = new_col.median() 17 | vis = {'bands': ['B5', 'B4', 'B3'], 'max': 0.3} 18 | 19 | Map.setCenter(-98.7011, 47.2624, 10) 20 | Map.addLayer(median, vis, 'Median') 21 | -------------------------------------------------------------------------------- /Algorithms/landsat_radiance.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a raw Landsat scene and display it. 5 | raw = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318') 6 | Map.centerObject(raw, 10) 7 | Map.addLayer(raw, {'bands': ['B4', 'B3', 'B2'], 'min': 6000, 'max': 12000}, 'raw') 8 | 9 | # Convert the raw data to radiance. 10 | radiance = ee.Algorithms.Landsat.calibratedRadiance(raw) 11 | Map.addLayer(radiance, {'bands': ['B4', 'B3', 'B2'], 'max': 90}, 'radiance') 12 | 13 | # Convert the raw data to top-of-atmosphere reflectance. 14 | toa = ee.Algorithms.Landsat.TOA(raw) 15 | 16 | Map.addLayer(toa, {'bands': ['B4', 'B3', 'B2'], 'max': 0.2}, 'toa reflectance') 17 | 18 | -------------------------------------------------------------------------------- /Algorithms/landsat_simple_composite.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a raw Landsat 5 ImageCollection for a single year. 5 | collection = ee.ImageCollection('LANDSAT/LT05/C01/T1') \ 6 | .filterDate('2010-01-01', '2010-12-31') 7 | 8 | # Create a cloud-free composite with default parameters. 9 | composite = ee.Algorithms.Landsat.simpleComposite(collection) 10 | 11 | # Create a cloud-free composite with custom parameters for 12 | # cloud score threshold and percentile. 13 | customComposite = ee.Algorithms.Landsat.simpleComposite(**{ 14 | 'collection': collection, 15 | 'percentile': 75, 16 | 'cloudScoreRange': 5 17 | }) 18 | 19 | # Display the composites. 20 | Map.setCenter(-122.3578, 37.7726, 10) 21 | Map.addLayer(composite, {'bands': ['B4', 'B3', 'B2'], 'max': 128}, 'TOA composite') 22 | Map.addLayer(customComposite, {'bands': ['B4', 'B3', 'B2'], 'max': 128}, 23 | 'Custom TOA composite') 24 | 25 | -------------------------------------------------------------------------------- /Algorithms/landsat_surface_reflectance.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # You can access pre-computed surface reflectance images directly from the SR collections. For example, to load a Landsat 7 surface reflectance image, use: 5 | srImage = ee.Image('LANDSAT/LE07/C01/T1_SR/LE07_044034_19990707') 6 | 7 | # The surface reflectance datasets for Collection 1 Landsat 4 through 7 are: 8 | surfaceReflectanceL4 = ee.ImageCollection('LANDSAT/LT04/C01/T1_SR') 9 | surfaceReflectanceL5 = ee.ImageCollection('LANDSAT/LT05/C01/T1_SR') 10 | surfaceReflectanceL7 = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR') 11 | 12 | Map.centerObject(srImage, 9) 13 | Map.addLayer(srImage, {'bands': ['B4', 'B3', 'B2']}, 'Landsat Surface Reflectance') 14 | # Map.addLayer(srImage) 15 | # print(srImage.getInfo()) -------------------------------------------------------------------------------- /Algorithms/ntl_linear_fit.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Compute the trend of nighttime lights from DMSP. 5 | 6 | # Add a band containing image date as years since 1990. 7 | def createTimeBand(img): 8 | year = img.date().difference(ee.Date('1990-01-01'), 'year') 9 | return ee.Image(year).float().addBands(img) 10 | 11 | # function createTimeBand(img) { 12 | # year = img.date().difference(ee.Date('1990-01-01'), 'year') 13 | # return ee.Image(year).float().addBands(img) 14 | # } 15 | 16 | # Fit a linear trend to the nighttime lights collection. 17 | collection = ee.ImageCollection('NOAA/DMSP-OLS/CALIBRATED_LIGHTS_V4') \ 18 | .select('avg_vis') \ 19 | .map(createTimeBand) 20 | fit = collection.reduce(ee.Reducer.linearFit()) 21 | 22 | # Display a single image 23 | Map.addLayer(ee.Image(collection.select('avg_vis').first()), 24 | {'min': 0, 'max': 63}, 25 | 'stable lights first asset') 26 | 27 | # Display trend in red/blue, brightness in green. 28 | Map.setCenter(30, 45, 4) 29 | Map.addLayer(fit, 30 | {'min': 0, 'max': [0.18, 20, -0.18], 'bands': ['scale', 'offset', 'scale']}, 31 | 'stable lights trend') 32 | -------------------------------------------------------------------------------- /Algorithms/reduce_resolution.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a MODIS EVI image. 5 | modis = ee.Image(ee.ImageCollection('MODIS/006/MOD13A1').first()) \ 6 | .select('EVI') 7 | 8 | # Display the EVI image near La Honda, California. 9 | Map.setCenter(-122.3616, 37.5331, 12) 10 | Map.addLayer(modis, {'min': 2000, 'max': 5000}, 'MODIS EVI') 11 | 12 | # Get information about the MODIS projection. 13 | modisProjection = modis.projection() 14 | print('MODIS projection:', modisProjection.getInfo()) 15 | 16 | # Load and display forest cover data at 30 meters resolution. 17 | forest = ee.Image('UMD/hansen/global_forest_change_2015') \ 18 | .select('treecover2000') 19 | Map.addLayer(forest, {'max': 80}, 'forest cover 30 m') 20 | 21 | # Get the forest cover data at MODIS scale and projection. 22 | forestMean = forest \ 23 | .reduceResolution(**{ 24 | 'reducer': ee.Reducer.mean(), 25 | 'maxPixels': 1024 26 | }) \ 27 | .reproject(**{ 28 | 'crs': modisProjection 29 | }) 30 | 31 | # Display the aggregated, reprojected forest cover data. 32 | Map.addLayer(forestMean, {'max': 80}, 'forest cover at MODIS scale') 33 | -------------------------------------------------------------------------------- /Algorithms/resampling.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat image over San Francisco, California, UAS. 5 | landsat = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20160323') 6 | 7 | # Set display and visualization parameters. 8 | Map.setCenter(-122.37383, 37.6193, 15) 9 | visParams = {'bands': ['B4', 'B3', 'B2'], 'max': 0.3} 10 | 11 | # Display the Landsat image using the default nearest neighbor resampling. 12 | # when reprojecting to Mercator for the Code Editor map. 13 | Map.addLayer(landsat, visParams, 'original image') 14 | 15 | # Force the next reprojection on this image to use bicubic resampling. 16 | resampled = landsat.resample('bicubic') 17 | 18 | # Display the Landsat image using bicubic resampling. 19 | Map.addLayer(resampled, visParams, 'resampled') 20 | -------------------------------------------------------------------------------- /Algorithms/sentinel-1_filtering.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load the Sentinel-1 ImageCollection. 5 | sentinel1 = ee.ImageCollection('COPERNICUS/S1_GRD') \ 6 | .filterBounds(ee.Geometry.Point(-122.37383, 37.6193)) 7 | 8 | # Filter by metadata properties. 9 | vh = sentinel1 \ 10 | .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VV')) \ 11 | .filter(ee.Filter.listContains('transmitterReceiverPolarisation', 'VH')) \ 12 | .filter(ee.Filter.eq('instrumentMode', 'IW')) 13 | 14 | # Filter to get images from different look angles. 15 | vhAscending = vh.filter(ee.Filter.eq('orbitProperties_pass', 'ASCENDING')) 16 | vhDescending = vh.filter(ee.Filter.eq('orbitProperties_pass', 'DESCENDING')) 17 | 18 | # Create a composite from means at different polarizations and look angles. 19 | composite = ee.Image.cat([ 20 | vhAscending.select('VH').mean(), 21 | ee.ImageCollection(vhAscending.select('VV').merge(vhDescending.select('VV'))).mean(), 22 | vhDescending.select('VH').mean() 23 | ]).focal_median() 24 | 25 | # Display as a composite of polarization and backscattering characteristics. 26 | Map.setCenter(-122.37383, 37.6193, 10) 27 | Map.addLayer(composite, {'min': [-25, -20, -25], 'max': [0, 10, 0]}, 'composite') 28 | 29 | -------------------------------------------------------------------------------- /Array/array_images.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Define an Array of Tasseled Cap coefficients. 5 | coefficients = ee.Array([ 6 | [0.3037, 0.2793, 0.4743, 0.5585, 0.5082, 0.1863], 7 | [-0.2848, -0.2435, -0.5436, 0.7243, 0.0840, -0.1800], 8 | [0.1509, 0.1973, 0.3279, 0.3406, -0.7112, -0.4572], 9 | [-0.8242, 0.0849, 0.4392, -0.0580, 0.2012, -0.2768], 10 | [-0.3280, 0.0549, 0.1075, 0.1855, -0.4357, 0.8085], 11 | [0.1084, -0.9022, 0.4120, 0.0573, -0.0251, 0.0238] 12 | ]) 13 | 14 | # Load a Landsat 5 image, select the bands of interest. 15 | image = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_20081011') \ 16 | .select(['B1', 'B2', 'B3', 'B4', 'B5', 'B7']) 17 | 18 | # Make an Array Image, with a 1-D Array per pixel. 19 | arrayImage1D = image.toArray() 20 | 21 | # Make an Array Image with a 2-D Array per pixel, 6x1. 22 | arrayImage2D = arrayImage1D.toArray(1) 23 | 24 | # Do a matrix multiplication: 6x6 times 6x1. 25 | componentsImage = ee.Image(coefficients) \ 26 | .matrixMultiply(arrayImage2D) \ 27 | .arrayProject([0]) \ 28 | .arrayFlatten( 29 | [['brightness', 'greenness', 'wetness', 'fourth', 'fifth', 'sixth']]) 30 | 31 | # Display the first three bands of the result and the input imagery. 32 | vizParams = { 33 | 'bands': ['brightness', 'greenness', 'wetness'], 34 | 'min': -0.1, 'max': [0.5, 0.1, 0.1] 35 | } 36 | Map.setCenter(-122.3, 37.562, 10) 37 | Map.addLayer(image, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 0.5}, 'image') 38 | Map.addLayer(componentsImage, vizParams, 'components') 39 | 40 | -------------------------------------------------------------------------------- /AssetManagement/export_FeatureCollection.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | 5 | 6 | fromFT = ee.FeatureCollection('ft:1CLldB-ULPyULBT2mxoRNv7enckVF0gCQoD2oH7XP') 7 | polys = fromFT.geometry() 8 | centroid = polys.centroid() 9 | lng, lat = centroid.getInfo()['coordinates'] 10 | print("lng = {}, lat = {}".format(lng, lat)) 11 | Map.setCenter(lng, lat, 10) 12 | Map.addLayer(fromFT) 13 | 14 | taskParams = { 15 | 'driveFolder': 'image', 16 | 'fileFormat': 'KML' # CSV, KMZ, GeoJSON 17 | } 18 | 19 | # export all features in a FeatureCollection as one file 20 | task = ee.batch.Export.table(fromFT, 'export_fc', taskParams) 21 | task.start() 22 | 23 | # # export each feature in a FeatureCollection as an individual file 24 | # count = fromFT.size().getInfo() 25 | # for i in range(2, 2 + count): 26 | # fc = fromFT.filter(ee.Filter.eq('system:index', str(i))) 27 | # task = ee.batch.Export.table(fc, 'watershed-' + str(i), taskParams) 28 | # task.start() 29 | -------------------------------------------------------------------------------- /AssetManagement/export_ImageCollection.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | 5 | # USDA NAIP ImageCollection 6 | collection = ee.ImageCollection('USDA/NAIP/DOQQ') 7 | 8 | # create an roi 9 | polys = ee.Geometry.Polygon( 10 | [[[-99.29615020751953, 46.725459351792374], 11 | [-99.2116928100586, 46.72404725733022], 12 | [-99.21443939208984, 46.772037733479884], 13 | [-99.30267333984375, 46.77321343419932]]]) 14 | 15 | # create a FeatureCollection based on the roi and center the map 16 | centroid = polys.centroid() 17 | lng, lat = centroid.getInfo()['coordinates'] 18 | print("lng = {}, lat = {}".format(lng, lat)) 19 | Map.setCenter(lng, lat, 12) 20 | fc = ee.FeatureCollection(polys) 21 | 22 | # filter the ImageCollection using the roi 23 | naip = collection.filterBounds(polys) 24 | naip_2015 = naip.filterDate('2015-01-01', '2015-12-31') 25 | mosaic = naip_2015.mosaic() 26 | 27 | # print out the number of images in the ImageCollection 28 | count = naip_2015.size().getInfo() 29 | print("Count: ", count) 30 | 31 | # add the ImageCollection and the roi to the map 32 | vis = {'bands': ['N', 'R', 'G']} 33 | Map.addLayer(mosaic,vis) 34 | Map.addLayer(fc) 35 | 36 | # export the ImageCollection to Google Drive 37 | downConfig = {'scale': 30, "maxPixels": 1.0E13, 'driveFolder': 'image'} # scale means resolution. 38 | img_lst = naip_2015.toList(100) 39 | 40 | for i in range(0, count): 41 | image = ee.Image(img_lst.get(i)) 42 | name = image.get('system:index').getInfo() 43 | # print(name) 44 | task = ee.batch.Export.image(image, name, downConfig) 45 | task.start() 46 | 47 | -------------------------------------------------------------------------------- /AssetManagement/export_raster.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | 5 | 6 | collection = ee.ImageCollection('USDA/NAIP/DOQQ') 7 | 8 | polys = ee.Geometry.Polygon( 9 | [[[-99.29615020751953, 46.725459351792374], 10 | [-99.2116928100586, 46.72404725733022], 11 | [-99.21443939208984, 46.772037733479884], 12 | [-99.30267333984375, 46.77321343419932]]]) 13 | 14 | centroid = polys.centroid() 15 | lng, lat = centroid.getInfo()['coordinates'] 16 | print("lng = {}, lat = {}".format(lng, lat)) 17 | 18 | lng_lat = ee.Geometry.Point(lng, lat) 19 | naip = collection.filterBounds(polys) 20 | naip_2015 = naip.filterDate('2015-01-01', '2015-12-31') 21 | ppr = naip_2015.mosaic() 22 | 23 | count = naip_2015.size().getInfo() 24 | print("Count: ", count) 25 | 26 | # print(naip_2015.size().getInfo()) 27 | # vis = {'bands': ['N', 'R', 'G']} 28 | # Map.setCenter(lng, lat, 12) 29 | # Map.addLayer(ppr,vis) 30 | # Map.addLayer(polys) 31 | 32 | downConfig = {'scale': 30, "maxPixels": 1.0E13, 'driveFolder': 'image'} # scale means resolution. 33 | img_lst = naip_2015.toList(100) 34 | 35 | for i in range(0, count): 36 | image = ee.Image(img_lst.get(i)) 37 | name = image.get('system:index').getInfo() 38 | # print(name) 39 | task = ee.batch.Export.image(image, name, downConfig) 40 | task.start() 41 | 42 | -------------------------------------------------------------------------------- /AssetManagement/export_table.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | 5 | 6 | fromFT = ee.FeatureCollection('ft:1CLldB-ULPyULBT2mxoRNv7enckVF0gCQoD2oH7XP') 7 | polys = fromFT.geometry() 8 | centroid = polys.centroid() 9 | lng, lat = centroid.getInfo()['coordinates'] 10 | # print("lng = {}, lat = {}".format(lng, lat)) 11 | # Map.setCenter(lng, lat, 10) 12 | # Map.addLayer(fromFT) 13 | 14 | count = fromFT.size().getInfo() 15 | Map.setCenter(lng, lat, 10) 16 | 17 | for i in range(2, 2 + count): 18 | fc = fromFT.filter(ee.Filter.eq('system:index', str(i))) 19 | Map.addLayer(fc) -------------------------------------------------------------------------------- /AssetManagement/export_vector.py: -------------------------------------------------------------------------------- 1 | import os, ee, datetime, csv, time 2 | 3 | 4 | 5 | startTime = datetime.datetime(2001, 1, 1) 6 | endTime = datetime.datetime(2001, 2, 1) 7 | 8 | lst = ee.ImageCollection('FORA0125_H002').filterDate(startTime, endTime) 9 | 10 | # Get the time series at these points. 11 | points = [ee.Geometry.Point(-85.16516, 30.850000000000001)] 12 | collection = ee.FeatureCollection(points) 13 | 14 | 15 | # Extract the values by running reduceRegions over each image in the image collection. 16 | def myfunction(i): 17 | return i.reduceRegions(collection, 'first') 18 | 19 | 20 | values = lst.map(myfunction).flatten() 21 | 22 | # Turn the result into a feature collection and export it. 23 | taskParams = { 24 | 'driveFolder': 'image', 25 | 'driveFileNamePrefix': 'TylerTest', 26 | 'fileFormat': 'CSV' 27 | } 28 | 29 | MyTry = ee.batch.Export.table(values, 'lst_timeseries', taskParams) 30 | MyTry.start() 31 | state = MyTry.status()['state'] 32 | while state in ['READY', 'RUNNING']: 33 | print(state, '...') 34 | time.sleep(1) 35 | state = MyTry.status()['state'] 36 | print('Done.', MyTry.status()) 37 | -------------------------------------------------------------------------------- /AssetManagement/exporting_data.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | 5 | 6 | image = ee.Image('USDA/NAIP/DOQQ/m_4609915_sw_14_1_20100629') 7 | 8 | # downConfig = {'scale':30, "maxPixels": 1.0E13, 'driveFolder':'test', 'CRS': 'EPGS:31983', 'region': roiExample } 9 | # 10 | # task = ee.batch.Export.image(image.select( ['B2', 'B3' ,'B4', 'B5', 'B6']).toDouble(), 'sirgas20023sPy', downConfig) 11 | # task.start() 12 | 13 | downConfig = {'scale': 10, "maxPixels": 1.0E13, 'driveFolder': 'image'} # scale means resolution. 14 | task = ee.batch.Export.image(image, "10m", downConfig) 15 | task.start() -------------------------------------------------------------------------------- /Datasets/Terrain/alos_chili.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/Global/ALOS_CHILI') 5 | alosChili = dataset.select('constant') 6 | alosChiliVis = { 7 | 'min': 0.0, 8 | 'max': 255.0, 9 | } 10 | Map.setCenter(-105.8636, 40.3439, 11) 11 | Map.addLayer(alosChili, alosChiliVis, 'ALOS CHILI') 12 | -------------------------------------------------------------------------------- /Datasets/Terrain/alos_global_dsm.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('JAXA/ALOS/AW3D30_V1_1') 5 | elevation = dataset.select('AVE') 6 | elevationVis = { 7 | 'min': 0.0, 8 | 'max': 4000.0, 9 | 'palette': ['0000ff', '00ffff', 'ffff00', 'ff0000', 'ffffff'], 10 | } 11 | Map.setCenter(136.85, 37.37, 4) 12 | Map.addLayer(elevation, elevationVis, 'Elevation') 13 | -------------------------------------------------------------------------------- /Datasets/Terrain/alos_landforms.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/Global/ALOS_landforms') 5 | landforms = dataset.select('constant') 6 | landformsVis = { 7 | 'min': 11.0, 8 | 'max': 42.0, 9 | 'palette': [ 10 | '141414', '383838', '808080', 'EBEB8F', 'F7D311', 'AA0000', 'D89382', 11 | 'DDC9C9', 'DCCDCE', '1C6330', '68AA63', 'B5C98E', 'E1F0E5', 'a975ba', 12 | '6f198c' 13 | ], 14 | } 15 | Map.setCenter(-105.58, 40.5498, 11) 16 | Map.addLayer(landforms, landformsVis, 'Landforms') 17 | -------------------------------------------------------------------------------- /Datasets/Terrain/alos_mtpi.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/Global/ALOS_mTPI') 5 | alosMtpi = dataset.select('AVE') 6 | alosMtpiVis = { 7 | 'min': -200.0, 8 | 'max': 200.0, 9 | 'palette': ['0b1eff', '4be450', 'fffca4', 'ffa011', 'ff0000'], 10 | } 11 | Map.setCenter(-105.8636, 40.3439, 11) 12 | Map.addLayer(alosMtpi, alosMtpiVis, 'ALOS mTPI') 13 | -------------------------------------------------------------------------------- /Datasets/Terrain/alos_topo_diversity.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/Global/ALOS_topoDiversity') 5 | alosTopographicDiversity = dataset.select('constant') 6 | alosTopographicDiversityVis = { 7 | 'min': 0.0, 8 | 'max': 1.0, 9 | } 10 | Map.setCenter(-111.313, 39.724, 6) 11 | Map.addLayer( 12 | alosTopographicDiversity, alosTopographicDiversityVis, 13 | 'ALOS Topographic Diversity') 14 | -------------------------------------------------------------------------------- /Datasets/Terrain/canada_dem.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.ImageCollection('NRCan/CDEM') 5 | elevation = dataset.select('elevation').mosaic() 6 | elevationVis = { 7 | 'min': -50.0, 8 | 'max': 1500.0, 9 | 'palette': ['0905ff', 'ffefc4', 'ffffff'], 10 | } 11 | Map.setCenter(-139.3643, 63.3213, 9) 12 | Map.addLayer(elevation, elevationVis, 'Elevation') 13 | -------------------------------------------------------------------------------- /Datasets/Terrain/hydrosheds_dem.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('WWF/HydroSHEDS/03CONDEM') 5 | elevation = dataset.select('b1') 6 | elevationVis = { 7 | 'min': -50.0, 8 | 'max': 3000.0, 9 | 'gamma': 2.0, 10 | } 11 | Map.setCenter(-121.652, 38.022, 8) 12 | Map.addLayer(elevation, elevationVis, 'Elevation') 13 | 14 | 15 | dataset = ee.Image('WWF/HydroSHEDS/03VFDEM') 16 | elevation = dataset.select('b1') 17 | elevationVis = { 18 | 'min': -50.0, 19 | 'max': 3000.0, 20 | 'gamma': 2.0, 21 | } 22 | Map.setCenter(-121.652, 38.022, 8) 23 | Map.addLayer(elevation, elevationVis, 'Elevation Void Filled') 24 | -------------------------------------------------------------------------------- /Datasets/Terrain/srtm.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | image = ee.Image('srtm90_v4') 4 | # path = image.getDownloadUrl({ 5 | # 'scale': 30, 6 | # 'crs': 'EPSG:4326', 7 | # 'region': '[[-120, 35], [-119, 35], [-119, 34], [-120, 34]]' 8 | # }) 9 | vis_params = {'min': 0, 'max': 3000} 10 | Map.addLayer(image, vis_params, 'SRTM') -------------------------------------------------------------------------------- /Datasets/Terrain/srtm_chili.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/Global/SRTM_CHILI') 5 | srtmChili = dataset.select('constant') 6 | srtmChiliVis = { 7 | 'min': 0.0, 8 | 'max': 255.0, 9 | } 10 | Map.setCenter(-105.8636, 40.3439, 11) 11 | Map.addLayer(srtmChili, srtmChiliVis, 'SRTM CHILI') 12 | -------------------------------------------------------------------------------- /Datasets/Terrain/srtm_landforms.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/Global/SRTM_landforms') 5 | landforms = dataset.select('constant') 6 | landformsVis = { 7 | 'min': 11.0, 8 | 'max': 42.0, 9 | 'palette': [ 10 | '141414', '383838', '808080', 'EBEB8F', 'F7D311', 'AA0000', 'D89382', 11 | 'DDC9C9', 'DCCDCE', '1C6330', '68AA63', 'B5C98E', 'E1F0E5', 'a975ba', 12 | '6f198c' 13 | ], 14 | } 15 | Map.setCenter(-105.58, 40.5498, 11) 16 | Map.addLayer(landforms, landformsVis, 'Landforms') 17 | -------------------------------------------------------------------------------- /Datasets/Terrain/srtm_mtpi.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/Global/SRTM_mTPI') 5 | srtmMtpi = dataset.select('elevation') 6 | srtmMtpiVis = { 7 | 'min': -200.0, 8 | 'max': 200.0, 9 | 'palette': ['0b1eff', '4be450', 'fffca4', 'ffa011', 'ff0000'], 10 | } 11 | Map.setCenter(-105.8636, 40.3439, 11) 12 | Map.addLayer(srtmMtpi, srtmMtpiVis, 'SRTM mTPI') 13 | -------------------------------------------------------------------------------- /Datasets/Terrain/srtm_topo_diversity.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/Global/SRTM_topoDiversity') 5 | srtmTopographicDiversity = dataset.select('constant') 6 | srtmTopographicDiversityVis = { 7 | 'min': 0.0, 8 | 'max': 1.0, 9 | } 10 | Map.setCenter(-111.313, 39.724, 6) 11 | Map.addLayer( 12 | srtmTopographicDiversity, srtmTopographicDiversityVis, 13 | 'SRTM Topographic Diversity') 14 | -------------------------------------------------------------------------------- /Datasets/Terrain/us_lithology.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/US/lithology') 5 | lithology = dataset.select('b1') 6 | lithologyVis = { 7 | 'min': 0.0, 8 | 'max': 20.0, 9 | 'palette': [ 10 | '356EFF', 'ACB6DA', 'D6B879', '313131', 'EDA800', '616161', 'D6D6D6', 11 | 'D0DDAE', 'B8D279', 'D5D378', '141414', '6DB155', '9B6D55', 'FEEEC9', 12 | 'D6B879', '00B7EC', 'FFDA90', 'F8B28C' 13 | ], 14 | } 15 | Map.setCenter(-105.8636, 40.3439, 11) 16 | Map.addLayer(lithology, lithologyVis, 'Lithology') 17 | -------------------------------------------------------------------------------- /Datasets/Terrain/us_ned.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('USGS/NED') 5 | elevation = dataset.select('elevation') 6 | elevationVis = { 7 | 'min': 0.0, 8 | 'max': 4000.0, 9 | 'gamma': 1.6, 10 | } 11 | Map.setCenter(-100.55, 40.71, 5) 12 | Map.addLayer(elevation, elevationVis, 'Elevation') 13 | -------------------------------------------------------------------------------- /Datasets/Terrain/us_ned_chili.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/US/CHILI') 5 | usChili = dataset.select('constant') 6 | usChiliVis = { 7 | 'min': 0.0, 8 | 'max': 255.0, 9 | } 10 | Map.setCenter(-105.8636, 40.3439, 11) 11 | Map.addLayer(usChili, usChiliVis, 'US CHILI') 12 | -------------------------------------------------------------------------------- /Datasets/Terrain/us_ned_landforms.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/US/landforms') 5 | landforms = dataset.select('constant') 6 | landformsVis = { 7 | 'min': 11.0, 8 | 'max': 42.0, 9 | 'palette': [ 10 | '141414', '383838', '808080', 'EBEB8F', 'F7D311', 'AA0000', 'D89382', 11 | 'DDC9C9', 'DCCDCE', '1C6330', '68AA63', 'B5C98E', 'E1F0E5', 'a975ba', 12 | '6f198c' 13 | ], 14 | } 15 | Map.setCenter(-105.58, 40.5498, 11) 16 | Map.addLayer(landforms, landformsVis, 'NED Landforms') 17 | -------------------------------------------------------------------------------- /Datasets/Terrain/us_ned_mtpi.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/US/mTPI') 5 | usMtpi = dataset.select('elevation') 6 | usMtpiVis = { 7 | 'min': -200.0, 8 | 'max': 200.0, 9 | 'palette': ['0b1eff', '4be450', 'fffca4', 'ffa011', 'ff0000'], 10 | } 11 | Map.setCenter(-105.8636, 40.3439, 11) 12 | Map.addLayer(usMtpi, usMtpiVis, 'US mTPI') 13 | -------------------------------------------------------------------------------- /Datasets/Terrain/us_ned_physio_diversity.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/US/physioDiversity') 5 | physiographicDiversity = dataset.select('b1') 6 | physiographicDiversityVis = { 7 | 'min': 0.0, 8 | 'max': 1.0, 9 | } 10 | Map.setCenter(-94.625, 39.825, 7) 11 | Map.addLayer( 12 | physiographicDiversity, physiographicDiversityVis, 13 | 'Physiographic Diversity') 14 | -------------------------------------------------------------------------------- /Datasets/Terrain/us_ned_topo_diversity.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/US/topoDiversity') 5 | usTopographicDiversity = dataset.select('constant') 6 | usTopographicDiversityVis = { 7 | 'min': 0.0, 8 | 'max': 1.0, 9 | } 10 | Map.setCenter(-111.313, 39.724, 6) 11 | Map.addLayer( 12 | usTopographicDiversity, usTopographicDiversityVis, 13 | 'US Topographic Diversity') 14 | -------------------------------------------------------------------------------- /Datasets/Terrain/us_physiography.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('CSP/ERGo/1_0/US/physiography') 5 | physiography = dataset.select('constant') 6 | physiographyVis = { 7 | 'min': 1100.0, 8 | 'max': 4220.0, 9 | } 10 | Map.setCenter(-105.4248, 40.5242, 8) 11 | Map.addLayer(physiography, physiographyVis, 'Physiography') 12 | -------------------------------------------------------------------------------- /Datasets/Vectors/global_land_ice_measurements.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/global_land_ice_measurements.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | dataset = ee.FeatureCollection('GLIMS/current') 7 | visParams = { 8 | 'palette': ['gray', 'cyan', 'blue'], 9 | 'min': 0.0, 10 | 'max': 10.0, 11 | 'opacity': 0.8, 12 | } 13 | 14 | image = ee.Image().float().paint(dataset, 'area') 15 | Map.setCenter(-35.618, 66.743, 7) 16 | Map.addLayer(image, visParams, 'GLIMS/current') 17 | # Map.addLayer(dataset, {}, 'for Inspector', False) 18 | -------------------------------------------------------------------------------- /Datasets/Vectors/international_boundary.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/international_boundary.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | # LSIB: Large Scale International Boundary Polygons, Simplified 7 | 8 | # dataset = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017') 9 | # styleParams = { 10 | # 'fillColor': 'b5ffb4', 11 | # 'color': '00909F', 12 | # 'width': 3.0, 13 | # } 14 | # countries = dataset.style(**styleParams) 15 | # Map.addLayer(countries, {}, 'USDOS/LSIB_SIMPLE/2017') 16 | 17 | 18 | # LSIB: Large Scale International Boundary Polygons, Detailed 19 | dataset = ee.FeatureCollection('USDOS/LSIB/2013') 20 | visParams = { 21 | 'palette': ['f5ff64', 'b5ffb4', 'beeaff', 'ffc0e8', '8e8dff', 'adadad'], 22 | 'min': 0.0, 23 | 'max': 894.0, 24 | 'opacity': 0.8, 25 | } 26 | image = ee.Image().float().paint(dataset, 'iso_num') 27 | Map.addLayer(image, visParams, 'USDOS/LSIB/2013') 28 | # Map.addLayer(dataset, {}, 'for Inspector', False) 29 | -------------------------------------------------------------------------------- /Datasets/Vectors/landsat_wrs2_grid.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/landsat_wrs2_grid.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | dataset = ee.FeatureCollection('projects/google/wrs2_descending') 7 | 8 | empty = ee.Image().byte() 9 | 10 | Map.setCenter(-78, 36, 8) 11 | Map.addLayer(empty.paint(dataset, 0, 2), {}, 'Landsat WRS-2 grid') -------------------------------------------------------------------------------- /Datasets/Vectors/resolve_ecoregions.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/resolve_ecoregions.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | 7 | def set_color(f): 8 | c = ee.String(f.get('COLOR')).slice(1) 9 | return f \ 10 | .set('R', ee.Number.parse(c.slice(0, 2), 16)) \ 11 | .set('G', ee.Number.parse(c.slice(2, 4), 16)) \ 12 | .set('B', ee.Number.parse(c.slice(4, 6), 16)) 13 | 14 | 15 | fc = ee.FeatureCollection('RESOLVE/ECOREGIONS/2017') \ 16 | .map(lambda f: set_color(f)) 17 | 18 | base = ee.Image(0).mask(0).toInt8() 19 | Map.addLayer(base.paint(fc, 'R') 20 | .addBands(base.paint(fc, 'G') 21 | .addBands(base.paint(fc, 'B'))), {'gamma': 0.3}) 22 | 23 | 24 | 25 | # # Load a FeatureCollection from a table dataset: 'RESOLVE' ecoregions. 26 | # ecoregions = ee.FeatureCollection('RESOLVE/ECOREGIONS/2017') 27 | 28 | # # Display as default and with a custom color. 29 | # Map.addLayer(ecoregions, {}, 'default display', False) 30 | # Map.addLayer(ecoregions, {'color': 'FF0000'}, 'colored', False) 31 | 32 | 33 | # Map.addLayer(ecoregions.draw(**{'color': '006600', 'strokeWidth': 5}), {}, 'drawn', False) 34 | 35 | 36 | # # Create an empty image into which to paint the features, cast to byte. 37 | # empty = ee.Image().byte() 38 | 39 | # # Paint all the polygon edges with the same number and 'width', display. 40 | # outline = empty.paint(**{ 41 | # 'featureCollection': ecoregions, 42 | # 'color': 1, 43 | # 'width': 3 44 | # }) 45 | # Map.addLayer(outline, {'palette': 'FF0000'}, 'edges') -------------------------------------------------------------------------------- /Datasets/Vectors/us_census_blocks.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/us_census_blocks.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | dataset = ee.FeatureCollection('TIGER/2010/Blocks') 7 | visParams = { 8 | 'min': 0.0, 9 | 'max': 700.0, 10 | 'palette': ['black', 'brown', 'yellow', 'orange', 'red'] 11 | } 12 | 13 | # Turn the strings into numbers 14 | dataset = dataset.map(lambda f: f.set('pop10', ee.Number.parse(f.get('pop10')))) 15 | 16 | image = ee.Image().float().paint(dataset, 'pop10') 17 | 18 | Map.setCenter(-73.99172, 40.74101, 13) 19 | Map.addLayer(image, visParams, 'TIGER/2010/Blocks') 20 | # Map.addLayer(dataset, {}, 'for Inspector', False) 21 | -------------------------------------------------------------------------------- /Datasets/Vectors/us_census_counties.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/us_census_counties.py 2 | 3 | #!/usr/bin/env python 4 | """Display US Counties. 5 | 6 | """ 7 | 8 | # import datetime 9 | import ee 10 | from ee_plugin import Map 11 | 12 | Map.setCenter(-110, 40, 5) 13 | states = ee.FeatureCollection('TIGER/2018/States') 14 | # .filter(ee.Filter.eq('STUSPS', 'MN')) 15 | # // Turn the strings into numbers 16 | states = states.map(lambda f: f.set('STATEFP', ee.Number.parse(f.get('STATEFP')))) 17 | 18 | state_image = ee.Image().float().paint(states, 'STATEFP') 19 | 20 | visParams = { 21 | 'palette': ['purple', 'blue', 'green', 'yellow', 'orange', 'red'], 22 | 'min': 0, 23 | 'max': 50, 24 | 'opacity': 0.8, 25 | }; 26 | 27 | counties = ee.FeatureCollection('TIGER/2016/Counties') 28 | 29 | image = ee.Image().paint(states, 0, 2) 30 | Map.setCenter(-99.844, 37.649, 5) 31 | # Map.addLayer(image, {'palette': 'FF0000'}, 'TIGER/2018/States') 32 | Map.addLayer(image, visParams, 'TIGER/2016/States'); 33 | Map.addLayer(ee.Image().paint(counties, 0, 1), {}, 'TIGER/2016/Counties') 34 | -------------------------------------------------------------------------------- /Datasets/Vectors/us_census_roads.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/us_census_roads.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | fc = ee.FeatureCollection('TIGER/2016/Roads') 7 | Map.setCenter(-73.9596, 40.7688, 12) 8 | Map.addLayer(fc, {}, 'Census roads') 9 | -------------------------------------------------------------------------------- /Datasets/Vectors/us_census_states.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/us_census_states.py 2 | 3 | #!/usr/bin/env python 4 | """Display US States. 5 | 6 | """ 7 | 8 | import ee 9 | from ee_plugin import Map 10 | 11 | fc = ee.FeatureCollection('TIGER/2018/States') 12 | # .filter(ee.Filter.eq('STUSPS', 'MN')) 13 | 14 | image = ee.Image().paint(fc, 0, 2) 15 | Map.setCenter(-99.844, 37.649, 5) 16 | Map.addLayer(image, {'palette': 'FF0000'}, 'TIGER/2018/States') 17 | # Map.addLayer(fc, {}, 'US States') 18 | 19 | -------------------------------------------------------------------------------- /Datasets/Vectors/us_census_tracts.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/us_census_tracts.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | dataset = ee.FeatureCollection('TIGER/2010/Tracts_DP1') 7 | visParams = { 8 | 'min': 0, 9 | 'max': 4000, 10 | 'opacity': 0.8, 11 | } 12 | 13 | # Turn the strings into numbers 14 | dataset = dataset.map(lambda f: f.set('shape_area', ee.Number.parse(f.get('dp0010001')))) 15 | 16 | # Map.setCenter(-103.882, 43.036, 8) 17 | image = ee.Image().float().paint(dataset, 'dp0010001') 18 | 19 | Map.addLayer(image, visParams, 'TIGER/2010/Tracts_DP1') 20 | # Map.addLayer(dataset, {}, 'for Inspector', False) -------------------------------------------------------------------------------- /Datasets/Vectors/us_census_zip_code.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/us_census_zip_code.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | dataset = ee.FeatureCollection('TIGER/2010/ZCTA5') 7 | visParams = { 8 | 'palette': ['black', 'purple', 'blue', 'green', 'yellow', 'orange', 'red'], 9 | 'min': 500000, 10 | 'max': 1000000000, 11 | } 12 | 13 | zctaOutlines = ee.Image().float().paint(**{ 14 | 'featureCollection': dataset, 15 | 'color': 'black', 16 | 'width': 1 17 | }) 18 | 19 | image = ee.Image().float().paint(dataset, 'ALAND10') 20 | # Map.setCenter(-93.8008, 40.7177, 6) 21 | Map.addLayer(image, visParams, 'TIGER/2010/ZCTA5') 22 | Map.addLayer(zctaOutlines, {}, 'borders') -------------------------------------------------------------------------------- /Datasets/Vectors/us_epa_ecoregions.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/us_epa_ecoregions.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | dataset = ee.FeatureCollection('EPA/Ecoregions/2013/L3') 7 | visParams = { 8 | 'palette': ['0a3b04', '1a9924', '15d812'], 9 | 'min': 23.0, 10 | 'max': 3.57e+11, 11 | 'opacity': 0.8, 12 | } 13 | image = ee.Image().float().paint(dataset, 'shape_area') 14 | Map.setCenter(-99.814, 40.166, 5) 15 | Map.addLayer(image, visParams, 'EPA/Ecoregions/2013/L3') 16 | # Map.addLayer(dataset, {}, 'for Inspector', False) 17 | 18 | 19 | dataset = ee.FeatureCollection('EPA/Ecoregions/2013/L4') 20 | visParams = { 21 | 'palette': ['0a3b04', '1a9924', '15d812'], 22 | 'min': 0.0, 23 | 'max': 67800000000.0, 24 | 'opacity': 0.8, 25 | } 26 | image = ee.Image().float().paint(dataset, 'shape_area') 27 | Map.setCenter(-99.814, 40.166, 5) 28 | Map.addLayer(image, visParams, 'EPA/Ecoregions/2013/L4') 29 | # Map.addLayer(dataset, {}, 'for Inspector', False) 30 | -------------------------------------------------------------------------------- /Datasets/Vectors/world_database_on_protected_areas.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/Vectors/world_database_on_protected_areas.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | dataset = ee.FeatureCollection('WCMC/WDPA/current/polygons') 7 | visParams = { 8 | 'palette': ['2ed033', '5aff05', '67b9ff', '5844ff', '0a7618', '2c05ff'], 9 | 'min': 0.0, 10 | 'max': 1550000.0, 11 | 'opacity': 0.8, 12 | } 13 | image = ee.Image().float().paint(dataset, 'REP_AREA') 14 | Map.setCenter(41.104, -17.724, 6) 15 | Map.addLayer(image, visParams, 'WCMC/WDPA/current/polygons') 16 | # Map.addLayer(dataset, {}, 'for Inspector', False) 17 | 18 | 19 | dataset = ee.FeatureCollection('WCMC/WDPA/current/points') 20 | styleParams = { 21 | 'color': '#4285F4', 22 | 'width': 1, 23 | } 24 | protectedAreaPoints = dataset.style(**styleParams) 25 | # Map.setCenter(110.57, 0.88, 4) 26 | Map.addLayer(protectedAreaPoints, {}, 'WCMC/WDPA/current/points') 27 | -------------------------------------------------------------------------------- /Datasets/Water/glcf_global_inland_water.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.ImageCollection('GLCF/GLS_WATER') 5 | water = dataset.select('water').mosaic() 6 | waterVis = { 7 | 'min': 1.0, 8 | 'max': 4.0, 9 | 'palette': ['FAFAFA', '00C5FF', 'DF73FF', '828282', 'CCCCCC'], 10 | } 11 | Map.setCenter(-79.3094, 44.5693, 8) 12 | Map.addLayer(water, waterVis, 'Water') 13 | -------------------------------------------------------------------------------- /Datasets/Water/jrc_global_surface_water.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | dataset = ee.Image('JRC/GSW1_1/GlobalSurfaceWater') 4 | occurrence = dataset.select('occurrence'); 5 | occurrenceVis = {'min': 0.0, 'max': 100.0, 'palette': ['ffffff', 'ffbbbb', '0000ff']} 6 | Map.setCenter(59.414, 45.182, 6) 7 | Map.addLayer(occurrence, occurrenceVis, 'Occurrence') -------------------------------------------------------------------------------- /Datasets/Water/jrc_metadata.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.Image('JRC/GSW1_1/Metadata') 5 | detectionsObservations = dataset.select(['detections', 'valid_obs', 'total_obs']) 6 | visParams = { 7 | 'min': 100.0, 8 | 'max': 900.0, 9 | } 10 | Map.setCenter(4.72, -2.48, 2) 11 | Map.addLayer(detectionsObservations, visParams, 'Detections/Observations') 12 | -------------------------------------------------------------------------------- /Datasets/Water/jrc_monthly_history.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.ImageCollection('JRC/GSW1_1/MonthlyHistory') \ 5 | .filter(ee.Filter.date('2015-01-01', '2015-12-31')) 6 | water = dataset.select('water').mosaic() 7 | waterVis = { 8 | 'min': 0.0, 9 | 'max': 2.0, 10 | 'palette': ['ffffff', 'fffcb8', '0905ff'], 11 | } 12 | Map.setCenter(-58.999, -3.373, 7) 13 | Map.addLayer(water, waterVis, 'Water') 14 | -------------------------------------------------------------------------------- /Datasets/Water/jrc_monthly_recurrence.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.ImageCollection('JRC/GSW1_1/MonthlyRecurrence').first() 5 | monthlyRecurrence = dataset.select('monthly_recurrence') 6 | monthlyRecurrenceVis = { 7 | 'min': 0.0, 8 | 'max': 100.0, 9 | 'palette': ['ffffff', 'ffbbbb', '0000ff'], 10 | } 11 | Map.setCenter(-51.482, -0.835, 9) 12 | Map.addLayer(monthlyRecurrence, monthlyRecurrenceVis, 'Monthly Recurrence') 13 | -------------------------------------------------------------------------------- /Datasets/Water/jrc_yearly_history.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.ImageCollection('JRC/GSW1_1/YearlyHistory') \ 5 | .filter(ee.Filter.date('2015-01-01', '2015-12-31')) 6 | waterClass = dataset.select('waterClass') 7 | waterClassVis = { 8 | 'min': 0.0, 9 | 'max': 3.0, 10 | 'palette': ['cccccc', 'ffffff', '99d9ea', '0000ff'], 11 | } 12 | Map.setCenter(59.414, 45.182, 7) 13 | Map.addLayer(waterClass, waterClassVis, 'Water Class') 14 | -------------------------------------------------------------------------------- /Datasets/naip_imagery.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | image = ee.Image('USDA/NAIP/DOQQ/m_4609915_sw_14_1_20100629') 4 | Map.addLayer(image, {'bands': ['N', 'R', 'G']}, 'NAIP') -------------------------------------------------------------------------------- /Datasets/sentinel-2.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | img = ee.Image("COPERNICUS/S2_SR/20191115T074201_20191115T075706_T37MBM") 4 | ndvi = img.normalizedDifference(['B8','B4']) 5 | pal = ["red","yellow","green"] 6 | Map.setCenter(36.9,-7.7, 12) 7 | Map.addLayer(ndvi,{'min':0,'max':0.8,'palette':pal},'NDVI') -------------------------------------------------------------------------------- /Datasets/us_cropland.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets/us_cropland.py 2 | 3 | # Metadata: https://developers.google.com/earth-engine/datasets/catalog/USDA_NASS_CDL 4 | 5 | import ee 6 | from ee_plugin import Map 7 | 8 | dataset = ee.ImageCollection('USDA/NASS/CDL') \ 9 | .filter(ee.Filter.date('2017-01-01', '2018-12-31')) \ 10 | .first() 11 | cropLandcover = dataset.select('cropland') 12 | Map.setCenter(-100.55, 40.71, 4) 13 | Map.addLayer(cropLandcover, {}, 'Crop Landcover') 14 | -------------------------------------------------------------------------------- /Datasets/usda_naip.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | roi = ee.FeatureCollection('TIGER/2018/States') \ 5 | .filter(ee.Filter.eq('STUSPS', 'ND')); 6 | 7 | dataset = ee.ImageCollection('USDA/NAIP/DOQQ') \ 8 | .filter(ee.Filter.date('2016-01-01', '2017-12-31'))\ 9 | .filterBounds(roi) 10 | TrueColor = dataset.select(['N', 'R', 'G']).mosaic() 11 | TrueColorVis = { 12 | 'min': 0.0, 13 | 'max': 255.0, 14 | } 15 | # Map.setCenter(-73.9958, 40.7278, 15) 16 | Map.centerObject(roi, 8) 17 | Map.addLayer(TrueColor, TrueColorVis, 'True Color') 18 | -------------------------------------------------------------------------------- /FeatureCollection/add_area_column.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/add_area_column.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | fromFT = ee.FeatureCollection("users/wqs/Pipestem/Pipestem_HUC10") 7 | # This function computes the feature's geometry area and adds it as a property. 8 | def addArea(feature): 9 | return feature.set({'areaHa': feature.geometry().area().divide(100 * 100)}) 10 | 11 | 12 | # Map the area getting function over the FeatureCollection. 13 | areaAdded = fromFT.map(addArea) 14 | # Print the first feature from the collection with the added property. 15 | 16 | first = areaAdded.first() 17 | print('First feature: ', first.getInfo()) 18 | print("areaHa: ", first.get("areaHa").getInfo()) 19 | -------------------------------------------------------------------------------- /FeatureCollection/add_new_attribute.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This function creates a new property that is the sum of two existing properties. 5 | def addField(feature): 6 | sum = ee.Number(feature.get('property1')).add(feature.get('property2')) 7 | return feature.set({'sum': sum}) 8 | 9 | # Create a FeatureCollection from a list of Features. 10 | features = ee.FeatureCollection([ 11 | ee.Feature(ee.Geometry.Point(-122.4536, 37.7403), 12 | {'property1': 100, 'property2': 100}), 13 | ee.Feature(ee.Geometry.Point(-118.2294, 34.039), 14 | {'property1': 200, 'property2': 300}), 15 | ]) 16 | 17 | # Map the function over the collection. 18 | featureCollection = features.map(addField) 19 | 20 | # Print the entire FeatureCollection. 21 | print(featureCollection.getInfo()) 22 | 23 | # Print a selected property of one Feature. 24 | print(featureCollection.first().get('sum').getInfo()) 25 | 26 | 27 | -------------------------------------------------------------------------------- /FeatureCollection/add_random_value_column.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/add_random_value_column.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | HUC10 = ee.FeatureCollection("USGS/WBD/2017/HUC10") 7 | HUC08 = ee.FeatureCollection('USGS/WBD/2017/HUC08') 8 | roi = HUC08.filter(ee.Filter.eq('name', 'Pipestem')) 9 | 10 | # Map.centerObject(roi, 10) 11 | # Map.addLayer(ee.Image().paint(roi, 0, 3), {}, 'HUC08') 12 | 13 | # # select polygons intersecting the roi 14 | roi2 = HUC10.filter(ee.Filter.contains(**{'leftValue': roi.geometry(), 'rightField': '.geo'})) 15 | # Map.addLayer(ee.Image().paint(roi2, 0, 2), {'palette': 'blue'}, 'HUC10') 16 | 17 | # roi = HUC10.filter(ee.Filter.stringContains(**{'leftField': 'huc10', 'rightValue': '10160002'})) 18 | roi3 = roi2.randomColumn('random') 19 | # # print(roi3) 20 | # Map.addLayer(roi3) 21 | 22 | print("Random value: ", roi3.first().get('random').getInfo()) -------------------------------------------------------------------------------- /FeatureCollection/buffer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Buffer Example. 3 | 4 | Display the area within 2 kilometers of any San Francisco BART station. 5 | """ 6 | 7 | import ee 8 | from ee_plugin import Map 9 | 10 | Map.setCenter(-122.4, 37.7, 11) 11 | 12 | bart_stations = ee.FeatureCollection('GOOGLE/EE/DEMOS/bart-locations') 13 | buffered = bart_stations.map(lambda f: f.buffer(2000)) 14 | unioned = buffered.union() 15 | 16 | Map.addLayer(unioned, {'color': '800080'}, "BART stations") 17 | -------------------------------------------------------------------------------- /FeatureCollection/centroid.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Create a geodesic polygon. 5 | polygon = ee.Geometry.Polygon([ 6 | [[-5, 40], [65, 40], [65, 60], [-5, 60], [-5, 60]] 7 | ]) 8 | 9 | # Compute a buffer of the polygon. 10 | buffer = polygon.buffer(0.1) 11 | 12 | # Compute the centroid of the polygon. 13 | centroid = polygon.centroid() 14 | Map.addLayer(buffer, {}, 'buffer') 15 | Map.addLayer(centroid, {'color': 'red'}, 'centroid') 16 | 17 | -------------------------------------------------------------------------------- /FeatureCollection/clipping.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/clipping.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | roi = ee.Geometry.Polygon( 7 | [[[-73.99891354682285, 40.74560250077625], 8 | [-73.99891354682285, 40.74053023068626], 9 | [-73.98749806525547, 40.74053023068626], 10 | [-73.98749806525547, 40.74560250077625]]]) 11 | 12 | 13 | fc = ee.FeatureCollection('TIGER/2016/Roads').filterBounds(roi) 14 | 15 | clipped = fc.map(lambda f: f.intersection(roi)) 16 | 17 | Map.centerObject(ee.FeatureCollection(roi), 17) 18 | Map.addLayer(ee.Image().paint(roi, 0, 2), {'palette': 'yellow'}, 'ROI') 19 | # Map.setCenter(-73.9596, 40.7688, 12) 20 | Map.addLayer(ee.Image().paint(clipped, 0, 3), {'palette': 'red'}, 'clipped') 21 | Map.addLayer(fc, {}, 'Census roads', False) 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /FeatureCollection/column_info.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | def getCols(tableMetadata): 5 | return tableMetadata.columns 6 | 7 | # Import a protected areas point feature collection. 8 | wdpa = ee.FeatureCollection("WCMC/WDPA/current/points") 9 | 10 | # Define a function to print metadata column names and datatypes. This function 11 | # is intended to be applied by the `evaluate` method which provides the 12 | # function a client-side dictionary allowing the 'columns' object of the 13 | # feature collection metadata to be subset by dot notation or bracket notation 14 | # (`tableMetadata['columns']`). 15 | # function getCols(tableMetadata) { 16 | # print(tableMetadata.columns) 17 | # } 18 | 19 | # Fetch collection metadata (`.limit(0)`) and apply the 20 | # previously defined function using `evaluate()`. The printed object is a 21 | # dictionary where keys are column names and values are datatypes. 22 | # print(getCols(wdpa.limit(0)).getInfo()) 23 | -------------------------------------------------------------------------------- /FeatureCollection/column_statistics.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/column_statistics.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | fromFT = ee.FeatureCollection("users/wqs/Pipestem/Pipestem_HUC10") 7 | geom = fromFT.geometry() 8 | Map.centerObject(fromFT) 9 | Map.addLayer(ee.Image().paint(geom, 0, 2), {}, 'Watersheds') 10 | 11 | print(fromFT.aggregate_stats('AreaSqKm')) 12 | 13 | total_area = fromFT.reduceColumns(**{ 14 | 'reducer': ee.Reducer.sum(), 15 | 'selectors': ['AreaSqKm'] 16 | # weightSelectors: ['weight'] 17 | }).getInfo() 18 | 19 | print("Total area: ", total_area) 20 | -------------------------------------------------------------------------------- /FeatureCollection/column_statistics_by_group.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/column_statistics_by_group.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | # Load a collection of US census blocks. 7 | blocks = ee.FeatureCollection('TIGER/2010/Blocks') 8 | 9 | # Compute sums of the specified properties, grouped by state code. 10 | sums = blocks \ 11 | .filter(ee.Filter.And( 12 | ee.Filter.neq('pop10', {}), 13 | ee.Filter.neq('housing10', {}))) \ 14 | .reduceColumns(**{ 15 | 'selectors': ['pop10', 'housing10', 'statefp10'], 16 | 'reducer': ee.Reducer.sum().repeat(2).group(**{ 17 | 'groupField': 2, 18 | 'groupName': 'state-code', 19 | }) 20 | }) 21 | 22 | # Print the resultant Dictionary. 23 | print(sums.getInfo()) 24 | 25 | -------------------------------------------------------------------------------- /FeatureCollection/column_statistics_multiple.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/column_statistics_multiple.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | fromFT = ee.FeatureCollection("users/wqs/Pipestem/Pipestem_HUC10") 7 | geom = fromFT.geometry() 8 | Map.centerObject(fromFT) 9 | Map.addLayer(ee.Image().paint(geom, 0, 2), {}, 'Watersheds') 10 | 11 | stats = fromFT.reduceColumns(**{ 12 | 'reducer': ee.Reducer.sum().repeat(2), 13 | 'selectors': ['AreaSqKm', 'AreaAcres'] 14 | # weightSelectors: ['weight'] 15 | }).getInfo() 16 | 17 | print(stats) 18 | -------------------------------------------------------------------------------- /FeatureCollection/computed_area_filter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Computed area filter example. 3 | 4 | Find US counties smaller than 3k square kilometers in area. 5 | """ 6 | 7 | import ee 8 | from ee_plugin import Map 9 | 10 | 11 | Map.setCenter(-119.7, 38.26, 7) 12 | 13 | # counties = ee.FeatureCollection( 14 | # 'ft:1pjtcfSKIbYbj4wRcBjc0Bb6NB-sQRI-L2nIzHiU') 15 | counties = ee.FeatureCollection("TIGER/2016/Counties") 16 | counties_with_area = counties.map(lambda f: f.set({'area': f.area()})) 17 | small_counties = counties_with_area.filterMetadata('area', 'less_than', 3e9) 18 | 19 | Map.addLayer(small_counties, {'color': '900000'}, "Small US counties") 20 | -------------------------------------------------------------------------------- /FeatureCollection/count_features.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Count features example. 3 | 4 | Count Panoramio photos near SF that mention bridges. 5 | """ 6 | 7 | import ee 8 | from ee_plugin import Map 9 | 10 | 11 | Map.setCenter(-122.39, 37.7857, 12) 12 | 13 | photos_near_sf = ee.FeatureCollection('GOOGLE/EE/DEMOS/sf-photo-locations') 14 | bridge_photos = photos_near_sf.filter( 15 | ee.Filter().Or(ee.Filter.stringContains('title', 'Bridge'), 16 | ee.Filter.stringContains('title', 'bridge'))) 17 | 18 | Map.addLayer(photos_near_sf, {'color': '0040b0'}, "Photos near SF") 19 | Map.addLayer(bridge_photos, {'color': 'e02070'}, "Bridge photos") 20 | 21 | print ('There are %d bridge photos around SF.' % 22 | bridge_photos.size().getInfo()) 23 | -------------------------------------------------------------------------------- /FeatureCollection/creating_feature.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Create an ee.Geometry. 5 | polygon = ee.Geometry.Polygon([ 6 | [[-35, -10], [35, -10], [35, 10], [-35, 10], [-35, -10]] 7 | ]) 8 | 9 | # Create a Feature from the Geometry. 10 | polyFeature = ee.Feature(polygon, {'foo': 42, 'bar': 'tart'}) 11 | 12 | 13 | print(polyFeature.getInfo()) 14 | Map.addLayer(polyFeature, {}, 'feature') 15 | 16 | -------------------------------------------------------------------------------- /FeatureCollection/creating_feature_collection.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Make a list of Features. 5 | features = [ 6 | ee.Feature(ee.Geometry.Rectangle(30.01, 59.80, 30.59, 60.15), {'name': 'Voronoi'}), 7 | ee.Feature(ee.Geometry.Point(-73.96, 40.781), {'name': 'Thiessen'}), 8 | ee.Feature(ee.Geometry.Point(6.4806, 50.8012), {'name': 'Dirichlet'}) 9 | ] 10 | 11 | # Create a FeatureCollection from the list and print it. 12 | fromList = ee.FeatureCollection(features) 13 | print(fromList.getInfo()) 14 | 15 | # Create a FeatureCollection from a single geometry and print it. 16 | fromGeom = ee.FeatureCollection(ee.Geometry.Point(16.37, 48.225)) 17 | print(fromGeom.getInfo()) 18 | -------------------------------------------------------------------------------- /FeatureCollection/distance.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Collection.distance example. 5 | # Computes the distance to the nearest feature in a collection. 6 | 7 | # Construct a FeatureCollection from a list of geometries. 8 | fc = ee.FeatureCollection([ 9 | ee.Geometry.Point(-72.94411, 41.32902), 10 | ee.Geometry.Point(-72.94411, 41.33402), 11 | ee.Geometry.Point(-72.94411, 41.33902), 12 | # The geometries do not need to be the same type. 13 | ee.Geometry.LineString( 14 | -72.93411, 41.30902, -72.93411, 41.31902, -72.94411, 41.31902) 15 | ]) 16 | 17 | # Compute distance from the dfeatures, to a max of 1000 meters. 18 | distance = fc.distance(1000, 100) 19 | 20 | Map.setCenter(-72.94, 41.32, 13) 21 | Map.addLayer(distance, {'min': 0, 'max': 1000, 'palette': ['yellow', 'red']}, 'distance') 22 | Map.addLayer(fc, {}, 'Features') 23 | -------------------------------------------------------------------------------- /FeatureCollection/extract_image_by_polygon.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Extract landsant image based on individual polygon 5 | def extract_landsat(feature): 6 | geom = ee.Feature(feature).geometry() 7 | image = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR') \ 8 | .filterDate('2019-01-01', '2019-12-31') \ 9 | .filterBounds(geom) \ 10 | .median() \ 11 | .clip(geom) 12 | return image 13 | 14 | # Select the first five U.S. counties 15 | counties = ee.FeatureCollection('TIGER/2018/Counties').toList(5) 16 | Map.setCenter(-110, 40, 5) 17 | Map.addLayer(ee.Image().paint(counties, 0, 2), {'palette': 'red'}, "Selected Counties") 18 | 19 | # Extract Landsat image for each county 20 | images = counties.map(extract_landsat) 21 | 22 | # Add images to map 23 | for i in range(0, 5): 24 | image = ee.Image(images.get(i)) 25 | vis = {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 3000, 'gamma': 1.4} 26 | Map.addLayer(image, vis, 'Image ' + str(i+1)) 27 | 28 | 29 | -------------------------------------------------------------------------------- /FeatureCollection/filtering_feature_collection.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | def cal_area(feature): 5 | num = ee.Number.parse(feature.get('areasqkm')) 6 | return feature.set('areasqkm', num) 7 | 8 | # Load watersheds from a data table. 9 | sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06') \ 10 | # .map(cal_area) 11 | 12 | # Define a region roughly covering the continental US. 13 | continentalUS = ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29) 14 | 15 | # Filter the table geographically: only watersheds in the continental US. 16 | filtered = sheds.filterBounds(continentalUS) \ 17 | .map(cal_area) 18 | 19 | # Check the number of watersheds after filtering for location. 20 | print('Count after filter:', filtered.size().getInfo()) 21 | 22 | # Filter to get only larger continental US watersheds. 23 | largeSheds = filtered.filter(ee.Filter.gt('areasqkm', 25000)) 24 | 25 | # Check the number of watersheds after filtering for size and location. 26 | print('Count after filtering by size:', largeSheds.size().getInfo()) 27 | 28 | -------------------------------------------------------------------------------- /FeatureCollection/from_ee_asset.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Create a FeatureCollection from an Earth Engine Table. 5 | 6 | # Load census roads. 7 | roads = ee.FeatureCollection('TIGER/2016/Roads') 8 | 9 | # Get only interstates. 10 | interstates = roads.filter(ee.Filter.eq('rttyp', 'I')) 11 | 12 | # Get only surface roads. 13 | surfaceRoads = roads.filter(ee.Filter.eq('rttyp', 'M')) 14 | 15 | # Display the roads in different colors. 16 | Map.addLayer(surfaceRoads, {'color': 'black'}, 'surface roads') 17 | Map.addLayer(interstates, {'color': 'red'}, 'interstates') 18 | 19 | -------------------------------------------------------------------------------- /FeatureCollection/from_polygons.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Create and render a feature collection from polygons.""" 3 | 4 | import ee 5 | from ee_plugin import Map 6 | 7 | 8 | Map.setCenter(-107, 41, 6) 9 | 10 | fc = ee.FeatureCollection([ 11 | ee.Feature( 12 | ee.Geometry.Polygon( 13 | [[-109.05, 41], [-109.05, 37], [-102.05, 37], [-102.05, 41]]), 14 | {'name': 'Colorado', 'fill': 1}), 15 | ee.Feature( 16 | ee.Geometry.Polygon( 17 | [[-114.05, 37.0], [-109.05, 37.0], [-109.05, 41.0], 18 | [-111.05, 41.0], [-111.05, 42.0], [-114.05, 42.0]]), 19 | {'name': 'Utah', 'fill': 2}) 20 | ]) 21 | 22 | # Fill, then outline the polygons into a blank image. 23 | image1 = ee.Image(0).mask(0).toByte() 24 | image2 = image1.paint(fc, 'fill') # Get color from property named 'fill' 25 | image3 = image2.paint(fc, 3, 5) # Outline using color 3, width 5. 26 | 27 | Map.addLayer(image3, { 28 | 'palette': ['000000', 'FF0000', '00FF00', '0000FF'], 29 | 'max': 3, 30 | 'opacity': 0.5 31 | }, "Colorado & Utah") 32 | -------------------------------------------------------------------------------- /FeatureCollection/geometric_operations.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Create two circular geometries. 5 | poly1 = ee.Geometry.Point([-50, 30]).buffer(1e6) 6 | poly2 = ee.Geometry.Point([-40, 30]).buffer(1e6) 7 | 8 | # Display polygon 1 in red and polygon 2 in blue. 9 | Map.setCenter(-45, 30) 10 | Map.addLayer(poly1, {'color': 'FF0000'}, 'poly1') 11 | Map.addLayer(poly2, {'color': '0000FF'}, 'poly2') 12 | 13 | # Compute the intersection, display it in blue. 14 | intersection = poly1.intersection(poly2, ee.ErrorMargin(1)) 15 | Map.addLayer(intersection, {'color': '00FF00'}, 'intersection') 16 | 17 | # Compute the union, display it in magenta. 18 | union = poly1.union(poly2, ee.ErrorMargin(1)) 19 | Map.addLayer(union, {'color': 'FF00FF'}, 'union') 20 | 21 | # Compute the difference, display in yellow. 22 | diff1 = poly1.difference(poly2, ee.ErrorMargin(1)) 23 | Map.addLayer(diff1, {'color': 'FFFF00'}, 'diff1') 24 | 25 | # Compute symmetric difference, display in black. 26 | symDiff = poly1.symmetricDifference(poly2, ee.ErrorMargin(1)) 27 | Map.addLayer(symDiff, {'color': '000000'}, 'symmetric difference') 28 | 29 | -------------------------------------------------------------------------------- /FeatureCollection/get_properties.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load input imagery: Landsat 7 5-year composite. 5 | image = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012') 6 | 7 | # Load a FeatureCollection of counties in Maine. 8 | maineCounties = ee.FeatureCollection('TIGER/2016/Counties') \ 9 | .filter(ee.Filter.eq('STATEFP', '23')) 10 | 11 | # Add reducer output to the Features in the collection. 12 | maineMeansFeatures = image.reduceRegions(**{ 13 | 'collection': maineCounties, 14 | 'reducer': ee.Reducer.mean(), 15 | 'scale': 30, 16 | }) 17 | 18 | feature = ee.Feature(maineMeansFeatures.first()).select(image.bandNames()) 19 | # print(feature.getInfo()) 20 | properties = feature.propertyNames() 21 | 22 | # Print the first feature, to illustrate the result. 23 | print(feature.toDictionary(properties).getInfo()) 24 | 25 | -------------------------------------------------------------------------------- /FeatureCollection/join.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """FeatureCollection Join example. 3 | 4 | Show parks in San Francisco within 2 kilometers of a BART station. 5 | """ 6 | 7 | import ee 8 | from ee_plugin import Map 9 | 10 | Map.setCenter(-122.45, 37.75, 13) 11 | 12 | bart = ee.FeatureCollection('GOOGLE/EE/DEMOS/bart-locations') 13 | parks = ee.FeatureCollection('GOOGLE/EE/DEMOS/sf-parks') 14 | buffered_bart = bart.map(lambda f: f.buffer(2000)) 15 | 16 | join_filter = ee.Filter.withinDistance(2000, '.geo', None, '.geo') 17 | close_parks = ee.Join.simple().apply(parks, bart, join_filter) 18 | 19 | Map.addLayer(buffered_bart, {'color': 'b0b0b0'}, "BART Stations") 20 | Map.addLayer(close_parks, {'color': '008000'}, "Parks") 21 | -------------------------------------------------------------------------------- /FeatureCollection/map_function.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | def addArea(feature): 5 | return feature.set({'areaHa': feature.geometry().area().divide(100 * 100)}) 6 | 7 | # Load watersheds from a data table. 8 | sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06') 9 | 10 | # This function computes the feature's geometry area and adds it as a property. 11 | # addArea = function(feature) { 12 | # return feature.set({areaHa: feature.geometry().area().divide(100 * 100)}) 13 | # } 14 | 15 | # Map the area getting function over the FeatureCollection. 16 | areaAdded = sheds.map(addArea) 17 | 18 | # Print the first feature from the collection with the added property. 19 | print('First feature:', areaAdded.first().getInfo()) 20 | 21 | -------------------------------------------------------------------------------- /FeatureCollection/merge_feature_collections.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/merge_feature_collections.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | 7 | SD = ee.FeatureCollection('TIGER/2018/States') \ 8 | .filter(ee.Filter.eq('STUSPS', 'SD')) 9 | 10 | ND = ee.FeatureCollection('TIGER/2018/States') \ 11 | .filter(ee.Filter.eq('STUSPS', 'ND')) 12 | 13 | states = SD.merge(ND) 14 | Map.centerObject(states, 6) 15 | Map.addLayer(ee.Image().paint(states, 0, 2), {}, 'Dakotas') 16 | # print(states.size().getInfo()) 17 | -------------------------------------------------------------------------------- /FeatureCollection/metadata_aggregation.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | def cal_area(feature): 5 | num = ee.Number.parse(feature.get('areasqkm')) 6 | return feature.set('areasqkm', num) 7 | 8 | # Load watersheds from a data table. 9 | sheds = ee.FeatureCollection('USGS/WBD/2017/HUC06') \ 10 | .filterBounds(ee.Geometry.Rectangle(-127.18, 19.39, -62.75, 51.29)) \ 11 | .map(cal_area) 12 | 13 | # Display the table and print its first element. 14 | # Map.addLayer(sheds, {}, 'watersheds') 15 | Map.addLayer(ee.Image().paint(sheds, 1, 2), {}, 'watersheds') 16 | print('First watershed', sheds.first().getInfo()) 17 | 18 | # Print the number of watersheds. 19 | print('Count:', sheds.size().getInfo()) 20 | 21 | # Print stats for an area property. 22 | # print('Area stats:', sheds.aggregate_stats('areasqkm').getInfo()) 23 | -------------------------------------------------------------------------------- /FeatureCollection/minimum_bounding_geometry.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/minimum_bounding_geometry.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | HUC10 = ee.FeatureCollection("USGS/WBD/2017/HUC10") 7 | HUC08 = ee.FeatureCollection('USGS/WBD/2017/HUC08') 8 | roi = HUC08.filter(ee.Filter.eq('name', 'Pipestem')) 9 | 10 | Map.centerObject(roi, 10) 11 | Map.addLayer(ee.Image().paint(roi, 0, 1), {}, 'HUC8') 12 | 13 | bound = ee.Geometry(roi.geometry()).bounds() 14 | Map.addLayer(ee.Image().paint(bound, 0, 1), {'palette': 'red'}, "Minimum bounding geometry") 15 | -------------------------------------------------------------------------------- /FeatureCollection/random_samples.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Define an arbitrary region in which to compute random points. 5 | region = ee.Geometry.Rectangle(-119.224, 34.669, -99.536, 50.064) 6 | 7 | # Create 1000 random points in the region. 8 | randomPoints = ee.FeatureCollection.randomPoints(region) 9 | 10 | # Display the points. 11 | Map.centerObject(randomPoints) 12 | Map.addLayer(randomPoints, {}, 'random points') 13 | -------------------------------------------------------------------------------- /FeatureCollection/reduce_to_image.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Example of FeatureCollection.reduceToImage() 5 | 6 | # Define a feature collection with a value we want to average. 7 | fc = ee.FeatureCollection([ 8 | ee.Feature( 9 | ee.Geometry.Rectangle( 10 | -122.4550, 37.8035, 11 | -122.4781, 37.7935), 12 | {'value': 0}), 13 | ee.Feature( 14 | ee.Geometry.Polygon([ 15 | [-122.4427, 37.8027], 16 | [-122.4587, 37.7987], 17 | [-122.4440, 37.7934]]), 18 | {'value': 1}) 19 | ]) 20 | 21 | # Reduce the collection to an image, where each pixel 22 | # is the mean of the 'value' property in all features 23 | # intersecting that pixel. 24 | image_reduced = fc.reduceToImage(['value'], 'mean') 25 | 26 | Map.setCenter(-122.4561, 37.7983, 14) 27 | Map.addLayer(image_reduced, { 28 | 'min': 0, 29 | 'max': 1, 30 | 'palette': ['008800', '00FF00']}, "Image") 31 | -------------------------------------------------------------------------------- /FeatureCollection/reverse_mask.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Reverse mask a region. 3 | 4 | Create an image that masks everything except for the specified polygon. 5 | """ 6 | 7 | import ee 8 | from ee_plugin import Map 9 | 10 | Map.setCenter(-100, 40, 4) 11 | 12 | fc = (ee.FeatureCollection('RESOLVE/ECOREGIONS/2017') 13 | .filter(ee.Filter().eq('ECO_NAME', 'Great Basin shrub steppe'))) 14 | 15 | # Start with a black image. 16 | empty = ee.Image(0).toByte() 17 | # Fill and outline the polygons in two colors 18 | filled = empty.paint(fc, 2) 19 | both = filled.paint(fc, 1, 5) 20 | # Mask off everything that matches the fill color. 21 | result = both.mask(filled.neq(2)) 22 | 23 | Map.addLayer(result, { 24 | 'palette': '000000,FF0000', 25 | 'max': 1, 26 | 'opacity': 0.5 27 | }, "Basin") 28 | -------------------------------------------------------------------------------- /FeatureCollection/search_by_buffer_distance.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/search_by_buffer_distance.py 2 | 3 | #!/usr/bin/env python 4 | """FeatureCollection Join example. 5 | 6 | Show parks in San Francisco within 2 kilometers of a BART station. 7 | """ 8 | 9 | import ee 10 | from ee_plugin import Map 11 | 12 | Map.setCenter(-122.45, 37.75, 13) 13 | 14 | bart = ee.FeatureCollection('GOOGLE/EE/DEMOS/bart-locations') 15 | parks = ee.FeatureCollection('GOOGLE/EE/DEMOS/sf-parks') 16 | buffered_bart = bart.map(lambda f: f.buffer(2000)) 17 | 18 | join_filter = ee.Filter.withinDistance(2000, '.geo', None, '.geo') 19 | close_parks = ee.Join.simple().apply(parks, bart, join_filter) 20 | 21 | Map.addLayer(buffered_bart, {'color': 'b0b0b0'}, "BART Stations") 22 | Map.addLayer(close_parks, {'color': '008000'}, "Parks") 23 | -------------------------------------------------------------------------------- /FeatureCollection/select_by_attributes.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_by_attributes.py 2 | 3 | #!/usr/bin/env python 4 | """Select by attributes 5 | 6 | """ 7 | 8 | import ee 9 | from ee_plugin import Map 10 | 11 | # Select North Dakota and South Dakota 12 | fc = ee.FeatureCollection('TIGER/2018/States') \ 13 | .filter(ee.Filter.Or( 14 | ee.Filter.eq('STUSPS', 'ND'), 15 | ee.Filter.eq('STUSPS', 'SD'), 16 | )) 17 | 18 | image = ee.Image().paint(fc, 0, 2) 19 | # Map.setCenter(-99.844, 37.649, 5) 20 | Map.centerObject(fc, 6) 21 | Map.addLayer(image, {'palette': 'FF0000'}, 'TIGER/2018/States') 22 | 23 | 24 | -------------------------------------------------------------------------------- /FeatureCollection/select_by_location.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_by_location.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | HUC10 = ee.FeatureCollection("USGS/WBD/2017/HUC10") 7 | HUC08 = ee.FeatureCollection('USGS/WBD/2017/HUC08') 8 | roi = HUC08.filter(ee.Filter.eq('name', 'Pipestem')) 9 | 10 | Map.centerObject(roi, 10) 11 | Map.addLayer(ee.Image().paint(roi, 0, 3), {}, 'HUC08') 12 | 13 | # select polygons intersecting the roi 14 | roi2 = HUC10.filter(ee.Filter.contains(**{'leftValue': roi.geometry(), 'rightField': '.geo'})) 15 | Map.addLayer(ee.Image().paint(roi2, 0, 2), {'palette': 'blue'}, 'HUC10') 16 | 17 | # roi3 = HUC10.filter(ee.Filter.stringContains(**{'leftField': 'huc10', 'rightValue': '10160002'})) 18 | # # print(roi3) 19 | # Map.addLayer(roi3) 20 | -------------------------------------------------------------------------------- /FeatureCollection/select_by_strings.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_by_strings.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | #!/usr/bin/env python 7 | """Select by strings 8 | 9 | """ 10 | 11 | # Select states with "A" in its name 12 | fc = ee.FeatureCollection('TIGER/2018/States') \ 13 | .filter(ee.Filter.stringContains('STUSPS', 'A')) 14 | 15 | image = ee.Image().paint(fc, 0, 2) 16 | Map.centerObject(fc, 6) 17 | Map.addLayer(image, {'palette': 'FF0000'}, '*A*') 18 | 19 | 20 | # Select states its name starting with 'A' 21 | fc = ee.FeatureCollection('TIGER/2018/States') \ 22 | .filter(ee.Filter.stringStartsWith('STUSPS', 'A')) 23 | 24 | image = ee.Image().paint(fc, 0, 2) 25 | Map.addLayer(image, {'palette': '0000FF'}, 'A*') 26 | -------------------------------------------------------------------------------- /FeatureCollection/select_columns.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_columns.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | fc = ee.FeatureCollection('TIGER/2018/States') 7 | 8 | print(fc.first().getInfo()) 9 | 10 | new_fc = fc.select(['STUSPS', 'NAME', 'ALAND'], ['abbr', 'name', 'area']) 11 | print(new_fc.first().getInfo()) 12 | 13 | propertyNames = new_fc.first().propertyNames() 14 | print(propertyNames.getInfo()) -------------------------------------------------------------------------------- /FeatureCollection/set_properties.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Make a feature and set some properties. 5 | feature = ee.Feature(ee.Geometry.Point([-122.22599, 37.17605])) \ 6 | .set('genus', 'Sequoia').set('species', 'sempervirens') 7 | 8 | # Get a property from the feature. 9 | species = feature.get('species') 10 | print(species.getInfo()) 11 | 12 | # Set a new property. 13 | feature = feature.set('presence', 1) 14 | 15 | # Overwrite the old properties with a new dictionary. 16 | newDict = {'genus': 'Brachyramphus', 'species': 'marmoratus'} 17 | feature = feature.set(newDict) 18 | 19 | # Check the result. 20 | print(feature.getInfo()) 21 | 22 | -------------------------------------------------------------------------------- /FeatureCollection/simplify_polygons.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/simplify_polygons.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | waterSurface = ee.Image('JRC/GSW1_0/GlobalSurfaceWater') 7 | waterChange = waterSurface.select('transition') 8 | # Select Permanent Water Only: 9 | Permanent_Water = 1 # value 1 represents pixels of permenant water, no change 10 | waterMask = waterChange.eq(Permanent_Water) # Water mask boolean = 1 to detect whater bodies 11 | # Map.setCenter(24.43874, 61.58173, 10) 12 | # Map.addLayer(waterMask, {}, 'Water Mask') 13 | # Map.centerObject(masked) 14 | OnlyLakes = waterMask.updateMask(waterMask) 15 | 16 | roi = ee.Geometry.Polygon( 17 | [[[22.049560546875, 61.171214253920965], 18 | [22.0330810546875, 60.833021871926185], 19 | [22.57415771484375, 60.83168327936567], 20 | [22.5714111328125, 61.171214253920965]]]) 21 | 22 | classes = OnlyLakes.reduceToVectors(**{ 23 | 'reducer': ee.Reducer.countEvery(), 24 | 'geometry': roi, 25 | 'scale': 30, 26 | 'maxPixels': 1e10 27 | }) 28 | simpleClasses = classes.geometry().simplify(50) 29 | 30 | Map.centerObject(ee.FeatureCollection(roi), 10) 31 | Map.addLayer(ee.Image().paint(classes, 0, 2),{'palette': 'red'}, "original") 32 | Map.addLayer(ee.Image().paint(simpleClasses, 0, 2),{'palette': 'blue'}, "simplified") 33 | -------------------------------------------------------------------------------- /FeatureCollection/vector_symbology.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/vector_symbology.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | fc = ee.FeatureCollection('TIGER/2018/States') 7 | # .filter(ee.Filter.eq('STUSPS', 'MN')) 8 | 9 | image = ee.Image().paint(**{ 10 | 'featureCollection': fc, 11 | 'color': 1, 12 | 'width': 3 13 | }) 14 | 15 | Map.setCenter(-99.844, 37.649, 5) 16 | Map.addLayer(image, {'palette': 'FF0000'}, 'TIGER/2018/States') 17 | # Map.addLayer(fc, {}, 'US States') 18 | -------------------------------------------------------------------------------- /Filter/filter_eq.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Filter/filter_eq.py 2 | # Filter to metadata equal to the given value. 3 | 4 | import ee 5 | from ee_plugin import Map 6 | 7 | states = ee.FeatureCollection('TIGER/2018/States') 8 | 9 | selected = states.filter(ee.Filter.eq("NAME", 'California')) 10 | 11 | Map.centerObject(selected, 6) 12 | Map.addLayer(ee.Image().paint(selected, 0, 2), {'palette': 'yellow'}, 'Selected') -------------------------------------------------------------------------------- /Filter/filter_in_list.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Filter/filter_in_list.py 2 | # Filter on metadata contained in a list 3 | 4 | import ee 5 | from ee_plugin import Map 6 | 7 | states = ee.FeatureCollection('TIGER/2018/States') 8 | 9 | selected = states.filter(ee.Filter.inList("NAME", ['California', 'Nevada', 'Utah', 'Arizona'])) 10 | 11 | Map.centerObject(selected, 6) 12 | Map.addLayer(ee.Image().paint(selected, 0, 2), {'palette': 'yellow'}, 'Selected') -------------------------------------------------------------------------------- /Filter/filter_neq.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Filter/filter_neq.py 2 | # Filter to metadata equal to the given value. 3 | 4 | import ee 5 | from ee_plugin import Map 6 | 7 | states = ee.FeatureCollection('TIGER/2018/States') 8 | 9 | # Select all states except California 10 | selected = states.filter(ee.Filter.neq("NAME", 'California')) 11 | 12 | Map.centerObject(selected, 6) 13 | Map.addLayer(ee.Image().paint(selected, 0, 2), {'palette': 'yellow'}, 'Selected') -------------------------------------------------------------------------------- /Filter/filter_range_contains.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Filter/filter_range_contains.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | states = ee.FeatureCollection('TIGER/2018/States') 7 | # print(states.first().getInfo()) 8 | 9 | # Select states with land area between 200,000 km2 and 300,000 km2 10 | selected = states.filter(ee.Filter.rangeContains("ALAND", 200000000000, 300000000000)) 11 | Map.centerObject(selected, 6) 12 | Map.addLayer(ee.Image().paint(selected, 0, 2), {'palette': 'yellow'}, 'Selected') -------------------------------------------------------------------------------- /Filter/filter_string_contains.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Filter/filter_string_contains.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | states = ee.FeatureCollection('TIGER/2018/States') 7 | 8 | selected = states.filter(ee.Filter.stringContains("NAME", 'Dakota')) 9 | 10 | Map.centerObject(selected, 6) 11 | Map.addLayer(ee.Image().paint(selected, 0, 2), {'palette': 'yellow'}, 'Selected') -------------------------------------------------------------------------------- /Filter/filter_string_ends_with.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Filter/filter_string_ends_with.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | states = ee.FeatureCollection('TIGER/2018/States') 7 | 8 | # Select states with its name ending with 'ia' 9 | selected = states.filter(ee.Filter.stringEndsWith('NAME', 'ia')) 10 | 11 | 12 | Map.centerObject(selected, 6) 13 | Map.addLayer(ee.Image().paint(selected, 0, 2), {'palette': 'yellow'}, 'Selected') 14 | -------------------------------------------------------------------------------- /Filter/filter_string_starts_with.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Filter/filter_string_starts_with.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | 7 | states = ee.FeatureCollection('TIGER/2018/States') 8 | 9 | # Select states its name starting with 'Al' 10 | selected = states.filter(ee.Filter.stringStartsWith('NAME', 'Al')) 11 | 12 | 13 | Map.centerObject(selected, 6) 14 | Map.addLayer(ee.Image().paint(selected, 0, 2), {'palette': 'yellow'}, 'Selected') 15 | -------------------------------------------------------------------------------- /Folium/README.md: -------------------------------------------------------------------------------- 1 | # Earth Engine Python API and Folium Interactive Mapping 2 | 3 | Jupyter notebook examples for using the [Google EarthEngine Python API](https://developers.google.com/earth-engine/python_install) and [folium](https://github.com/python-visualization/folium) library for interactive mapping. 4 | 5 | ## Examples 6 | 7 | * [Run in Google Colab](https://colab.research.google.com/github/giswqs/qgis-earthengine-examples/blob/master/Folium/ee-api-folium-setup.ipynb) 8 | * [View source on GitHub](https://github.com/giswqs/qgis-earthengine-examples/blob/master/Folium/ee-api-folium-setup.ipynb) 9 | * [Run in binder](https://mybinder.org/v2/gh/giswqs/qgis-earthengine-examples/master?filepath=Folium%2Fee-api-folium-setup.ipynb) 10 | * [Notebook Viewer](https://nbviewer.jupyter.org/github/giswqs/qgis-earthengine-examples/blob/master/Folium/ee-api-folium-setup.ipynb) 11 | 12 | ## Demo 13 | 14 | ![GEE folium demo](https://i.imgur.com/bTzz5JL.gif) -------------------------------------------------------------------------------- /Gena/basic_image.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | image = ee.Image.pixelLonLat() \ 5 | .add([180, 90]).divide([360, 180]) 6 | 7 | # image = image.multiply(50).sin() 8 | 9 | Map.setCenter(0, 28, 2.5) 10 | Map.addLayer(image, {}, 'coords', True) 11 | -------------------------------------------------------------------------------- /Gena/contrib/palettes-test-crameri-dem.py: -------------------------------------------------------------------------------- 1 | import ee 2 | 3 | from ee_plugin import Map 4 | from ee_plugin.contrib import utils, palettes 5 | 6 | Map.setCenter(4.408241, 52.177595, 18) 7 | 8 | dem = ee.Image("AHN/AHN2_05M_RUW") \ 9 | .resample('bicubic') \ 10 | .convolve(ee.Kernel.gaussian(0.5, 0.25, 'meters')) 11 | 12 | # palette = palettes.crameri['lisbon'][50] 13 | palette = palettes.crameri['oleron'][50] 14 | # palette = palettes.crameri['roma'][50].slice(0).reverse() 15 | 16 | demRGB = dem.visualize(**{ 'min': -5, 'max': 15, 'palette': palette }) 17 | 18 | weight = 0.4 # wegith of Hillshade vs RGB intensity (0 - flat, 1 - HS) 19 | exaggeration = 5 # vertical exaggeration 20 | azimuth = 315 # Sun azimuth 21 | zenith = 20 # Sun elevation 22 | brightness = -0.05 # 0 - default 23 | contrast = 0.05 # 0 - default 24 | saturation = 0.8 # 1 - default 25 | castShadows = False 26 | 27 | # no shadows 28 | rgb = utils.hillshadeRGB(demRGB, dem, weight, exaggeration, azimuth, zenith, contrast, brightness, saturation, castShadows) 29 | Map.addLayer(rgb, {}, 'DEM (no shadows)', False) 30 | 31 | # with shadows 32 | castShadows = True 33 | rgb = utils.hillshadeRGB(demRGB, dem, weight, exaggeration, azimuth, zenith, contrast, brightness, saturation, castShadows) 34 | Map.addLayer(rgb, {}, 'DEM') 35 | 36 | Map.addLayer(dem, {}, 'DEM (raw)', False) 37 | -------------------------------------------------------------------------------- /Gena/contrib/utils-hillshadeRgb.py: -------------------------------------------------------------------------------- 1 | import ee 2 | 3 | from ee_plugin import Map 4 | from ee_plugin.contrib import utils, palettes 5 | 6 | dem = ee.Image("AHN/AHN2_05M_RUW") \ 7 | .resample('bicubic') \ 8 | .focal_max(0.5, 'circle', 'meters') \ 9 | .convolve(ee.Kernel.gaussian(0.5, 0.25, 'meters')) 10 | 11 | # See https://github.com/gee-community/ee-palettes 12 | # for the full list of supported color palettes 13 | # palette = palettes.crameri['lisbon'][50] 14 | palette = palettes.crameri['oleron'][50] 15 | # palette = palettes.crameri['roma'][50][::-1] # reversed 16 | # palette = palettes.crameri['batlow'][50] 17 | 18 | demRGB = dem.visualize(**{ 'min': -5, 'max': 5, 'palette': palette }) 19 | Map.addLayer(demRGB , {}, 'DEM (RGB)', False) 20 | 21 | weight = 0.5 # hillshade vs RGB intensity (0 - flat, 1 - HS) 22 | exaggeration = 3 # vertical exaggeration 23 | azimuth = 300 # Sun azimuth 24 | zenith = 25 # Sun elevation 25 | brightness = -0.05 # 0 - default 26 | contrast = 0.05 # 0 - default 27 | saturation = 0.8 # 1 - default 28 | castShadows = False 29 | 30 | # no shadows 31 | rgb = utils.hillshadeRGB(demRGB, dem, weight, exaggeration, 32 | azimuth, zenith, contrast, brightness, saturation, castShadows) 33 | Map.addLayer(rgb, {}, 'DEM (hillshade)', False) 34 | 35 | # with shadows 36 | castShadows = True 37 | rgb = utils.hillshadeRGB(demRGB, dem, weight, exaggeration, 38 | azimuth, zenith, contrast, brightness, saturation, castShadows) 39 | Map.addLayer(rgb, {}, 'DEM (hillshade, shadows)') 40 | 41 | 42 | # Map.setCenter(4.407, 52.177, 18) 43 | -------------------------------------------------------------------------------- /Gena/hillshade.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | from ee_plugin.contrib import palettes 4 | 5 | dem = ee.Image("JAXA/ALOS/AW3D30_V1_1").select('MED') 6 | dem = dem.updateMask(dem.gt(0)) 7 | palette = palettes.cb['Pastel1'][7] 8 | #palette = ['black', 'white'] 9 | rgb = dem.visualize(**{'min': 0, 'max': 5000, 'palette': palette }) 10 | hsv = rgb.unitScale(0, 255).rgbToHsv() 11 | 12 | extrusion = 30 13 | weight = 0.7 14 | 15 | hs = ee.Terrain.hillshade(dem.multiply(extrusion), 315, 35).unitScale(10, 250).resample('bicubic') 16 | 17 | hs = hs.multiply(weight).add(hsv.select('value').multiply(1 - weight)) 18 | hsv = hsv.addBands(hs.rename('value'), ['value'], True) 19 | rgb = hsv.hsvToRgb() 20 | 21 | Map.setCenter(0, 28, 2.5) 22 | Map.addLayer(rgb, {}, 'ALOS DEM', True) 23 | -------------------------------------------------------------------------------- /Gena/hillshade_and_water.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | from ee_plugin.contrib import palettes 4 | 5 | dem = ee.Image("JAXA/ALOS/AW3D30_V1_1").select('MED') 6 | dem = dem.updateMask(dem.gt(0)) 7 | palette = palettes.cb['Pastel1'][7] 8 | #palette = ['black', 'white'] 9 | rgb = dem.visualize(**{'min': 0, 'max': 5000, 'palette': palette }) 10 | hsv = rgb.unitScale(0, 255).rgbToHsv() 11 | 12 | extrusion = 30 13 | weight = 0.7 14 | 15 | hs = ee.Terrain.hillshade(dem.multiply(extrusion), 315, 35).unitScale(10, 250).resample('bicubic') 16 | 17 | hs = hs.multiply(weight).add(hsv.select('value').multiply(1 - weight)) 18 | hsv = hsv.addBands(hs.rename('value'), ['value'], True) 19 | rgb = hsv.hsvToRgb() 20 | 21 | Map.addLayer(rgb, {}, 'ALOS DEM', True, 0.5) 22 | 23 | water_occurrence = ( ee.Image("JRC/GSW1_0/GlobalSurfaceWater") 24 | .select('occurrence') 25 | .divide(100) 26 | .unmask(0) 27 | .resample('bicubic') ) 28 | 29 | palette = ["ffffcc","ffeda0","fed976","feb24c","fd8d3c","fc4e2a","e31a1c","bd0026","800026"][::-1][1:] 30 | 31 | land = ee.Image("users/gena/land_polygons_image").mask() 32 | 33 | Map.addLayer(water_occurrence.mask(water_occurrence.multiply(2).multiply(land)), {'min': 0, 'max': 1, 'palette': palette}, 'water occurrence', True) 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Gena/landsat_median.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')\ 5 | .filter(ee.Filter.eq('WRS_PATH', 44))\ 6 | .filter(ee.Filter.eq('WRS_ROW', 34))\ 7 | .filterDate('2014-01-01', '2015-01-01') 8 | 9 | median = collection.median() 10 | 11 | Map.setCenter(-122.3578, 37.7726, 12) 12 | Map.addLayer(median, {"bands": ['B4', 'B3', 'B2'], "max": 0.3}, 'median') 13 | -------------------------------------------------------------------------------- /Gena/map_add_features.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # get a single feature 5 | countries = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017") 6 | country = countries.filter(ee.Filter.eq('country_na', 'Ukraine')) 7 | 8 | # TEST: add feature to the Map 9 | Map.addLayer(country, { 'color': 'orange' }, 'feature') 10 | 11 | # set Map center using coordinates and zoom 12 | Map.setCenter(31.472, 49.044, 6) 13 | -------------------------------------------------------------------------------- /Gena/map_center_object.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # get a single feature 5 | countries = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017") 6 | country = countries.filter(ee.Filter.eq('country_na', 'Ukraine')) 7 | Map.addLayer(country, { 'color': 'orange' }, 'feature collection layer') 8 | 9 | # TEST: center feature on a map 10 | Map.centerObject(country, 6) 11 | -------------------------------------------------------------------------------- /Gena/map_center_object_no_zoom.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # get a single feature 5 | countries = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017") 6 | country = countries.filter(ee.Filter.eq('country_na', 'Ukraine')) 7 | Map.addLayer(country, { 'color': 'orange' }, 'feature collection layer') 8 | 9 | # TEST: center feature on a map 10 | Map.centerObject(country) 11 | -------------------------------------------------------------------------------- /Gena/map_get_bounds.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # add some data to the Map 5 | dem = ee.Image("AHN/AHN2_05M_RUW") 6 | Map.addLayer(dem, {'min': -5, 'max': 50, 'palette': ['000000', 'ffffff'] }, 'DEM', True) 7 | 8 | # zoom in somewhere 9 | Map.setCenter(4.4585, 52.0774, 15) 10 | 11 | # TEST 12 | bounds = Map.getBounds(True) 13 | 14 | # add bounds to the map 15 | Map.addLayer(bounds, { 'color': 'green' }, 'bounds', True, 0.5) 16 | -------------------------------------------------------------------------------- /Gena/map_get_center.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # add some data to the Map 5 | dem = ee.Image("AHN/AHN2_05M_RUW") 6 | Map.addLayer(dem, {'min': -5, 'max': 50, 'palette': ['000000', 'ffffff'] }, 'DEM', True) 7 | 8 | # zoom in somewhere 9 | Map.setCenter(4.4585, 52.0774, 14) 10 | 11 | # TEST 12 | center= Map.getCenter() 13 | 14 | # add bounds to the map 15 | Map.addLayer(center, { 'color': 'red' }, 'center') 16 | -------------------------------------------------------------------------------- /Gena/map_set_center.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Add some data to the Map 5 | dem = ee.Image("JAXA/ALOS/AW3D30_V1_1").select('MED') 6 | Map.addLayer(dem, {'min': 0, 'max': 5000, 'palette': ['000000', 'ffffff'] }, 'DEM', True) 7 | 8 | # TEST Map.setCenter 9 | Map.setCenter(0, 28, 2.5) 10 | -------------------------------------------------------------------------------- /Gena/map_set_zoom.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # add some data to the Map 5 | dem = ee.Image("AHN/AHN2_05M_RUW") 6 | Map.addLayer(dem, {'min': -5, 'max': 50, 'palette': ['000000', 'ffffff'] }, 'DEM', True) 7 | 8 | # zoom in somewhere 9 | Map.setCenter(4.4585, 52.0774, 15) 10 | 11 | # TEST 12 | Map.setZoom(10) 13 | -------------------------------------------------------------------------------- /Gena/palettes_crameri_oleron_dem.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | from ee_plugin.contrib import palettes 4 | 5 | dem = ee.Image("AHN/AHN2_05M_RUW").convolve(ee.Kernel.gaussian(0.5, 0.3, 'meters')) 6 | 7 | extrusion = 3 8 | weight = 0.7 9 | palette = palettes.crameri['oleron'][50] 10 | 11 | rgb = dem.visualize(**{'min': 0, 'max': 3, 'palette': palette }) 12 | hsv = rgb.unitScale(0, 255).rgbToHsv() 13 | hs = ee.Terrain.hillshade(dem.multiply(extrusion), 315, 35).unitScale(0, 255) 14 | hs = hs.multiply(weight).add(hsv.select('value').multiply(1 - weight)) 15 | saturation = hsv.select('saturation').multiply(0.5) 16 | hsv = hsv.addBands(hs.rename('value'), ['value'], True) 17 | hsv = hsv.addBands(saturation, ['saturation'], True) 18 | rgb = hsv.hsvToRgb() 19 | 20 | # rgb = rgb.updateMask(dem.unitScale(0, 3)) 21 | 22 | Map.addLayer(rgb, {}, 'Dutch AHN DEM', True) 23 | Map.setCenter(4.5618, 52.1664, 18) 24 | -------------------------------------------------------------------------------- /Gena/test_sentinel2.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | image = ee.ImageCollection('COPERNICUS/S2') \ 5 | .filterDate('2017-01-01', '2017-01-02').median() \ 6 | .divide(10000).visualize(**{'bands': ['B12', 'B8', 'B4'], 'min': 0.05, 'max': 0.5}) 7 | 8 | Map.setCenter(35.2, 31, 13) 9 | Map.addLayer(image, {}, 'Sentinel-2 images January, 2018') 10 | -------------------------------------------------------------------------------- /GetStarted/01_hello_world.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # traditional python string 5 | print('Hello world!') 6 | 7 | # Earth Eninge object 8 | print(ee.String('Hello World from Earth Engine!').getInfo()) 9 | print(ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318').getInfo()) 10 | -------------------------------------------------------------------------------- /GetStarted/02_adding_data_to_qgis.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load an image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318') 6 | 7 | # Center the map on the image. 8 | Map.centerObject(image, 9) 9 | 10 | # Display the image. 11 | Map.addLayer(image, {}, 'Landsat 8 original image') 12 | 13 | # Define visualization parameters in an object literal. 14 | vizParams = {'bands': ['B5', 'B4', 'B3'], 15 | 'min': 5000, 'max': 15000, 'gamma': 1.3} 16 | 17 | # Center the map on the image and display. 18 | Map.centerObject(image, 9) 19 | Map.addLayer(image, vizParams, 'Landsat 8 False color') 20 | 21 | # Use Map.addLayer() to add features and feature collections to the map. For example, 22 | counties = ee.FeatureCollection('TIGER/2016/Counties') 23 | Map.addLayer(ee.Image().paint(counties, 0, 2), {}, 'counties') 24 | -------------------------------------------------------------------------------- /GetStarted/03_finding_images.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1') 5 | 6 | point = ee.Geometry.Point(-122.262, 37.8719) 7 | start = ee.Date('2014-06-01') 8 | finish = ee.Date('2014-10-01') 9 | 10 | filteredCollection = ee.ImageCollection('LANDSAT/LC08/C01/T1') \ 11 | .filterBounds(point) \ 12 | .filterDate(start, finish) \ 13 | .sort('CLOUD_COVER', True) 14 | 15 | first = filteredCollection.first() 16 | # Define visualization parameters in an object literal. 17 | vizParams = {'bands': ['B5', 'B4', 'B3'], 18 | 'min': 5000, 'max': 15000, 'gamma': 1.3} 19 | Map.addLayer(first, vizParams, 'Landsat 8 image') 20 | 21 | # Load a feature collection. 22 | featureCollection = ee.FeatureCollection('TIGER/2016/States') 23 | 24 | # Filter the collection. 25 | filteredFC = featureCollection.filter(ee.Filter.eq('NAME', 'California')) 26 | 27 | # Display the collection. 28 | Map.addLayer(ee.Image().paint(filteredFC, 0, 2), 29 | {'palette': 'red'}, 'California') 30 | -------------------------------------------------------------------------------- /GetStarted/04_band_math.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This function gets NDVI from Landsat 5 imagery. 5 | 6 | 7 | def getNDVI(image): 8 | return image.normalizedDifference(['B4', 'B3']) 9 | 10 | 11 | # Load two Landsat 5 images, 20 years apart. 12 | image1 = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_19900604') 13 | image2 = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_20100611') 14 | 15 | # Compute NDVI from the scenes. 16 | ndvi1 = getNDVI(image1) 17 | ndvi2 = getNDVI(image2) 18 | 19 | # Compute the difference in NDVI. 20 | ndviDifference = ndvi2.subtract(ndvi1) 21 | 22 | ndviParams = {'palette': ['#d73027', '#f46d43', '#fdae61', 23 | '#fee08b', '#d9ef8b', '#a6d96a', '#66bd63', '#1a9850']} 24 | ndwiParams = {'min': -0.5, 'max': 0.5, 'palette': ['FF0000', 'FFFFFF', '0000FF']} 25 | 26 | 27 | Map.centerObject(image1, 10) 28 | Map.addLayer(ndvi1, ndviParams, 'NDVI 1') 29 | Map.addLayer(ndvi2, ndviParams, 'NDVI 2') 30 | Map.addLayer(ndviDifference, ndwiParams, 'NDVI difference') 31 | -------------------------------------------------------------------------------- /GetStarted/05_map_function.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This function gets NDVI from Landsat 8 imagery. 5 | 6 | 7 | def addNDVI(image): 8 | return image.addBands(image.normalizedDifference(['B5', 'B4'])) 9 | 10 | 11 | # Load the Landsat 8 raw data, filter by location and date. 12 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1') \ 13 | .filterBounds(ee.Geometry.Point(-122.262, 37.8719)) \ 14 | .filterDate('2014-06-01', '2014-10-01') 15 | 16 | # Map the function over the collection. 17 | ndviCollection = collection.map(addNDVI) 18 | 19 | first = ndviCollection.first() 20 | print(first.getInfo()) 21 | 22 | bandNames = first.bandNames() 23 | print(bandNames.getInfo()) 24 | -------------------------------------------------------------------------------- /GetStarted/06_reducing.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 collection. 5 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1') \ 6 | .filterBounds(ee.Geometry.Point(-122.262, 37.8719)) \ 7 | .filterDate('2014-01-01', '2014-12-31') \ 8 | .sort('CLOUD_COVER') 9 | 10 | # Compute the median of each pixel for each band of the 5 least cloudy scenes. 11 | median = collection.limit(5).reduce(ee.Reducer.median()) 12 | 13 | # Define visualization parameters in an object literal. 14 | vizParams = {'bands': ['B5_median', 'B4_median', 'B3_median'], 15 | 'min': 5000, 'max': 15000, 'gamma': 1.3} 16 | 17 | Map.setCenter(-122.262, 37.8719, 10) 18 | Map.addLayer(median, vizParams, 'Median image') -------------------------------------------------------------------------------- /GetStarted/07_image_statistics.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load and display a Landsat TOA image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') 6 | Map.addLayer(image, {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}, 'Landsat 8') 7 | 8 | # Create an arbitrary rectangle as a region and display it. 9 | region = ee.Geometry.Rectangle(-122.2806, 37.1209, -122.0554, 37.2413) 10 | Map.centerObject(ee.FeatureCollection(region), 13) 11 | Map.addLayer(ee.Image().paint(region, 0, 2), {}, 'Region') 12 | 13 | # Get a dictionary of means in the region. Keys are bandnames. 14 | mean = image.reduceRegion(**{ 15 | 'reducer': ee.Reducer.mean(), 16 | 'geometry': region, 17 | 'scale': 30 18 | }) 19 | 20 | print(mean.getInfo()) -------------------------------------------------------------------------------- /GetStarted/08_masking.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This function gets NDVI from Landsat 5 imagery. 5 | 6 | 7 | def getNDVI(image): 8 | return image.normalizedDifference(['B4', 'B3']) 9 | 10 | 11 | # Load two Landsat 5 images, 20 years apart. 12 | image1 = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_19900604') 13 | image2 = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_20100611') 14 | 15 | # Compute NDVI from the scenes. 16 | ndvi1 = getNDVI(image1) 17 | ndvi2 = getNDVI(image2) 18 | 19 | # Compute the difference in NDVI. 20 | ndviDifference = ndvi2.subtract(ndvi1) 21 | # Load the land mask from the SRTM DEM. 22 | landMask = ee.Image('CGIAR/SRTM90_V4').mask() 23 | 24 | # Update the NDVI difference mask with the land mask. 25 | maskedDifference = ndviDifference.updateMask(landMask) 26 | 27 | # Display the masked result. 28 | vizParams = {'min': -0.5, 'max': 0.5, 29 | 'palette': ['FF0000', 'FFFFFF', '0000FF']} 30 | Map.setCenter(-122.2531, 37.6295, 9) 31 | Map.addLayer(maskedDifference, vizParams, 'NDVI difference') 32 | -------------------------------------------------------------------------------- /HowEarthEngineWorks/ClientVsServer.py: -------------------------------------------------------------------------------- 1 | import ee 2 | 3 | serverString = ee.String('I am not a String!') 4 | print(type(serverString)) 5 | 6 | clientString = 'I am a String' 7 | print(type(clientString)) 8 | 9 | serverList = ee.List.sequence(0, 7) 10 | print(serverList.getInfo()) 11 | serverList = serverList.map(lambda n: ee.Number(n).add(1)) 12 | print(serverList.getInfo()) 13 | -------------------------------------------------------------------------------- /HowEarthEngineWorks/DeferredExecution.py: -------------------------------------------------------------------------------- 1 | import ee 2 | 3 | image = ee.Image('CGIAR/SRTM90_V4') 4 | operation = image.add(10) 5 | # print(operation.getInfo()) 6 | print(operation) 7 | -------------------------------------------------------------------------------- /HowEarthEngineWorks/Projections.py: -------------------------------------------------------------------------------- 1 | import ee 2 | 3 | 4 | image = ee.Image('LANDSAT/LC8_L1T/LC80440342014077LGN00').select(0) 5 | print('Projection, crs, and crs_transform:', image.projection().getInfo()) 6 | print('Scale in meters:', image.projection().nominalScale().getInfo()) 7 | -------------------------------------------------------------------------------- /Image/band_math.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load two 5-year Landsat 7 composites. 5 | landsat1999 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003') 6 | landsat2008 = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012') 7 | 8 | # Compute NDVI the hard way. 9 | ndvi1999 = landsat1999.select('B4').subtract(landsat1999.select('B3')) \ 10 | .divide(landsat1999.select('B4').add(landsat1999.select('B3'))) 11 | 12 | # Compute NDVI the easy way. 13 | ndvi2008 = landsat2008.normalizedDifference(['B4', 'B3']) 14 | 15 | # Compute the multi-band difference image. 16 | diff = landsat2008.subtract(landsat1999) 17 | Map.addLayer(diff, 18 | {'bands': ['B4', 'B3', 'B2'], 'min': -32, 'max': 32}, 19 | 'difference') 20 | 21 | # Compute the squared difference in each band. 22 | squaredDifference = diff.pow(2) 23 | Map.addLayer(squaredDifference, 24 | {'bands': ['B4', 'B3', 'B2'], 'max': 1000}, 25 | 'squared diff.') 26 | 27 | -------------------------------------------------------------------------------- /Image/canny_edge_detector.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Canny Edge Detector example. 5 | 6 | # Load an image and compute NDVI from it. 7 | image = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_031034_20110619') 8 | ndvi = image.normalizedDifference(['B4','B3']) 9 | 10 | # Detect edges in the composite. 11 | canny = ee.Algorithms.CannyEdgeDetector(ndvi, 0.7) 12 | 13 | # Mask the image with itself to get rid of areas with no edges. 14 | canny = canny.updateMask(canny) 15 | 16 | Map.setCenter(-101.05259, 37.93418, 13) 17 | Map.addLayer(ndvi, {'min': 0, 'max': 1}, 'Landsat NDVI') 18 | Map.addLayer(canny, {'min': 0, 'max': 1, 'palette': 'FF0000'}, 'Canny Edges') 19 | -------------------------------------------------------------------------------- /Image/cell_statistics.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/cell_statistics.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | # Load an image and select some bands of interest. 7 | image = ee.Image('LANDSAT/LC8_L1T/LC80440342014077LGN00') \ 8 | .select(['B4', 'B3', 'B2']) 9 | 10 | # Reduce the image to get a one-band maximum value image. 11 | maxValue = image.reduce(ee.Reducer.max()) 12 | 13 | # Display the result. 14 | Map.centerObject(image, 10) 15 | Map.addLayer(maxValue, {'max': 13000}, 'Maximum value image') 16 | -------------------------------------------------------------------------------- /Image/clamp.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # ee.Image.clamp() example. 5 | 6 | # Clamp the values of all bands in an image to lie within the specified range. 7 | # Values below the low value of that range are set to low value, values above 8 | # the high value of that range are set to the high value. 9 | 10 | image = ee.Image('CGIAR/SRTM90_V4') 11 | clamped = image.clamp(1000, 2000) 12 | 13 | Map.setCenter(-121.753, 46.855, 9) 14 | Map.addLayer(image, {'min': 0, 'max': 4300}, 'Full stretch') 15 | Map.addLayer(clamped, {'min': 0, 'max': 4300}, 'Clamped') 16 | -------------------------------------------------------------------------------- /Image/clipping.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | image = ee.Image('LANDSAT/LC8_L1T_TOA/LC80440342014077LGN00') 5 | 6 | roi = ee.Geometry.Point([-122.4481, 37.7599]).buffer(20000) 7 | 8 | clipped = image.clip(roi) 9 | 10 | # print(image.getInfo()) 11 | Map.setCenter(-122.1899, 37.5010, 10) 12 | vis = {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5, 'gamma': [0.95, 1.1, 1]} 13 | Map.addLayer(image, vis, "Full Image", False) 14 | Map.addLayer(clipped, vis, "Clipped Image") 15 | -------------------------------------------------------------------------------- /Image/composite_bands.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/composite_bands.py 2 | 3 | #!/usr/bin/env python 4 | """Composite bands example.""" 5 | 6 | import ee 7 | from ee_plugin import Map 8 | 9 | # There are many fine places to look here is one. Comment 10 | # this out if you want to twiddle knobs while panning around. 11 | Map.setCenter(-61.61625, -11.64273, 14) 12 | 13 | # Grab a sample L7 image and pull out the RGB and pan bands 14 | # in the range (0, 1). (The range of the pan band values was 15 | # chosen to roughly match the other bands.) 16 | image1 = ee.Image('LANDSAT/LE7/LE72300681999227EDC00') 17 | 18 | rgb = image1.select('B3', 'B2', 'B1').unitScale(0, 255) 19 | gray = image1.select('B8').unitScale(0, 155) 20 | 21 | # Convert to HSV, swap in the pan band, and convert back to RGB. 22 | huesat = rgb.rgbToHsv().select('hue', 'saturation') 23 | upres = ee.Image.cat(huesat, gray).hsvToRgb() 24 | 25 | # Display before and after layers using the same vis parameters. 26 | visparams = {'min': [.15, .15, .25], 'max': [1, .9, .9], 'gamma': 1.6} 27 | Map.addLayer(rgb, visparams, 'Orignal') 28 | Map.addLayer(upres, visparams, 'Pansharpened') 29 | -------------------------------------------------------------------------------- /Image/conditional_operations.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') 6 | 7 | # Create NDVI and NDWI spectral indices. 8 | ndvi = image.normalizedDifference(['B5', 'B4']) 9 | ndwi = image.normalizedDifference(['B3', 'B5']) 10 | 11 | # Create a binary layer using logical operations. 12 | bare = ndvi.lt(0.2).And(ndwi.lt(0)) 13 | 14 | # Mask and display the binary layer. 15 | Map.setCenter(-122.3578, 37.7726, 12) 16 | Map.setOptions('satellite') 17 | Map.addLayer(bare.updateMask(bare), {}, 'bare') 18 | 19 | -------------------------------------------------------------------------------- /Image/connected_pixel_count.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Image.ConnectedPixelCount example. 5 | 6 | # Split pixels of band 01 into "bright" (arbitrarily defined as 7 | # reflectance > 0.3) and "dim". Highlight small (<30 pixels) 8 | # standalone islands of "bright" or "dim" type. 9 | img = ee.Image('MODIS/006/MOD09GA/2012_03_09') \ 10 | .select('sur_refl_b01') \ 11 | .multiply(0.0001) 12 | 13 | # Create a threshold image. 14 | bright = img.gt(0.3) 15 | 16 | # Compute connected pixel counts stop searching for connected pixels 17 | # once the size of the connected neightborhood reaches 30 pixels, and 18 | # use 8-connected rules. 19 | conn = bright.connectedPixelCount(**{ 20 | 'maxSize': 30, 21 | 'eightConnected': True 22 | }) 23 | 24 | # Make a binary image of small clusters. 25 | smallClusters = conn.lt(30) 26 | 27 | Map.setCenter(-107.24304, 35.78663, 8) 28 | Map.addLayer(img, {'min': 0, 'max': 1}, 'original') 29 | Map.addLayer(smallClusters.updateMask(smallClusters), 30 | {'min': 0, 'max': 1, 'palette': 'FF0000'}, 'cc') 31 | -------------------------------------------------------------------------------- /Image/convert_bands_to_image_collection.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/convert_bands_to_image_collection.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318') 7 | print("Number of bands:", image.bandNames().size().getInfo()) 8 | imageCollection = ee.ImageCollection(image.bandNames().map(lambda b: image.select([b]))) 9 | 10 | print("ImageCollection size: ", imageCollection.size().getInfo()) 11 | -------------------------------------------------------------------------------- /Image/convolutions.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load and display an image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') 6 | Map.setCenter(-121.9785, 37.8694, 11) 7 | Map.addLayer(image, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 'input image') 8 | 9 | # Define a boxcar or low-pass kernel. 10 | # boxcar = ee.Kernel.square({ 11 | # 'radius': 7, 'units': 'pixels', 'normalize': True 12 | # }) 13 | 14 | boxcar = ee.Kernel.square(7, 'pixels', True) 15 | 16 | # Smooth the image by convolving with the boxcar kernel. 17 | smooth = image.convolve(boxcar) 18 | Map.addLayer(smooth, {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 'smoothed') 19 | 20 | # Define a Laplacian, or edge-detection kernel. 21 | laplacian = ee.Kernel.laplacian8(1, False) 22 | 23 | # Apply the edge-detection kernel. 24 | edgy = image.convolve(laplacian) 25 | Map.addLayer(edgy, 26 | {'bands': ['B5', 'B4', 'B3'], 'max': 0.5}, 27 | 'edges') 28 | 29 | -------------------------------------------------------------------------------- /Image/cumulative_cost_mapping.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # A rectangle representing Bangui, Central African Republic. 5 | geometry = ee.Geometry.Rectangle([18.5229, 4.3491, 18.5833, 4.4066]) 6 | 7 | # Create a source image where the geometry is 1, everything else is 0. 8 | sources = ee.Image().toByte().paint(geometry, 1) 9 | 10 | # Mask the sources image with itself. 11 | sources = sources.updateMask(sources) 12 | 13 | # The cost data is generated from classes in ESA/GLOBCOVER. 14 | cover = ee.Image('ESA/GLOBCOVER_L4_200901_200912_V2_3').select(0) 15 | 16 | # Classes 60, 80, 110, 140 have cost 1. 17 | # Classes 40, 90, 120, 130, 170 have cost 2. 18 | # Classes 50, 70, 150, 160 have cost 3. 19 | cost = \ 20 | cover.eq(60).Or(cover.eq(80)).Or(cover.eq(110)).Or(cover.eq(140)) \ 21 | .multiply(1).add( 22 | cover.eq(40).Or(cover.eq(90)).Or(cover.eq(120)).Or(cover.eq(130)) \ 23 | .Or(cover.eq(170)) \ 24 | .multiply(2).add( 25 | cover.eq(50).Or(cover.eq(70)).Or(cover.eq(150)).Or(cover.eq(160)) \ 26 | .multiply(3))) 27 | 28 | # Compute the cumulative cost to traverse the lAnd cover. 29 | cumulativeCost = cost.cumulativeCost(**{ 30 | 'source': sources, 31 | 'maxDistance': 80 * 1000 # 80 kilometers 32 | }) 33 | 34 | # Display the results 35 | Map.setCenter(18.71, 4.2, 9) 36 | Map.addLayer(cover, {}, 'Globcover') 37 | Map.addLayer(cumulativeCost, {'min': 0, 'max': 5e4}, 'accumulated cost') 38 | Map.addLayer(geometry, {'color': 'FF0000'}, 'source geometry') 39 | -------------------------------------------------------------------------------- /Image/download.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/download.py 2 | 3 | #!/usr/bin/env python 4 | """Download example.""" 5 | 6 | import ee 7 | from ee_plugin import Map 8 | 9 | # Get a download URL for an image. 10 | image1 = ee.Image('srtm90_v4') 11 | path = image1.getDownloadUrl({ 12 | 'scale': 30, 13 | 'crs': 'EPSG:4326', 14 | 'region': '[[-120, 35], [-119, 35], [-119, 34], [-120, 34]]' 15 | }) 16 | 17 | print(path) 18 | vis_params = {'min': 0, 'max': 3000} 19 | Map.addLayer(image1, vis_params) 20 | -------------------------------------------------------------------------------- /Image/edge_detection.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 image, select the panchromatic band. 5 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318').select('B8') 6 | 7 | # Perform Canny edge detection and display the result. 8 | canny = ee.Algorithms.CannyEdgeDetector(**{ 9 | 'image': image, 'threshold': 10, 'sigma': 1 10 | }) 11 | Map.setCenter(-122.054, 37.7295, 10) 12 | Map.addLayer(canny, {}, 'canny') 13 | 14 | # Perform Hough transform of the Canny result and display. 15 | hough = ee.Algorithms.HoughTransform(canny, 256, 600, 100) 16 | Map.addLayer(hough, {}, 'hough') 17 | 18 | # Load a Landsat 8 image, select the panchromatic band. 19 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318').select('B8') 20 | Map.addLayer(image, {'max': 12000}) 21 | 22 | # Define a "fat" Gaussian kernel. 23 | fat = ee.Kernel.gaussian(**{ 24 | 'radius': 3, 25 | 'sigma': 3, 26 | 'units': 'pixels', 27 | 'normalize': True, 28 | 'magnitude': -1 29 | }) 30 | 31 | # Define a "skinny" Gaussian kernel. 32 | skinny = ee.Kernel.gaussian(**{ 33 | 'radius': 3, 34 | 'sigma': 1, 35 | 'units': 'pixels', 36 | 'normalize': True, 37 | }) 38 | 39 | # Compute a difference-of-Gaussians (DOG) kernel. 40 | dog = fat.add(skinny) 41 | 42 | # Compute the zero crossings of the second derivative, display. 43 | zeroXings = image.convolve(dog).zeroCrossing() 44 | Map.setCenter(-122.054, 37.7295, 10) 45 | Map.addLayer(zeroXings.updateMask(zeroXings), {'palette': 'FF0000'}, 'zero crossings') 46 | 47 | -------------------------------------------------------------------------------- /Image/expressions.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') 6 | 7 | # Compute the EVI using an expression. 8 | evi = image.expression( 9 | '2.5 * ((NIR - RED) / (NIR + 6 * RED - 7.5 * BLUE + 1))', { 10 | 'NIR': image.select('B5'), 11 | 'RED': image.select('B4'), 12 | 'BLUE': image.select('B2') 13 | }) 14 | 15 | Map.centerObject(image, 9) 16 | Map.addLayer(evi, {'min': -1, 'max': 1, 'palette': ['FF0000', '00FF00']}, "EVI") 17 | 18 | -------------------------------------------------------------------------------- /Image/extract_value_to_points.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Input imagery is a cloud-free Landsat 8 composite. 5 | l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1') 6 | 7 | image = ee.Algorithms.Landsat.simpleComposite(**{ 8 | 'collection': l8.filterDate('2018-01-01', '2018-12-31'), 9 | 'asFloat': True 10 | }) 11 | 12 | # Use these bands for prediction. 13 | bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'] 14 | 15 | # Load training points. The numeric property 'class' stores known labels. 16 | points = ee.FeatureCollection('GOOGLE/EE/DEMOS/demo_landcover_labels') 17 | 18 | # This property of the table stores the land cover labels. 19 | label = 'landcover' 20 | 21 | # Overlay the points on the imagery to get training. 22 | training = image.select(bands).sampleRegions(**{ 23 | 'collection': points, 24 | 'properties': [label], 25 | 'scale': 30 26 | }) 27 | 28 | # Define visualization parameters in an object literal. 29 | vizParams = {'bands': ['B5', 'B4', 'B3'], 30 | 'min': 0, 'max': 1, 'gamma': 1.3} 31 | 32 | 33 | Map.centerObject(points, 10) 34 | Map.addLayer(image, vizParams, 'Image') 35 | Map.addLayer(points, {'color': "yellow"}, 'Training points') 36 | 37 | first = training.first() 38 | print(first.getInfo()) 39 | -------------------------------------------------------------------------------- /Image/find_image_by_path_row.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/find_image_by_path_row.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | # Load an image collection, filtered so it's not too much data. 7 | collection = ee.ImageCollection('LANDSAT/LT05/C01/T1') \ 8 | .filterDate('2008-01-01', '2008-12-31') \ 9 | .filter(ee.Filter.eq('WRS_PATH', 44)) \ 10 | .filter(ee.Filter.eq('WRS_ROW', 34)) 11 | 12 | # Compute the median in each band, each pixel. 13 | # Band names are B1_median, B2_median, etc. 14 | median = collection.reduce(ee.Reducer.median()) 15 | 16 | # The output is an Image. Add it to the map. 17 | vis_param = {'bands': ['B4_median', 'B3_median', 'B2_median'], 'gamma': 1.6} 18 | Map.setCenter(-122.3355, 37.7924, 9) 19 | Map.addLayer(median, vis_param, 'Median Image') 20 | -------------------------------------------------------------------------------- /Image/from_name.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Display an image given its ID.""" 3 | 4 | import ee 5 | from ee_plugin import Map 6 | 7 | image = ee.Image('srtm90_v4') 8 | vis_params = {'min': 0, 'max': 3000} 9 | Map.addLayer(image, vis_params,"SRTM") 10 | Map.setCenter(0,0, 2) 11 | -------------------------------------------------------------------------------- /Image/get_band_name_and_type.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_band_name_and_type.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | roi = ee.Geometry.Point([-99.2182, 46.7824]) 7 | 8 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \ 9 | .filterBounds(roi) \ 10 | .filter(ee.Filter.calendarRange(6, 6, 'month')) \ 11 | .sort('DATE_ACQUIRED') 12 | 13 | print(collection.size().getInfo()) 14 | 15 | first = ee.Image(collection.first()) 16 | print(first.bandNames().getInfo()) 17 | print(first.bandTypes().getInfo()) 18 | -------------------------------------------------------------------------------- /Image/get_image_extent.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_image_extent.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318') 7 | 8 | # Define visualization parameters in an object literal. 9 | vizParams = {'bands': ['B5', 'B4', 'B3'], 10 | 'min': 5000, 'max': 15000, 'gamma': 1.3} 11 | 12 | # Center the map on the image and display. 13 | Map.centerObject(image, 9) 14 | Map.addLayer(image, vizParams, 'Landsat 8 False color') 15 | 16 | extent = image.geometry() 17 | outline = ee.Image().paint(extent, 0, 2) 18 | Map.addLayer(outline, {'palette': "yellow"}, "Image extent") 19 | 20 | coordinates = extent.coordinates() 21 | 22 | print(coordinates.getInfo()) 23 | -------------------------------------------------------------------------------- /Image/get_image_id.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_image_id.py 2 | 3 | # GitHub URL: https:#github.com/giswqs/qgis-earthengine-examples/tree/master/ImageCollection/sort_by_cloud_and_date.py 4 | 5 | import ee 6 | from ee_plugin import Map 7 | 8 | # This function masks the input with a threshold on the simple cloud score. 9 | 10 | 11 | def cloudMask(img): 12 | cloudscore = ee.Algorithms.Landsat.simpleCloudScore(img).select('cloud') 13 | return img.updateMask(cloudscore.lt(50)) 14 | 15 | 16 | # Load a Landsat 5 image collection. 17 | collection = ee.ImageCollection('LANDSAT/LT5_L1T_TOA') \ 18 | .filterDate('2008-04-01', '2010-04-01') \ 19 | .filterBounds(ee.Geometry.Point(-122.2627, 37.8735)) \ 20 | .map(cloudMask) \ 21 | .select(['B4', 'B3']) \ 22 | .sort('system:time_start', True) # Sort the collection in chronological order. 23 | 24 | print(collection.size().getInfo()) 25 | 26 | first = collection.first() 27 | propertyNames = first.propertyNames() 28 | print(propertyNames.getInfo()) 29 | 30 | uid = first.get('system:id') 31 | print(uid.getInfo()) 32 | -------------------------------------------------------------------------------- /Image/get_image_resolution.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_image_resolution.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | naip = ee.Image('USDA/NAIP/DOQQ/m_3712213_sw_10_1_20140613') 7 | Map.setCenter(-122.466123, 37.769833, 17) 8 | Map.addLayer(naip, {'bands': ['N', 'R','G']}, 'NAIP') 9 | 10 | naip_resolution =naip.select('N').projection().nominalScale() 11 | print("NAIP resolution: ", naip_resolution.getInfo()) 12 | 13 | 14 | landsat = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318') 15 | landsat_resolution =landsat.select('B1').projection().nominalScale() 16 | print("Landsat resolution: ", landsat_resolution.getInfo()) -------------------------------------------------------------------------------- /Image/gradients.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 image and select the panchromatic band. 5 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318').select('B8') 6 | 7 | # Compute the image gradient in the X and Y directions. 8 | xyGrad = image.gradient() 9 | 10 | # Compute the magnitude of the gradient. 11 | gradient = xyGrad.select('x').pow(2) \ 12 | .add(xyGrad.select('y').pow(2)).sqrt() 13 | 14 | # Compute the direction of the gradient. 15 | direction = xyGrad.select('y').atan2(xyGrad.select('x')) 16 | 17 | # Display the results. 18 | Map.setCenter(-122.054, 37.7295, 10) 19 | Map.addLayer(direction, {'min': -2, 'max': 2}, 'direction') 20 | Map.addLayer(gradient, {'min': -7, 'max': 7}, 'gradient') 21 | 22 | -------------------------------------------------------------------------------- /Image/hdr_landsat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """HDR Landsat. 3 | 4 | Display portions of an image with different dynamic ranges. 5 | The land areas are displayed normally, but the water areas 6 | are streched to show more details. 7 | """ 8 | 9 | import ee 10 | from ee_plugin import Map 11 | import datetime 12 | 13 | 14 | Map.setCenter(-95.738, 18.453, 9) 15 | 16 | # Filter the LE7 collection to a single date. 17 | collection = (ee.ImageCollection('LE7_L1T') 18 | .filterDate(datetime.datetime(2002, 11, 8), 19 | datetime.datetime(2002, 11, 9))) 20 | image = collection.mosaic().select('B3', 'B2', 'B1') 21 | 22 | # Display the image normally. 23 | Map.addLayer(image, {'gain': '1.6, 1.4, 1.1'}, 'Land') 24 | 25 | # Add and stretch the water. Once where the elevation is masked, 26 | # and again where the elevation is zero. 27 | elev = ee.Image('srtm90_v4') 28 | mask1 = elev.mask().eq(0).And(image.mask()) 29 | mask2 = elev.eq(0).And(image.mask()) 30 | Map.addLayer(image.mask(mask1), {'gain': 6.0, 'bias': -200}, 'Water: Masked') 31 | Map.addLayer(image.mask(mask2), {'gain': 6.0, 'bias': -200}, 'Water: Elev 0') 32 | -------------------------------------------------------------------------------- /Image/hillshade.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Compute hillshade from elevation.""" 3 | 4 | import ee 5 | from ee_plugin import Map 6 | import math 7 | 8 | Map.setCenter(-121.767, 46.852, 11) 9 | 10 | def Radians(img): 11 | return img.toFloat().multiply(math.pi).divide(180) 12 | 13 | 14 | def Hillshade(az, ze, slope, aspect): 15 | """Compute hillshade for the given illumination az, el.""" 16 | azimuth = Radians(ee.Image(az)) 17 | zenith = Radians(ee.Image(ze)) 18 | # Hillshade = cos(Azimuth - Aspect) * sin(Slope) * sin(Zenith) + 19 | # cos(Zenith) * cos(Slope) 20 | return (azimuth.subtract(aspect).cos() 21 | .multiply(slope.sin()) 22 | .multiply(zenith.sin()) 23 | .add( 24 | zenith.cos().multiply(slope.cos()))) 25 | 26 | terrain = ee.Algorithms.Terrain(ee.Image('srtm90_v4')) 27 | slope_img = Radians(terrain.select('slope')) 28 | aspect_img = Radians(terrain.select('aspect')) 29 | 30 | # Add 1 hillshade at az=0, el=60. 31 | Map.addLayer(Hillshade(0, 60, slope_img, aspect_img), {}, 'Hillshade') 32 | -------------------------------------------------------------------------------- /Image/hough_transform.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # An example finding linear features using the HoughTransform. 5 | 6 | # Load an image and compute NDVI. 7 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_033032_20170719') 8 | ndvi = image.normalizedDifference(['B5', 'B4']) 9 | 10 | # Apply a Canny edge detector. 11 | canny = ee.Algorithms.CannyEdgeDetector(**{ 12 | 'image': ndvi, 13 | 'threshold': 0.4 14 | }).multiply(255) 15 | 16 | # Apply the Hough transform. 17 | h = ee.Algorithms.HoughTransform(**{ 18 | 'image': canny, 19 | 'gridSize': 256, 20 | 'inputThreshold': 50, 21 | 'lineThreshold': 100 22 | }) 23 | 24 | # Display. 25 | Map.setCenter(-103.80140, 40.21729, 13) 26 | Map.addLayer(image, {'bands': ['B4', 'B3', 'B2'], 'max': 0.3}, 'source_image') 27 | Map.addLayer(canny.updateMask(canny), {'min': 0, 'max': 1, 'palette': 'blue'}, 'canny') 28 | Map.addLayer(h.updateMask(h), {'min': 0, 'max': 1, 'palette': 'red'}, 'hough') 29 | -------------------------------------------------------------------------------- /Image/hsv_pan_sharpen.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """HSV-based Pan-Sharpening example.""" 3 | 4 | import ee 5 | from ee_plugin import Map 6 | 7 | # There are many fine places to look here is one. Comment 8 | # this out if you want to twiddle knobs while panning around. 9 | Map.setCenter(-61.61625, -11.64273, 14) 10 | 11 | # Grab a sample L7 image and pull out the RGB and pan bands 12 | # in the range (0, 1). (The range of the pan band values was 13 | # chosen to roughly match the other bands.) 14 | image1 = ee.Image('LANDSAT/LE7/LE72300681999227EDC00') 15 | 16 | rgb = image1.select('B3', 'B2', 'B1').unitScale(0, 255) 17 | gray = image1.select('B8').unitScale(0, 155) 18 | 19 | # Convert to HSV, swap in the pan band, and convert back to RGB. 20 | huesat = rgb.rgbToHsv().select('hue', 'saturation') 21 | upres = ee.Image.cat(huesat, gray).hsvToRgb() 22 | 23 | # Display before and after layers using the same vis parameters. 24 | visparams = {'min': [.15, .15, .25], 'max': [1, .9, .9], 'gamma': 1.6} 25 | Map.addLayer(rgb, visparams, 'Orignal') 26 | Map.addLayer(upres, visparams, 'Pansharpened') 27 | -------------------------------------------------------------------------------- /Image/image_metadata.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | image = ee.Image('LANDSAT/LC8_L1T/LC80440342014077LGN00') 5 | 6 | bandNames = image.bandNames() 7 | print('Band names: ', bandNames.getInfo()) # ee.List 8 | 9 | b1proj = image.select('B1').projection() 10 | print('Band 1 projection: ', b1proj.getInfo()) # ee.Projection 11 | 12 | b1scale = image.select('B1').projection().nominalScale() 13 | print('Band 1 scale: ', b1scale.getInfo()) # ee.Number 14 | 15 | b8scale = image.select('B8').projection().nominalScale() 16 | print('Band 8 scale: ', b8scale.getInfo()) # ee.Number 17 | 18 | properties = image.propertyNames() 19 | print('Metadata properties: ', properties.getInfo()) # ee.List 20 | 21 | cloudiness = image.get('CLOUD_COVER') 22 | print('CLOUD_COVER: ', cloudiness.getInfo()) # ee.Number 23 | 24 | date = ee.Date(image.get('system:time_start')) 25 | print('Timestamp: ', date.getInfo()) # ee.Date 26 | -------------------------------------------------------------------------------- /Image/image_overview.py: -------------------------------------------------------------------------------- 1 | import ee 2 | 3 | 4 | image1 = ee.Image(1) 5 | print(image1) 6 | print(image1.getInfo()) 7 | 8 | image2 = ee.Image(2) 9 | image3 = ee.Image.cat([image1, image2]) 10 | print(image3.getInfo()) 11 | 12 | multiband = ee.Image([1, 2, 3]) 13 | print(multiband) 14 | renamed = multiband.select(['constant','constant_1','constant_2'],['band1','band2','band3']) 15 | print(renamed) -------------------------------------------------------------------------------- /Image/image_smoothing.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/image_smoothing.py 2 | 3 | 4 | import ee 5 | from ee_plugin import Map 6 | 7 | image = ee.Image('srtm90_v4') 8 | 9 | smoothed = image.reduceNeighborhood(**{ 10 | 'reducer': ee.Reducer.mean(), 11 | 'kernel': ee.Kernel.square(3), 12 | }) 13 | 14 | # vis_params = {'min': 0, 'max': 3000} 15 | # Map.addLayer(image, vis_params, 'SRTM original') 16 | # Map.addLayer(smooth, vis_params, 'SRTM smoothed') 17 | Map.setCenter(-112.40, 42.53, 12) 18 | Map.addLayer(ee.Terrain.hillshade(image), {}, 'Original hillshade') 19 | Map.addLayer(ee.Terrain.hillshade(smoothed), {}, 'Smoothed hillshade') 20 | -------------------------------------------------------------------------------- /Image/image_stats_by_band.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/image_stats_by_band.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | image = ee.Image('USDA/NAIP/DOQQ/m_3712213_sw_10_1_20140613') 7 | Map.setCenter(-122.466123, 37.769833, 17) 8 | Map.addLayer(image, {'bands': ['N', 'R','G']}, 'NAIP') 9 | 10 | geometry = image.geometry() 11 | 12 | means = image.reduceRegions(geometry, ee.Reducer.mean().forEachBand(image), 10) 13 | 14 | print(means.getInfo()) 15 | -------------------------------------------------------------------------------- /Image/image_vis.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | image = ee.Image('LANDSAT/LC8_L1T_TOA/LC80440342014077LGN00') 5 | # print(image.getInfo()) 6 | Map.setCenter(-122.1899, 37.5010, 10) 7 | vis = {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5, 'gamma': [0.95, 1.1, 1]} 8 | # Map.addLayer(image, vis) 9 | 10 | 11 | # Color palettes 12 | image = ee.Image('LANDSAT/LC8_L1T_TOA/LC80440342014077LGN00') 13 | ndwi = image.normalizedDifference(['B3', 'B5']) 14 | ndwiViz = {'min': 0.5, 'max': 1, 'palette': ['00FFFF', '0000FF']} 15 | # Map.addLayer(ndwi, ndwiViz, 'NDWI') 16 | 17 | # Masking 18 | ndwiMasked = ndwi.updateMask(ndwi.gte(0.4)) 19 | # Map.addLayer(ndwiMasked, ndwiViz, 'NDWI masked') 20 | 21 | # Create visualization layers. 22 | imageRGB = image.visualize({'bands': ['B5', 'B4', 'B3'], max: 0.5}) 23 | ndwiRGB = ndwiMasked.visualize({ 24 | 'min': 0.5, 25 | 'max': 1, 26 | 'palette': ['00FFFF', '0000FF'] 27 | }) 28 | 29 | # Mosaic the visualization layers and display( or export). 30 | # mosaic = ee.ImageCollection([imageRGB, ndwiRGB]).mosaic() 31 | # mosaic = ee.ImageCollection([image,ndwiMasked]).mosaic() 32 | # Map.addLayer(mosaic, {}, 'mosaic') 33 | 34 | 35 | roi = ee.Geometry.Point([-122.4481, 37.7599]).buffer(20000) 36 | Map.addLayer(image.clip(roi), vis, 'Landsat 8') 37 | Map.addLayer(ndwiMasked.clip(roi),ndwiViz, 'NDWI') 38 | 39 | -------------------------------------------------------------------------------- /Image/landcover_cleanup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Landcover cleanup. 3 | 4 | Display the MODIS land cover classification image with appropriate colors. 5 | """ 6 | 7 | import ee 8 | from ee_plugin import Map 9 | 10 | Map.setCenter(-113.41842, 40.055489, 6) 11 | 12 | # Force projection of 500 meters/pixel, which is the native MODIS resolution. 13 | VECTORIZATION_SCALE = 500 14 | 15 | image1 = ee.Image('MCD12Q1/MCD12Q1_005_2001_01_01') 16 | image2 = image1.select(['Land_Cover_Type_1']) 17 | image3 = image2.reproject('EPSG:4326', None, 500) 18 | image4 = image3.focal_mode() 19 | image5 = image4.focal_max(3).focal_min(5).focal_max(3) 20 | image6 = image5.reproject('EPSG:4326', None, 500) 21 | 22 | PALETTE = [ 23 | 'aec3d4', # water 24 | '152106', '225129', '369b47', '30eb5b', '387242', # forest 25 | '6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40', # shrub, grass, savannah 26 | '111149', # wetlands 27 | 'cdb33b', # croplands 28 | 'cc0013', # urban 29 | '33280d', # crop mosaic 30 | 'd7cdcc', # snow and ice 31 | 'f7e084', # barren 32 | '6f6f6f' # tundra 33 | ] 34 | 35 | vis_params = {'min': 0, 'max': 17, 'palette': PALETTE} 36 | 37 | Map.addLayer(image2, vis_params, 'IGBP classification') 38 | Map.addLayer(image3, vis_params, 'Reprojected') 39 | Map.addLayer(image4, vis_params, 'Mode') 40 | Map.addLayer(image5, vis_params, 'Smooth') 41 | Map.addLayer(image6, vis_params, 'Smooth') 42 | -------------------------------------------------------------------------------- /Image/morphological_operations.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 image, select the NIR band, threshold, display. 5 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') \ 6 | .select(4).gt(0.2) 7 | Map.setCenter(-122.1899, 37.5010, 13) 8 | Map.addLayer(image, {}, 'NIR threshold') 9 | 10 | # Define a kernel. 11 | kernel = ee.Kernel.circle(**{'radius': 1}) 12 | 13 | # Perform an erosion followed by a dilation, display. 14 | opened = image \ 15 | .focal_min(**{'kernel': kernel, 'iterations': 2}) \ 16 | .focal_max(**{'kernel': kernel, 'iterations': 2}) 17 | Map.addLayer(opened, {}, 'opened') 18 | 19 | -------------------------------------------------------------------------------- /Image/normalized_difference.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # NormalizedDifference example. 5 | # 6 | # Compute Normalized Difference Vegetation Index over MOD09GA product. 7 | # NDVI = (NIR - RED) / (NIR + RED), where 8 | # RED is sur_refl_b01, 620-670nm 9 | # NIR is sur_refl_b02, 841-876nm 10 | 11 | # Load a MODIS image. 12 | img = ee.Image('MODIS/006/MOD09GA/2012_03_09') 13 | 14 | # Use the normalizedDifference(A, B) to compute (A - B) / (A + B) 15 | ndvi = img.normalizedDifference(['sur_refl_b02', 'sur_refl_b01']) 16 | 17 | # Make a 'palette': a list of hex strings. 18 | palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', 19 | '74A901', '66A000', '529400', '3E8601', '207401', '056201', 20 | '004C00', '023B01', '012E01', '011D01', '011301'] 21 | 22 | # Center the map 23 | Map.setCenter(-94.84497, 39.01918, 8) 24 | 25 | # Display the input image and the NDVI derived from it. 26 | Map.addLayer(img.select(['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03']), 27 | {'gain': [0.1, 0.1, 0.1]}, 'MODIS bands 1/4/3') 28 | Map.addLayer(ndvi, {'min': 0, 'max': 1, 'palette': palette}, 'NDVI') 29 | -------------------------------------------------------------------------------- /Image/pansharpen.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 top-of-atmosphere reflectance image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') 6 | Map.addLayer( 7 | image, 8 | {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 0.25, 'gamma': [1.1, 1.1, 1]}, 9 | 'rgb') 10 | 11 | # Convert the RGB bands to the HSV color space. 12 | hsv = image.select(['B4', 'B3', 'B2']).rgbToHsv() 13 | 14 | # Swap in the panchromatic band and convert back to RGB. 15 | sharpened = ee.Image.cat([ 16 | hsv.select('hue'), hsv.select('saturation'), image.select('B8') 17 | ]).hsvToRgb() 18 | 19 | # Display the pan-sharpened result. 20 | Map.setCenter(-122.44829, 37.76664, 13) 21 | Map.addLayer(sharpened, 22 | {'min': 0, 'max': 0.25, 'gamma': [1.3, 1.3, 1.3]}, 23 | 'pan-sharpened') 24 | 25 | -------------------------------------------------------------------------------- /Image/pixel_area.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Displays the decreasing area covered by a single pixel at 5 | # higher latitudes using the Image.pixelArea() function. 6 | 7 | # Create an image in which the value of each pixel is its area. 8 | img = ee.Image.pixelArea() 9 | Map.setCenter(0, 0, 3) 10 | Map.addLayer(img, {'min': 2e8, 'max': 4e8, 'opacity': 0.85}, 'pixel area') 11 | -------------------------------------------------------------------------------- /Image/pixel_lon_lat.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Draws 60 lat/long lines per degree using the pixelLonLat() function. 5 | 6 | # Create an image in which the value of each pixel is its 7 | # coordinates in minutes. 8 | img = ee.Image.pixelLonLat().multiply(60.0) 9 | 10 | # Get the decimal part and check if it's less than a small delta. 11 | img = img.subtract(img.floor()).lt(0.05) 12 | 13 | # The pixels less than the delta are the grid, in both directions. 14 | grid = img.select('latitude').Or(img.select('longitude')) 15 | 16 | # Draw the grid. 17 | Map.setCenter(-122.09228, 37.42330, 12) 18 | Map.addLayer(grid.updateMask(grid), {'palette': '008000'}, 'Graticule') 19 | -------------------------------------------------------------------------------- /Image/polynomial.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Applies a non-linear contrast enhancement to a MODIS image using 5 | # function -0.2 + 2.4x - 1.2x^2. 6 | 7 | # Load a MODIS image and apply the scaling factor. 8 | img = ee.Image('MODIS/006/MOD09GA/2012_03_09') \ 9 | .select(['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03']) \ 10 | .multiply(0.0001) 11 | 12 | # Apply the polynomial enhancement. 13 | adj = img.polynomial([-0.2, 2.4, -1.2]) 14 | 15 | Map.setCenter(-107.24304, 35.78663, 8) 16 | Map.addLayer(img, {'min': 0, 'max': 1}, 'original') 17 | Map.addLayer(adj, {'min': 0, 'max': 1}, 'adjusted') 18 | -------------------------------------------------------------------------------- /Image/reduce_region.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Image.reduceRegion example 5 | # 6 | # Computes a simple reduction over a region of an image. A reduction 7 | # is any process that takes an arbitrary number of inputs (such as 8 | # all the pixels of an image in a given region) and computes one or 9 | # more fixed outputs. The result is a dictionary that contains the 10 | # computed values, which in this example is the maximum pixel value 11 | # in the region. 12 | 13 | # This example shows how to print the resulting dictionary to the 14 | # console, which is useful when developing and debugging your 15 | # scripts, but in a larger workflow you might instead use the 16 | # Dicitionary.get() function to extract the values you need from the 17 | # dictionary for use as inputs to other functions. 18 | 19 | # The input image to reduce, in this case an SRTM elevation map. 20 | image = ee.Image('CGIAR/SRTM90_V4') 21 | 22 | # The region to reduce within. 23 | poly = ee.Geometry.Rectangle([-109.05, 41, -102.05, 37]) 24 | 25 | # Reduce the image within the given region, using a reducer that 26 | # computes the max pixel value. We also specify the spatial 27 | # resolution at which to perform the computation, in this case 200 28 | # meters. 29 | max = image.reduceRegion(**{ 30 | 'reducer': ee.Reducer.max(), 31 | 'geometry': poly, 32 | 'scale': 200 33 | }) 34 | 35 | # Print the result (a Dictionary) to the console. 36 | print(max.getInfo()) 37 | -------------------------------------------------------------------------------- /Image/relational_operators.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a 2012 nightlights image. 5 | nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012') 6 | lights = nl2012.select('stable_lights') 7 | 8 | # Define arbitrary thresholds on the 6-bit stable lights band. 9 | zones = lights.gt(30).add(lights.gt(55)).add(lights.gt(62)) 10 | 11 | # Display the thresholded image as three distinct zones near Paris. 12 | palette = ['000000', '0000FF', '00FF00', 'FF0000'] 13 | Map.setCenter(2.373, 48.8683, 8) 14 | Map.addLayer(zones, {'min': 0, 'max': 3, 'palette': palette}, 'development zones') 15 | 16 | # Create zones using an expression, display. 17 | zonesExp = nl2012.expression( 18 | "(b('stable_lights') > 62) ? 3" + 19 | ": (b('stable_lights') > 55) ? 2" + 20 | ": (b('stable_lights') > 30) ? 1" + 21 | ": 0" 22 | ) 23 | Map.addLayer(zonesExp, 24 | {'min': 0, 'max': 3, 'palette': palette}, 25 | 'development zones (ternary)') 26 | 27 | -------------------------------------------------------------------------------- /Image/rename_bands.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/rename_bands.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | image = ee.Image('LANDSAT/LC8_L1T/LC80260412017023LGN00') 7 | b5 = image.select('B5') 8 | 9 | Map.centerObject(image, 10) 10 | Map.addLayer(image, {}, 'Band 5') 11 | 12 | selected = image.select(["B5", 'B4', 'B3'], ['Nir', 'Red', 'Green']) 13 | Map.addLayer(selected, {}, "Renamed bands") 14 | print(selected.bandNames().getInfo()) 15 | 16 | -------------------------------------------------------------------------------- /Image/select_bands.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/select_bands.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | # Load an image. 7 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318') 8 | 9 | band345 = image.select(['B[3-5]']) 10 | 11 | bandNames = band345.bandNames() 12 | 13 | print(bandNames.getInfo()) 14 | 15 | # Define visualization parameters in an object literal. 16 | vizParams = {'bands': ['B5', 'B4', 'B3'], 17 | 'min': 5000, 'max': 15000, 'gamma': 1.3} 18 | 19 | # Center the map on the image and display. 20 | Map.centerObject(image, 9) 21 | Map.addLayer(band345, vizParams, 'Landsat 8 False color') 22 | -------------------------------------------------------------------------------- /Image/set_image_properties.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/set_image_properties.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | 7 | def addDate(image): 8 | # parse date stored in 'system:index' 9 | date = ee.Date(image.get('system:index')) 10 | 11 | # format date, see http:#www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html 12 | str = date.format('YYYY-mm-dd') 13 | 14 | return image.set({'Date': str}) 15 | 16 | 17 | # point = ee.Geometry.Point(-122.262, 37.8719) 18 | # start = ee.Date('2014-06-01') 19 | # finish = ee.Date('2014-10-01') 20 | 21 | # filteredCollection = ee.ImageCollection('LANDSAT/LC08/C01/T1') \ 22 | # .filterBounds(point) \ 23 | # .filterDate(start, finish) \ 24 | # .sort('CLOUD_COVER', True) 25 | 26 | filteredCollection = ee.ImageCollection('users/sdavidcomer/L7maskedNDVIdated') 27 | 28 | # Bring in image collection 29 | # ndvi = ee.ImageCollection('users/sdavidcomer/L7maskedNDVIdated') 30 | 31 | # Map addDate over image collection 32 | result = filteredCollection.map(addDate) 33 | print(result.first().get('Date').getInfo()) 34 | -------------------------------------------------------------------------------- /Image/spectral_unmixing.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 5 image and select the bands we want to unmix. 5 | bands = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7'] 6 | image = ee.Image('LANDSAT/LT05/C01/T1/LT05_044034_20080214') \ 7 | .select(bands) 8 | Map.setCenter(-122.1899, 37.5010, 10) # San Francisco Bay 9 | Map.addLayer(image, {'bands': ['B4', 'B3', 'B2'], 'min': 0, 'max': 128}, 'image') 10 | 11 | # Define spectral endmembers. 12 | urban = [88, 42, 48, 38, 86, 115, 59] 13 | veg = [50, 21, 20, 35, 50, 110, 23] 14 | water = [51, 20, 14, 9, 7, 116, 4] 15 | 16 | # Unmix the image. 17 | fractions = image.unmix([urban, veg, water]) 18 | Map.addLayer(fractions, {}, 'unmixed') 19 | 20 | -------------------------------------------------------------------------------- /Image/symbology.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | 5 | cover = ee.Image('MODIS/051/MCD12Q1/2012_01_01').select('Land_Cover_Type_1') 6 | 7 | igbpPalette = [ 8 | 'aec3d4', 9 | '152106', '225129', '369b47', '30eb5b', '387242', 10 | '6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40', 11 | '111149', 12 | 'cdb33b', 13 | 'cc0013', 14 | '33280d', 15 | 'd7cdcc', 16 | 'f7e084', 17 | '6f6f6f' 18 | ] 19 | 20 | Map.setCenter(-99.229, 40.413, 5) 21 | Map.addLayer(cover, {'min': 0, 'max': 17, 'palette': igbpPalette}, 'MODIS Land Cover') 22 | 23 | -------------------------------------------------------------------------------- /Image/where.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Where operator example. 3 | 4 | Select the forest classes from the MODIS land cover image and intersect them 5 | with elevations above 1000m. 6 | """ 7 | 8 | import ee 9 | from ee_plugin import Map 10 | 11 | Map.setCenter(-113.41842, 40.055489, 6) 12 | 13 | elev = ee.Image('srtm90_v4') 14 | cover = ee.Image('MCD12Q1/MCD12Q1_005_2001_01_01').select('Land_Cover_Type_1') 15 | blank = ee.Image(0) 16 | 17 | # Where (1 <= cover <= 4) and (elev > 1000), set the output to 1. 18 | output = blank.where( 19 | cover.lte(4).And(cover.gte(1)).And(elev.gt(1000)), 20 | 1) 21 | 22 | # Output contains 0s and 1s. Mask it with itself to get rid of the 0s. 23 | result = output.mask(output) 24 | vis = {'min': 0, 'max': 3000} 25 | Map.addLayer(elev, vis, 'SRTM') 26 | Map.addLayer(result, {'palette': '00AA00'}, 'Land Cover') 27 | -------------------------------------------------------------------------------- /Image/where_operators.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a cloudy Landsat 8 image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20130603') 6 | Map.addLayer(image, 7 | {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5}, 8 | 'original image') 9 | 10 | # Load another image to replace the cloudy pixels. 11 | replacement = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20130416') 12 | 13 | # Compute a cloud score band. 14 | cloud = ee.Algorithms.Landsat.simpleCloudScore(image).select('cloud') 15 | 16 | # Set cloudy pixels to the other image. 17 | replaced = image.where(cloud.gt(10), replacement) 18 | 19 | # Display the result. 20 | Map.centerObject(image, 9) 21 | Map.addLayer(replaced, 22 | {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 0.5}, 23 | 'clouds replaced') 24 | 25 | -------------------------------------------------------------------------------- /Image/zero_crossing.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Mark pixels where the elevation crosses 1000m value and compare 5 | # that to pixels that are exactly equal to 1000m. 6 | 7 | elev = ee.Image('CGIAR/SRTM90_V4') 8 | 9 | # A zero-crossing is defined as any pixel where the right, 10 | # bottom, or diagonal bottom-right pixel has the opposite sign. 11 | image = elev.subtract(1000).zeroCrossing() 12 | 13 | Map.setCenter(-121.68148, 37.50877, 13) 14 | Map.addLayer(image, {'min': 0, 'max': 1, 'opacity': 0.5}, 'Crossing 1000m') 15 | 16 | exact = elev.eq(1000) 17 | Map.addLayer(exact.updateMask(exact), {'palette': 'red'}, 'Exactly 1000m') 18 | -------------------------------------------------------------------------------- /ImageCollection/clipped_composite.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Composite an image collection and clip it to a boundary from a fusion table. 3 | 4 | See also: Filtered Seasonal Composite, which filters the 5 | collection by bounds instead. 6 | """ 7 | 8 | # import datetime 9 | import ee 10 | from ee_plugin import Map 11 | 12 | 13 | Map.setCenter(-110, 40, 5) 14 | fc = ee.FeatureCollection('TIGER/2018/States').filter(ee.Filter.eq('STUSPS', 'MN')) 15 | 16 | # Create a Landsat 7, median-pixel composite for Spring of 2000. 17 | collection = ee.ImageCollection('LE7_L1T').filterDate("2000-05-01", "2000-10-31") \ 18 | .filterBounds(fc) 19 | image1 = collection.median() 20 | # Map.addLayer(image1) 21 | 22 | # # Clip to the output image to the California state boundary. 23 | # # fc = (ee.FeatureCollection('ft:1fRY18cjsHzDgGiJiS2nnpUU3v9JPDc2HNaR7Xk8') 24 | # # .filter(ee.Filter().eq('Name', 'Minnesota'))) 25 | 26 | 27 | image2 = image1.clipToCollection(fc) 28 | 29 | # Select the red, green and blue bands. 30 | image = image2.select('B4', 'B3', 'B2') 31 | Map.addLayer(image, {'gain': [1.4, 1.4, 1.1]}, 'Landsat 7') 32 | -------------------------------------------------------------------------------- /ImageCollection/convert_imagecollection_to_image.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/ImageCollection/convert_imagecollection_to_image.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | images = ee.ImageCollection('MODIS/MCD43A4') \ 7 | .filterDate('2017-01-01', '2017-01-31') \ 8 | .select(['Nadir_Reflectance_Band1']) 9 | 10 | # unmask to ensure we have the same number of values everywhere 11 | images = images.map(lambda i: i.unmask(-1)) 12 | 13 | # convert to array 14 | array = images.toArray() 15 | 16 | # convert to an image 17 | bandNames = images.aggregate_array('system:index') 18 | image = array.arrayProject([0]).arrayFlatten([bandNames]) 19 | 20 | print(image.getInfo()) 21 | 22 | bandNames = image.bandNames() 23 | print(bandNames.getInfo()) 24 | -------------------------------------------------------------------------------- /ImageCollection/expression_map.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Map an expression. 3 | 4 | Computes the mean NDVI and SAVI by mapping an expression over a collection 5 | and taking the mean. This intentionally exercises both variants of 6 | Image.expression. 7 | """ 8 | import ee 9 | from ee_plugin import Map 10 | 11 | fc = ee.FeatureCollection('TIGER/2018/States').filter(ee.Filter.eq('STUSPS', 'MN')) 12 | # Filter the L7 collection to a single month. 13 | collection = (ee.ImageCollection('LE7_L1T_TOA') 14 | .filterDate("2002-11-01", "2002-12-01") 15 | .filterBounds(fc)) 16 | 17 | def NDVI(image): 18 | """A function to compute NDVI.""" 19 | return image.expression('float(b("B4") - b("B3")) / (b("B4") + b("B3"))') 20 | 21 | 22 | def SAVI(image): 23 | """A function to compute Soil Adjusted Vegetation Index.""" 24 | return ee.Image(0).expression( 25 | '(1 + L) * float(nir - red)/ (nir + red + L)', 26 | { 27 | 'nir': image.select('B4'), 28 | 'red': image.select('B3'), 29 | 'L': 0.2 30 | }) 31 | 32 | vis = { 33 | 'min': 0, 34 | 'max': 1, 35 | 'palette': [ 36 | 'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', 37 | '99B718', '74A901', '66A000', '529400', '3E8601', 38 | '207401', '056201', '004C00', '023B01', '012E01', 39 | '011D01', '011301' 40 | ]} 41 | 42 | Map.setCenter(-110, 40, 5) 43 | Map.addLayer(collection.map(NDVI).mean(), vis) 44 | Map.addLayer(collection.map(SAVI).mean(), vis) 45 | 46 | -------------------------------------------------------------------------------- /ImageCollection/filtered_composite.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Filter an image collection by date and region to make a median composite. 3 | 4 | See also: Clipped composite, which crops the output image 5 | instead of filtering the input collection. 6 | """ 7 | 8 | import datetime 9 | import ee 10 | from ee_plugin import Map 11 | 12 | 13 | Map.setCenter(-110, 40, 7) 14 | 15 | # Filter to only include images within the colorado and utah boundaries. 16 | polygon = ee.Geometry.Polygon([[ 17 | [-109.05, 37.0], [-102.05, 37.0], [-102.05, 41.0], # colorado 18 | [-109.05, 41.0], [-111.05, 41.0], [-111.05, 42.0], # utah 19 | [-114.05, 42.0], [-114.05, 37.0], [-109.05, 37.0]]]) 20 | 21 | # Create a Landsat 7 composite for Spring of 2000, and filter by 22 | # the bounds of the FeatureCollection. 23 | collection = (ee.ImageCollection('LE7_L1T') 24 | .filterDate("2000-04-01", "2000-07-01") 25 | .filterBounds(polygon)) 26 | 27 | # Select the median pixel. 28 | image1 = collection.median() 29 | 30 | # Select the red, green and blue bands. 31 | image = image1.select('B3', 'B2', 'B1') 32 | Map.addLayer(image, {'gain': [1.4, 1.4, 1.1]}) 33 | -------------------------------------------------------------------------------- /ImageCollection/filtering_by_band_names.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/ImageCollection/filtering_by_band_names.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | roi = ee.Geometry.Point([-99.2182, 46.7824]) 7 | 8 | 9 | collection = ee.ImageCollection('USDA/NAIP/DOQQ') \ 10 | .filterBounds(roi) \ 11 | .filter(ee.Filter.listContains("system:band_names", "N")) 12 | print(collection.size().getInfo()) 13 | 14 | first = collection.first() 15 | Map.centerObject(first, 13) 16 | Map.addLayer(first, {'bands': ['N', 'R', 'G']}, 'NAIP') -------------------------------------------------------------------------------- /ImageCollection/filtering_by_calendar_range.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/ImageCollection/filtering_by_calendar_range.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | roi = ee.Geometry.Point([-99.2182, 46.7824]) 7 | 8 | # find images acquired during June and July 9 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \ 10 | .filterBounds(roi) \ 11 | .filter(ee.Filter.calendarRange(6, 7, 'month')) \ 12 | .sort('DATE_ACQUIRED') 13 | 14 | print(collection.size().getInfo()) 15 | 16 | first = collection.first() 17 | propertyNames = first.propertyNames() 18 | print(propertyNames.getInfo()) 19 | 20 | time_start = ee.Date(first.get('system:time_start')).format("YYYY-MM-dd") 21 | print(time_start.getInfo()) -------------------------------------------------------------------------------- /ImageCollection/filtering_by_metadata.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/ImageCollection/filtering_by_metadata.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | region = ee.Geometry.Polygon( 7 | [[[121.89674377441406, 11.91539248304918], 8 | [121.98291778564453, 11.93218823174339], 9 | [121.95236206054688, 12.020516709145957], 10 | [121.86378679003906, 12.006748772470699]]]) 11 | 12 | # Image Collection 13 | collection = ee.ImageCollection('COPERNICUS/S2') \ 14 | .filterDate('2017-03-01', '2017-04-04') \ 15 | .filterBounds(region) \ 16 | .filterMetadata('CLOUDY_PIXEL_PERCENTAGE', 'less_than', 20) 17 | # Map.addLayer(collection, {'min': 0, 'max': 3000, 18 | # 'bands': ["B4", "B3", "B2"]}, 'TOA') 19 | print(collection.size().getInfo()) 20 | 21 | first = collection.first() 22 | Map.centerObject(first, 10) 23 | Map.addLayer(first, {'min': 0, 'max': 3000, 24 | 'bands': ["B4", "B3", "B2"]}, 'TOA') 25 | -------------------------------------------------------------------------------- /ImageCollection/filtering_collection.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load Landsat 5 data, filter by date and bounds. 5 | collection = ee.ImageCollection('LANDSAT/LT05/C01/T2') \ 6 | .filterDate('1987-01-01', '1990-05-01') \ 7 | .filterBounds(ee.Geometry.Point(25.8544, -18.08874)) 8 | 9 | # Also filter the collection by the IMAGE_QUALITY property. 10 | filtered = collection \ 11 | .filterMetadata('IMAGE_QUALITY', 'equals', 9) 12 | 13 | # Create two composites to check the effect of filtering by IMAGE_QUALITY. 14 | badComposite = ee.Algorithms.Landsat.simpleComposite(collection, 75, 3) 15 | goodComposite = ee.Algorithms.Landsat.simpleComposite(filtered, 75, 3) 16 | 17 | # Display the composites. 18 | Map.setCenter(25.8544, -18.08874, 13) 19 | Map.addLayer(badComposite, 20 | {'bands': ['B3', 'B2', 'B1'], 'gain': 3.5}, 21 | 'bad composite') 22 | Map.addLayer(goodComposite, 23 | {'bands': ['B3', 'B2', 'B1'], 'gain': 3.5}, 24 | 'good composite') 25 | 26 | -------------------------------------------------------------------------------- /ImageCollection/get_image_centroid.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This function returns the image centroid as a new Feature. 5 | 6 | 7 | def getGeom(image): 8 | return ee.Feature(image.geometry().centroid(), {'foo': 1}) 9 | 10 | 11 | # Load a Landsat 8 collection. 12 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1') \ 13 | .filterBounds(ee.Geometry.Point(-122.262, 37.8719)) \ 14 | .filterDate('2014-06-01', '2014-10-01') 15 | 16 | # Map the function over the ImageCollection. 17 | featureCollection = ee.FeatureCollection(collection.map(getGeom)) 18 | 19 | # Map.setCenter(-122.1231, 37.8719, 12) 20 | Map.centerObject(featureCollection, 15) 21 | Map.addLayer(featureCollection, {'color': "yellow"}, "Image centroids") 22 | 23 | # Print the collection. 24 | # print(featureCollection) 25 | -------------------------------------------------------------------------------- /ImageCollection/landsat_filtering.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | collection = ee.ImageCollection('LANDSAT/LC8_L1T_TOA') 5 | 6 | path = collection.filter(ee.Filter.eq('WRS_PATH', 44)) 7 | row = path.filter(ee.Filter.eq('WRS_ROW', 34)) 8 | 9 | images = row.filterDate('2016-01-01', '2016-12-31') 10 | print(images.size().getInfo()) 11 | # images.map(lambda image: image.getInfo()) 12 | 13 | lng = -122.3578 14 | lat = 37.7726 15 | 16 | median = images.median() 17 | Map.setCenter(lng, lat, 12) 18 | vis = {'bands': ['B5', 'B4', 'B3'], 'max': 0.3} 19 | Map.addLayer(median, vis) 20 | -------------------------------------------------------------------------------- /ImageCollection/landsat_simple_composite.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Composite 6 months of Landsat 8. 5 | 6 | # Note that the input to simpleComposite is raw data. 7 | l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1') 8 | 9 | # The asFloat parameter gives floating-point TOA output instead of 10 | # the UINT8 outputs of the default simpleComposite(). 11 | composite = ee.Algorithms.Landsat.simpleComposite(**{ 12 | 'collection': l8.filterDate('2015-1-1', '2015-7-1'), 13 | 'asFloat': True 14 | }) 15 | 16 | # Pick a spot with lots of clouds. 17 | Map.setCenter(-47.6735, -0.6344, 12) 18 | # Display a composite with a band combination chosen from: 19 | # https:#landsat.usgs.gov/how-do-landsat-8-band-combinations-differ-landsat-7-or-landsat-5-satellite-data 20 | Map.addLayer(composite, {'bands': ['B6', 'B5', 'B4'], 'max': [0.3, 0.4, 0.3]}) 21 | -------------------------------------------------------------------------------- /ImageCollection/linear_fit.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Compute the trend of nighttime lights from DMSP. 5 | 6 | # Add a band containing image date as years since 1990. 7 | def createTimeBand(img): 8 | year = img.date().difference(ee.Date('1990-01-01'), 'year') 9 | return ee.Image(year).float().addBands(img) 10 | 11 | # function createTimeBand(img) { 12 | # year = img.date().difference(ee.Date('1990-01-01'), 'year') 13 | # return ee.Image(year).float().addBands(img) 14 | # } 15 | 16 | # Fit a linear trend to the nighttime lights collection. 17 | collection = ee.ImageCollection('NOAA/DMSP-OLS/CALIBRATED_LIGHTS_V4') \ 18 | .select('avg_vis') \ 19 | .map(createTimeBand) 20 | fit = collection.reduce(ee.Reducer.linearFit()) 21 | 22 | # Display a single image 23 | Map.addLayer(ee.Image(collection.select('avg_vis').first()), 24 | {'min': 0, 'max': 63}, 25 | 'stable lights first asset') 26 | 27 | # Display trend in red/blue, brightness in green. 28 | Map.setCenter(30, 45, 4) 29 | Map.addLayer(fit, 30 | {'min': 0, 'max': [0.18, 20, -0.18], 'bands': ['scale', 'offset', 'scale']}, 31 | 'stable lights trend') 32 | -------------------------------------------------------------------------------- /ImageCollection/map_function.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This function adds a band representing the image timestamp. 5 | def addTime(image): 6 | return image.addBands(image.metadata('system:time_start')) 7 | 8 | def conditional(image): 9 | return ee.Algorithms.If(ee.Number(image.get('SUN_ELEVATION')).gt(40), 10 | image, 11 | ee.Image(0)) 12 | 13 | # Load a Landsat 8 collection for a single path-row. 14 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \ 15 | .filter(ee.Filter.eq('WRS_PATH', 44)) \ 16 | .filter(ee.Filter.eq('WRS_ROW', 34)) 17 | 18 | 19 | 20 | # Map the function over the collection and display the result. 21 | print(collection.map(addTime).getInfo()) 22 | 23 | 24 | # Load a Landsat 8 collection for a single path-row. 25 | collection = ee.ImageCollection('LANDSAT/LC8_L1T_TOA') \ 26 | .filter(ee.Filter.eq('WRS_PATH', 44)) \ 27 | .filter(ee.Filter.eq('WRS_ROW', 34)) 28 | 29 | # This function uses a conditional statement to return the image if 30 | # the solar elevation > 40 degrees. Otherwise it returns a zero image. 31 | # conditional = function(image) { 32 | # return ee.Algorithms.If(ee.Number(image.get('SUN_ELEVATION')).gt(40), 33 | # image, 34 | # ee.Image(0)) 35 | # } 36 | 37 | # Map the function over the collection, convert to a List and print the result. 38 | print('Expand this to see the result: ', collection.map(conditional).getInfo()) 39 | 40 | -------------------------------------------------------------------------------- /ImageCollection/metadata.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 ImageCollection for a single path-row. 5 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \ 6 | .filter(ee.Filter.eq('WRS_PATH', 44)) \ 7 | .filter(ee.Filter.eq('WRS_ROW', 34)) \ 8 | .filterDate('2014-03-01', '2014-08-01') 9 | print('Collection: ', collection.getInfo()) 10 | 11 | # Get the number of images. 12 | count = collection.size() 13 | print('Count: ', count.getInfo()) 14 | 15 | # Get the date range of images in the collection. 16 | range = collection.reduceColumns(ee.Reducer.minMax(), ["system:time_start"]) 17 | print('Date range: ', ee.Date(range.get('min')).getInfo(), ee.Date(range.get('max')).getInfo()) 18 | 19 | # Get statistics for a property of the images in the collection. 20 | sunStats = collection.aggregate_stats('SUN_ELEVATION') 21 | print('Sun elevation statistics: ', sunStats.getInfo()) 22 | 23 | # Sort by a cloud cover property, get the least cloudy image. 24 | image = ee.Image(collection.sort('CLOUD_COVER').first()) 25 | print('Least cloudy image: ', image.getInfo()) 26 | 27 | # Limit the collection to the 10 most recent images. 28 | recent = collection.sort('system:time_start', False).limit(10) 29 | print('Recent images: ', recent.getInfo()) 30 | 31 | -------------------------------------------------------------------------------- /ImageCollection/overview.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Create arbitrary constant images. 5 | constant1 = ee.Image(1) 6 | constant2 = ee.Image(2) 7 | 8 | # Create a collection by giving a list to the constructor. 9 | collectionFromConstructor = ee.ImageCollection([constant1, constant2]) 10 | print('collectionFromConstructor: ', collectionFromConstructor.getInfo()) 11 | 12 | # Create a collection with fromImages(). 13 | collectionFromImages = ee.ImageCollection.fromImages( 14 | [ee.Image(3), ee.Image(4)]) 15 | print('collectionFromImages: ', collectionFromImages.getInfo()) 16 | 17 | # Merge two collections. 18 | mergedCollection = collectionFromConstructor.merge(collectionFromImages) 19 | print('mergedCollection: ', mergedCollection.getInfo()) 20 | 21 | # # Create a toy FeatureCollection 22 | # features = ee.FeatureCollection( 23 | # [ee.Feature({}, {'foo': 1}), ee.Feature({}, {'foo': 2})]) 24 | 25 | # # Create an ImageCollection from the FeatureCollection 26 | # # by mapping a function over the FeatureCollection. 27 | # images = features.map(function(feature) { 28 | # return ee.Image(ee.Number(feature.get('foo'))) 29 | # }) 30 | 31 | # # Print the resultant collection. 32 | # print('Image collection: ', images) 33 | 34 | -------------------------------------------------------------------------------- /ImageCollection/select_image_by_index.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/ImageCollection/select_image_by_index.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')\ 7 | .filter(ee.Filter.eq('WRS_PATH', 44))\ 8 | .filter(ee.Filter.eq('WRS_ROW', 34))\ 9 | .filterDate('2014-01-01', '2015-01-01') 10 | 11 | image = ee.Image(collection.toList(collection.size()).get(0)) # select by index from 0 to size-1 12 | print(image.get('system:id').getInfo()) 13 | 14 | Map.setCenter(-122.3578, 37.7726, 12) 15 | Map.addLayer(image, {"bands": ['B4', 'B3', 'B2'], "max": 0.3}, 'median') 16 | -------------------------------------------------------------------------------- /ImageCollection/sort_by_cloud_and_date.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/ImageCollection/sort_by_cloud_and_date.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | # This function masks the input with a threshold on the simple cloud score. 7 | 8 | 9 | def cloudMask(img): 10 | cloudscore = ee.Algorithms.Landsat.simpleCloudScore(img).select('cloud') 11 | return img.updateMask(cloudscore.lt(50)) 12 | 13 | 14 | # Load a Landsat 5 image collection. 15 | collection = ee.ImageCollection('LANDSAT/LT5_L1T_TOA') \ 16 | .filterDate('2008-04-01', '2010-04-01') \ 17 | .filterBounds(ee.Geometry.Point(-122.2627, 37.8735)) \ 18 | .map(cloudMask) \ 19 | .select(['B4', 'B3']) \ 20 | .sort('system:time_start', True) #Sort the collection in chronological order. 21 | 22 | print(collection.size().getInfo()) 23 | 24 | first = collection.first().get('system:id') 25 | print(first.getInfo()) -------------------------------------------------------------------------------- /JavaScripts/ClippedComposite.js: -------------------------------------------------------------------------------- 1 | // Composite an image collection and clip it to a boundary. 2 | 3 | // Load Landsat 7 raw imagery and filter it to April-July 2000. 4 | var collection = ee.ImageCollection('LANDSAT/LE07/C01/T1') 5 | .filterDate('2000-04-01', '2000-07-01'); 6 | 7 | // Reduce the collection by taking the median. 8 | var median = collection.median(); 9 | 10 | // Load a table of state boundaries and filter. 11 | var fc = ee.FeatureCollection('TIGER/2016/States') 12 | .filter(ee.Filter.or( 13 | ee.Filter.eq('NAME', 'Nevada'), 14 | ee.Filter.eq('NAME', 'Arizona'))); 15 | 16 | // Clip to the output image to the Nevada and Arizona state boundaries. 17 | var clipped = median.clipToCollection(fc); 18 | 19 | // Display the result. 20 | Map.setCenter(-110, 40, 5); 21 | var visParams = {bands: ['B3', 'B2', 'B1'], gain: [1.4, 1.4, 1.1]}; 22 | Map.addLayer(clipped, visParams, 'clipped composite'); 23 | -------------------------------------------------------------------------------- /JavaScripts/ClippedComposite.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Composite an image collection and clip it to a boundary. 5 | 6 | # Load Landsat 7 raw imagery and filter it to April-July 2000. 7 | collection = ee.ImageCollection('LANDSAT/LE07/C01/T1') \ 8 | .filterDate('2000-04-01', '2000-07-01') 9 | 10 | # Reduce the collection by taking the median. 11 | median = collection.median() 12 | 13 | # Load a table of state boundaries and filter. 14 | fc = ee.FeatureCollection('TIGER/2016/States') \ 15 | .filter(ee.Filter.Or( 16 | ee.Filter.eq('NAME', 'Nevada'), 17 | ee.Filter.eq('NAME', 'Arizona'))) 18 | 19 | # Clip to the output image to the Nevada and Arizona state boundaries. 20 | clipped = median.clipToCollection(fc) 21 | 22 | # Display the result. 23 | Map.setCenter(-110, 40, 5) 24 | visParams = {'bands': ['B3', 'B2', 'B1'], 'gain': [1.4, 1.4, 1.1]} 25 | Map.addLayer(clipped, visParams, 'clipped composite') 26 | -------------------------------------------------------------------------------- /JavaScripts/FromName.js: -------------------------------------------------------------------------------- 1 | // Display an image given its ID. 2 | 3 | var image = ee.Image('CGIAR/SRTM90_V4'); 4 | // Center the Map. 5 | Map.setCenter(-110, 40, 5); 6 | // Display the image. 7 | Map.addLayer(image, {min: 0, max: 3000}, 'SRTM'); 8 | -------------------------------------------------------------------------------- /JavaScripts/FromName.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Display an image given its ID. 5 | 6 | image = ee.Image('CGIAR/SRTM90_V4') 7 | # Center the Map. 8 | Map.setCenter(-110, 40, 5) 9 | # Display the image. 10 | Map.addLayer(image, {'min': 0, 'max': 3000}, 'SRTM') 11 | -------------------------------------------------------------------------------- /JavaScripts/NormalizedDifference.js: -------------------------------------------------------------------------------- 1 | // NormalizedDifference example. 2 | // 3 | // Compute Normalized Difference Vegetation Index over MOD09GA product. 4 | // NDVI = (NIR - RED) / (NIR + RED), where 5 | // RED is sur_refl_b01, 620-670nm 6 | // NIR is sur_refl_b02, 841-876nm 7 | 8 | // Load a MODIS image. 9 | var img = ee.Image('MODIS/006/MOD09GA/2012_03_09'); 10 | 11 | // Use the normalizedDifference(A, B) to compute (A - B) / (A + B) 12 | var ndvi = img.normalizedDifference(['sur_refl_b02', 'sur_refl_b01']); 13 | 14 | // Make a palette: a list of hex strings. 15 | var palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', 16 | '74A901', '66A000', '529400', '3E8601', '207401', '056201', 17 | '004C00', '023B01', '012E01', '011D01', '011301']; 18 | 19 | // Center the map 20 | Map.setCenter(-94.84497, 39.01918, 8); 21 | 22 | // Display the input image and the NDVI derived from it. 23 | Map.addLayer(img.select(['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03']), 24 | {gain: [0.1, 0.1, 0.1]}, 'MODIS bands 1/4/3'); 25 | Map.addLayer(ndvi, {min: 0, max: 1, palette: palette}, 'NDVI'); 26 | -------------------------------------------------------------------------------- /JavaScripts/NormalizedDifference.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # NormalizedDifference example. 5 | # 6 | # Compute Normalized Difference Vegetation Index over MOD09GA product. 7 | # NDVI = (NIR - RED) / (NIR + RED), where 8 | # RED is sur_refl_b01, 620-670nm 9 | # NIR is sur_refl_b02, 841-876nm 10 | 11 | # Load a MODIS image. 12 | img = ee.Image('MODIS/006/MOD09GA/2012_03_09') 13 | 14 | # Use the normalizedDifference(A, B) to compute (A - B) / (A + B) 15 | ndvi = img.normalizedDifference(['sur_refl_b02', 'sur_refl_b01']) 16 | 17 | # Make a 'palette': a list of hex strings. 18 | palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', 19 | '74A901', '66A000', '529400', '3E8601', '207401', '056201', 20 | '004C00', '023B01', '012E01', '011D01', '011301'] 21 | 22 | # Center the map 23 | Map.setCenter(-94.84497, 39.01918, 8) 24 | 25 | # Display the input image and the NDVI derived from it. 26 | Map.addLayer(img.select(['sur_refl_b01', 'sur_refl_b04', 'sur_refl_b03']), 27 | {'gain': [0.1, 0.1, 0.1]}, 'MODIS bands 1/4/3') 28 | Map.addLayer(ndvi, {'min': 0, 'max': 1, 'palette': palette}, 'NDVI') 29 | -------------------------------------------------------------------------------- /Join/inner_joins.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Make a date filter to get images in this date range. 5 | dateFilter = ee.Filter.date('2014-01-01', '2014-02-01') 6 | 7 | # Load a MODIS collection with EVI data. 8 | mcd43a4 = ee.ImageCollection('MODIS/MCD43A4_006_EVI') \ 9 | .filter(dateFilter) 10 | 11 | # Load a MODIS collection with quality data. 12 | mcd43a2 = ee.ImageCollection('MODIS/006/MCD43A2') \ 13 | .filter(dateFilter) 14 | 15 | # Define an inner join. 16 | innerJoin = ee.Join.inner() 17 | 18 | # Specify an equals filter for image timestamps. 19 | filterTimeEq = ee.Filter.equals(**{ 20 | 'leftField': 'system:time_start', 21 | 'rightField': 'system:time_start' 22 | }) 23 | 24 | # Apply the join. 25 | innerJoinedMODIS = innerJoin.apply(mcd43a4, mcd43a2, filterTimeEq) 26 | 27 | # Display the join result: a FeatureCollection. 28 | print('Inner join output:', innerJoinedMODIS) 29 | 30 | # Map a function to merge the results in the output FeatureCollection. 31 | joinedMODIS = innerJoinedMODIS.map(lambda feature: ee.Image.cat(feature.get('primary'), feature.get('secondary'))) 32 | 33 | # Print the result of merging. 34 | print('Inner join, merged bands:', joinedMODIS.getInfo()) 35 | 36 | -------------------------------------------------------------------------------- /Join/inverted_joins.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 image collection at a point of interest. 5 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \ 6 | .filterBounds(ee.Geometry.Point(-122.09, 37.42)) 7 | 8 | # Define start and end dates with which to filter the collections. 9 | april = '2014-04-01' 10 | may = '2014-05-01' 11 | june = '2014-06-01' 12 | july = '2014-07-01' 13 | 14 | # The primary collection is Landsat images from April to June. 15 | primary = collection.filterDate(april, june) 16 | 17 | # The secondary collection is Landsat images from May to July. 18 | secondary = collection.filterDate(may, july) 19 | 20 | # Use an equals filter to define how the collections match. 21 | filter = ee.Filter.equals(**{ 22 | 'leftField': 'system:index', 23 | 'rightField': 'system:index' 24 | }) 25 | 26 | # Define the join. 27 | invertedJoin = ee.Join.inverted() 28 | 29 | # Apply the join. 30 | invertedJoined = invertedJoin.apply(primary, secondary, filter) 31 | 32 | # Display the result. 33 | print('Inverted join: ', invertedJoined.getInfo()) 34 | 35 | -------------------------------------------------------------------------------- /Join/save_all_joins.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a primary 'collection': Landsat imagery. 5 | primary = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \ 6 | .filterDate('2014-04-01', '2014-06-01') \ 7 | .filterBounds(ee.Geometry.Point(-122.092, 37.42)) 8 | 9 | # Load a secondary 'collection': MODIS imagery. 10 | modSecondary = ee.ImageCollection('MODIS/006/MOD09GA') \ 11 | .filterDate('2014-03-01', '2014-07-01') 12 | 13 | # Define an allowable time difference: two days in milliseconds. 14 | twoDaysMillis = 2 * 24 * 60 * 60 * 1000 15 | 16 | # Create a time filter to define a match as overlapping timestamps. 17 | timeFilter = ee.Filter.Or( 18 | ee.Filter.maxDifference(**{ 19 | 'difference': twoDaysMillis, 20 | 'leftField': 'system:time_start', 21 | 'rightField': 'system:time_end' 22 | }), 23 | ee.Filter.maxDifference(**{ 24 | 'difference': twoDaysMillis, 25 | 'leftField': 'system:time_end', 26 | 'rightField': 'system:time_start' 27 | }) 28 | ) 29 | 30 | # Define the join. 31 | saveAllJoin = ee.Join.saveAll(**{ 32 | 'matchesKey': 'terra', 33 | 'ordering': 'system:time_start', 34 | 'ascending': True 35 | }) 36 | 37 | # Apply the join. 38 | landsatModis = saveAllJoin.apply(primary, modSecondary, timeFilter) 39 | 40 | # Display the result. 41 | print('Join.saveAll:', landsatModis.getInfo()) 42 | 43 | -------------------------------------------------------------------------------- /Join/save_best_joins.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a primary 'collection': Landsat imagery. 5 | primary = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \ 6 | .filterDate('2014-04-01', '2014-06-01') \ 7 | .filterBounds(ee.Geometry.Point(-122.092, 37.42)) 8 | 9 | # Load a secondary 'collection': GRIDMET meteorological data 10 | gridmet = ee.ImageCollection('IDAHO_EPSCOR/GRIDMET') 11 | 12 | # Define a max difference filter to compare timestamps. 13 | maxDiffFilter = ee.Filter.maxDifference(**{ 14 | 'difference': 2 * 24 * 60 * 60 * 1000, 15 | 'leftField': 'system:time_start', 16 | 'rightField': 'system:time_start' 17 | }) 18 | 19 | # Define the join. 20 | saveBestJoin = ee.Join.saveBest(**{ 21 | 'matchKey': 'bestImage', 22 | 'measureKey': 'timeDiff' 23 | }) 24 | 25 | # Apply the join. 26 | landsatMet = saveBestJoin.apply(primary, gridmet, maxDiffFilter) 27 | 28 | # Print the result. 29 | print(landsatMet.getInfo()) 30 | 31 | -------------------------------------------------------------------------------- /Join/simple_joins.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 image collection at a point of interest. 5 | collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \ 6 | .filterBounds(ee.Geometry.Point(-122.09, 37.42)) 7 | 8 | # Define start and end dates with which to filter the collections. 9 | april = '2014-04-01' 10 | may = '2014-05-01' 11 | june = '2014-06-01' 12 | july = '2014-07-01' 13 | 14 | # The primary collection is Landsat images from April to June. 15 | primary = collection.filterDate(april, june) 16 | 17 | # The secondary collection is Landsat images from May to July. 18 | secondary = collection.filterDate(may, july) 19 | 20 | # Use an equals filter to define how the collections match. 21 | filter = ee.Filter.equals(**{ 22 | 'leftField': 'system:index', 23 | 'rightField': 'system:index' 24 | }) 25 | 26 | # Create the join. 27 | simpleJoin = ee.Join.simple() 28 | 29 | # Apply the join. 30 | simpleJoined = simpleJoin.apply(primary, secondary, filter) 31 | 32 | # Display the result. 33 | print('Simple join: ', simpleJoined.getInfo()) 34 | 35 | -------------------------------------------------------------------------------- /Join/spatial_joins.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a primary 'collection': protected areas (Yosemite National Park). 5 | primary = ee.FeatureCollection("WCMC/WDPA/current/polygons") \ 6 | .filter(ee.Filter.eq('NAME', 'Yosemite National Park')) 7 | 8 | # Load a secondary 'collection': power plants. 9 | powerPlants = ee.FeatureCollection('WRI/GPPD/power_plants') 10 | 11 | # Define a spatial filter, with distance 100 km. 12 | distFilter = ee.Filter.withinDistance(**{ 13 | 'distance': 100000, 14 | 'leftField': '.geo', 15 | 'rightField': '.geo', 16 | 'maxError': 10 17 | }) 18 | 19 | # Define a saveAll join. 20 | distSaveAll = ee.Join.saveAll(**{ 21 | 'matchesKey': 'points', 22 | 'measureKey': 'distance' 23 | }) 24 | 25 | # Apply the join. 26 | spatialJoined = distSaveAll.apply(primary, powerPlants, distFilter) 27 | 28 | # Print the result. 29 | # print(spatialJoined.getInfo()) 30 | Map.centerObject(spatialJoined, 10) 31 | Map.addLayer(ee.Image().paint(spatialJoined, 1, 3), {}, 'Spatial Joined') 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019, Qiusheng Wu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /MachineLearning/cart_classifier.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Input imagery is a cloud-free Landsat 8 composite. 5 | l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1') 6 | 7 | image = ee.Algorithms.Landsat.simpleComposite(**{ 8 | 'collection': l8.filterDate('2018-01-01', '2018-12-31'), 9 | 'asFloat': True 10 | }) 11 | 12 | # Use these bands for prediction. 13 | bands = ['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B10', 'B11'] 14 | 15 | # Load training points. The numeric property 'class' stores known labels. 16 | points = ee.FeatureCollection('GOOGLE/EE/DEMOS/demo_landcover_labels') 17 | 18 | # This property of the table stores the land cover labels. 19 | label = 'landcover' 20 | 21 | # Overlay the points on the imagery to get training. 22 | training = image.select(bands).sampleRegions(**{ 23 | 'collection': points, 24 | 'properties': [label], 25 | 'scale': 30 26 | }) 27 | 28 | # Train a CART classifier with default parameters. 29 | trained = ee.Classifier.smileCart().train(training, label, bands) 30 | 31 | # Classify the image with the same bands used for training. 32 | classified = image.select(bands).classify(trained) 33 | 34 | # Display the inputs and the results. 35 | Map.centerObject(points, 11) 36 | Map.addLayer(image, {'bands': ['B4', 'B3', 'B2'], 'max': 0.4}, 'image') 37 | Map.addLayer(classified, 38 | {'min': 0, 'max': 2, 'palette': ['red', 'green', 'blue']}, 39 | 'classification') 40 | 41 | -------------------------------------------------------------------------------- /MachineLearning/clustering.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a pre-computed Landsat composite for input. 5 | input = ee.Image('LANDSAT/LE7_TOA_1YEAR/2001') 6 | 7 | # Define a region in which to generate a sample of the input. 8 | region = ee.Geometry.Rectangle(29.7, 30, 32.5, 31.7) 9 | 10 | # Display the sample region. 11 | Map.setCenter(31.5, 31.0, 8) 12 | Map.addLayer(ee.Image().paint(region, 0, 2), {}, 'region') 13 | 14 | # Make the training dataset. 15 | training = input.sample(**{ 16 | 'region': region, 17 | 'scale': 30, 18 | 'numPixels': 5000 19 | }) 20 | 21 | # Instantiate the clusterer and train it. 22 | clusterer = ee.Clusterer.wekaKMeans(15).train(training) 23 | 24 | # Cluster the input using the trained clusterer. 25 | result = input.cluster(clusterer) 26 | 27 | # Display the clusters with random colors. 28 | Map.addLayer(result.randomVisualizer(), {}, 'clusters') 29 | -------------------------------------------------------------------------------- /NAIP/filter_poly.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | collection = ee.ImageCollection('USDA/NAIP/DOQQ') 5 | 6 | polys = ee.Geometry.Polygon( 7 | [[[-99.29615020751953, 46.725459351792374], 8 | [-99.2116928100586, 46.72404725733022], 9 | [-99.21443939208984, 46.772037733479884], 10 | [-99.30267333984375, 46.77321343419932]]]) 11 | 12 | centroid = polys.centroid() 13 | lng, lat = centroid.getInfo()['coordinates'] 14 | print("lng = {}, lat = {}".format(lng, lat)) 15 | 16 | lng_lat = ee.Geometry.Point(lng, lat) 17 | naip = collection.filterBounds(polys) 18 | naip_2015 = naip.filterDate('2015-01-01', '2015-12-31') 19 | ppr = naip_2015.mosaic() 20 | 21 | count = naip_2015.size().getInfo() 22 | print("Count: ", count) 23 | 24 | # print(naip_2015.size().getInfo()) 25 | # vis = {'bands': ['N', 'R', 'G']} 26 | # Map.setCenter(lng, lat, 12) 27 | # Map.addLayer(ppr,vis) 28 | # Map.addLayer(polys) 29 | 30 | downConfig = {'scale': 30, "maxPixels": 1.0E13, 'driveFolder': 'image'} # scale means resolution. 31 | img_lst = naip_2015.toList(100) 32 | 33 | for i in range(0, count): 34 | image = ee.Image(img_lst.get(i)) 35 | name = image.get('system:index').getInfo() 36 | # print(name) 37 | task = ee.batch.Export.image(image, name, downConfig) 38 | task.start() 39 | 40 | -------------------------------------------------------------------------------- /NAIP/filtering.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | 5 | 6 | collection = ee.ImageCollection('USDA/NAIP/DOQQ') 7 | 8 | 9 | fromFT = ee.FeatureCollection('ft:1CLldB-ULPyULBT2mxoRNv7enckVF0gCQoD2oH7XP') 10 | polys = fromFT.geometry() 11 | centroid = polys.centroid() 12 | lng, lat = centroid.getInfo()['coordinates'] 13 | print("lng = {}, lat = {}".format(lng, lat)) 14 | 15 | # lat = 46.80514 16 | # lng = -99.22023 17 | lng_lat = ee.Geometry.Point(lng, lat) 18 | # naip = collection.filterBounds(lng_lat) 19 | naip = collection.filterBounds(polys) 20 | naip_2015 = naip.filterDate('2015-01-01', '2015-12-31') 21 | ppr = naip_2015.mosaic().clip(polys) 22 | 23 | # print(naip_2015.size().getInfo()) 24 | vis = {'bands': ['N', 'R', 'G']} 25 | Map.setCenter(lng, lat, 10) 26 | # Map.addLayer(naip_2015,vis) 27 | Map.addLayer(ppr,vis) 28 | # Map.addLayer(fromFT) 29 | 30 | # image = ee.Image('USDA/NAIP/DOQQ/m_4609915_sw_14_1_20100629') 31 | # Map.setCenter(lng, lat, 12) 32 | # Map.addLayer(image,vis) 33 | -------------------------------------------------------------------------------- /NAIP/from_name.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | image = ee.Image('USDA/NAIP/DOQQ/m_3712213_sw_10_1_20140613') 5 | Map.setCenter(-122.466123, 37.769833, 17) 6 | Map.addLayer(image, {'bands': ['N', 'R','G']}, 'NAIP') -------------------------------------------------------------------------------- /NAIP/ndwi.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | 5 | 6 | collection = ee.ImageCollection('USDA/NAIP/DOQQ') 7 | fromFT = ee.FeatureCollection('ft:1CLldB-ULPyULBT2mxoRNv7enckVF0gCQoD2oH7XP') 8 | polys = fromFT.geometry() 9 | centroid = polys.centroid() 10 | lng, lat = centroid.getInfo()['coordinates'] 11 | # print("lng = {}, lat = {}".format(lng, lat)) 12 | 13 | # lng_lat = ee.Geometry.Point(lng, lat) 14 | naip = collection.filterBounds(polys) 15 | naip_2015 = naip.filterDate('2015-01-01', '2015-12-31') 16 | ppr = naip_2015.mosaic().clip(polys) 17 | 18 | # print(naip_2015.size().getInfo()) # count = 120 19 | vis = {'bands': ['N', 'R', 'G']} 20 | Map.setCenter(lng, lat, 10) 21 | # Map.addLayer(naip_2015,vis) 22 | Map.addLayer(ppr,vis) 23 | # Map.addLayer(fromFT) 24 | 25 | ndwi = ppr.normalizedDifference(['G', 'N']) 26 | ndwiViz = {'min': 0, 'max': 1, 'palette': ['00FFFF', '0000FF']} 27 | ndwiMasked = ndwi.updateMask(ndwi.gte(0.05)) 28 | ndwi_bin = ndwiMasked.gt(0) 29 | Map.addLayer(ndwiMasked, ndwiViz) 30 | 31 | patch_size = ndwi_bin.connectedPixelCount(256, True) 32 | # Map.addLayer(patch_size) 33 | 34 | patch_id = ndwi_bin.connectedComponents(ee.Kernel.plus(1), 256) 35 | Map.addLayer(patch_id) -------------------------------------------------------------------------------- /Reducer/convert_raster_to_vector.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Japan boundary from the Large Scale International Boundary dataset. 5 | japan = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017') \ 6 | .filter(ee.Filter.eq('country_na', 'Japan')) 7 | 8 | # Load a 2012 nightlights image, clipped to the Japan border. 9 | nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012') \ 10 | .select('stable_lights') \ 11 | .clipToCollection(japan) 12 | 13 | # Define arbitrary thresholds on the 6-bit nightlights image. 14 | zones = nl2012.gt(30).add(nl2012.gt(55)).add(nl2012.gt(62)) 15 | zones = zones.updateMask(zones.neq(0)) 16 | 17 | # Convert the zones of the thresholded nightlights to vectors. 18 | vectors = zones.addBands(nl2012).reduceToVectors(**{ 19 | 'geometry': japan, 20 | 'crs': nl2012.projection(), 21 | 'scale': 1000, 22 | 'geometryType': 'polygon', 23 | 'eightConnected': False, 24 | 'labelProperty': 'zone', 25 | 'reducer': ee.Reducer.mean() 26 | }) 27 | 28 | # Display the thresholds. 29 | Map.setCenter(139.6225, 35.712, 9) 30 | Map.addLayer(zones, {'min': 1, 'max': 3, 'palette': ['0000FF', '00FF00', 'FF0000']}, 'raster') 31 | 32 | # Make a display image for the vectors, add it to the map. 33 | display = ee.Image(0).updateMask(0).paint(vectors, '000000', 3) 34 | Map.addLayer(display, {'palette': '000000'}, 'vectors') 35 | 36 | -------------------------------------------------------------------------------- /Reducer/convert_vector_to_raster.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a collection of US counties. 5 | counties = ee.FeatureCollection('TIGER/2018/Counties') 6 | 7 | # Make an image out of the land area attribute. 8 | landAreaImg = counties \ 9 | .filter(ee.Filter.notNull(['ALAND'])) \ 10 | .reduceToImage(**{ 11 | 'properties': ['ALAND'], 12 | 'reducer': ee.Reducer.first() 13 | }) 14 | 15 | # Display the county land area image. 16 | Map.setCenter(-99.976, 40.38, 5) 17 | Map.addLayer(landAreaImg, { 18 | 'min': 3e8, 19 | 'max': 1.5e10, 20 | 'palette': ['FCFDBF', 'FDAE78', 'EE605E', 'B63679', '711F81', '2C105C'] 21 | }, 'Land Area') 22 | -------------------------------------------------------------------------------- /Reducer/image_reductions.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load an image and select some bands of interest. 5 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318') \ 6 | .select(['B4', 'B3', 'B2']) 7 | 8 | # Reduce the image to get a one-band maximum value image. 9 | maxValue = image.reduce(ee.Reducer.max()) 10 | 11 | # Display the result. 12 | Map.centerObject(image, 10) 13 | Map.addLayer(maxValue, {'max': 13000}, 'Maximum value image') 14 | 15 | -------------------------------------------------------------------------------- /Reducer/linear_regression.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # This function adds a time band to the image. 5 | 6 | def createTimeBand(image): 7 | return image.addBands(image.metadata('system:time_start').divide(1e18)) 8 | 9 | # createTimeBand = function(image) { 10 | # # Scale milliseconds by a large constant to avoid very small slopes 11 | # # in the linear regression output. 12 | # return image.addBands(image.metadata('system:time_start').divide(1e18)) 13 | # } 14 | 15 | # Load the input image 'collection': projected climate data. 16 | collection = ee.ImageCollection('NASA/NEX-DCP30_ENSEMBLE_STATS') \ 17 | .filter(ee.Filter.eq('scenario', 'rcp85')) \ 18 | .filterDate(ee.Date('2006-01-01'), ee.Date('2050-01-01')) \ 19 | .map(createTimeBand) 20 | 21 | # Reduce the collection with the linear fit reducer. 22 | # Independent variable are followed by dependent variables. 23 | linearFit = collection.select(['system:time_start', 'pr_mean']) \ 24 | .reduce(ee.Reducer.linearFit()) 25 | 26 | # Display the results. 27 | Map.setCenter(-100.11, 40.38, 5) 28 | Map.addLayer(linearFit, 29 | {'min': 0, 'max': [-0.9, 8e-5, 1], 'bands': ['scale', 'offset', 'scale']}, 'fit') 30 | 31 | -------------------------------------------------------------------------------- /Reducer/mean_std_image.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318') 6 | 7 | # Combine the mean and standard deviation reducers. 8 | reducers = ee.Reducer.mean().combine(**{ 9 | 'reducer2': ee.Reducer.stdDev(), 10 | 'sharedInputs': True 11 | }) 12 | 13 | # Use the combined reducer to get the mean and SD of the image. 14 | stats = image.reduceRegion(**{ 15 | 'reducer': reducers, 16 | 'bestEffort': True, 17 | }) 18 | 19 | # Display the dictionary of band means and SDs. 20 | print(stats.getInfo()) 21 | 22 | -------------------------------------------------------------------------------- /Reducer/median_reducer.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load an image collection, filtered so it's not too much data. 5 | collection = ee.ImageCollection('LANDSAT/LT05/C01/T1') \ 6 | .filterDate('2008-01-01', '2008-12-31') \ 7 | .filter(ee.Filter.eq('WRS_PATH', 44)) \ 8 | .filter(ee.Filter.eq('WRS_ROW', 34)) 9 | 10 | # Compute the median in each band, each pixel. 11 | # Band names are B1_median, B2_median, etc. 12 | median = collection.reduce(ee.Reducer.median()) 13 | 14 | # The output is an Image. Add it to the map. 15 | vis_param = {'bands': ['B4_median', 'B3_median', 'B2_median'], 'gamma': 1.6} 16 | Map.setCenter(-122.3355, 37.7924, 9) 17 | Map.addLayer(median, vis_param, 'Median') 18 | 19 | -------------------------------------------------------------------------------- /Reducer/min_max_reducer.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load and filter the Sentinel-2 image collection. 5 | collection = ee.ImageCollection('COPERNICUS/S2') \ 6 | .filterDate('2016-01-01', '2016-12-31') \ 7 | .filterBounds(ee.Geometry.Point([-81.31, 29.90])) 8 | 9 | # Reduce the collection. 10 | extrema = collection.reduce(ee.Reducer.minMax()) 11 | # print(extrema.getInfo()) 12 | min_image = extrema.select(0) 13 | max_image = extrema.select(1) 14 | 15 | Map.setCenter(-81.31, 29.90, 10) 16 | Map.addLayer(min_image, {}, 'Min image') 17 | Map.addLayer(max_image, {}, 'Max image') -------------------------------------------------------------------------------- /Reducer/stats_by_group.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a collection of US census blocks. 5 | blocks = ee.FeatureCollection('TIGER/2010/Blocks') 6 | 7 | # Compute sums of the specified properties, grouped by state code. 8 | sums = blocks \ 9 | .filter(ee.Filter.And( 10 | ee.Filter.neq('pop10', {}), 11 | ee.Filter.neq('housing10', {}))) \ 12 | .reduceColumns(**{ 13 | 'selectors': ['pop10', 'housing10', 'statefp10'], 14 | 'reducer': ee.Reducer.sum().repeat(2).group(**{ 15 | 'groupField': 2, 16 | 'groupName': 'state-code', 17 | }) 18 | }) 19 | 20 | # Print the resultant Dictionary. 21 | print(sums.getInfo()) 22 | -------------------------------------------------------------------------------- /Reducer/stats_of_an_image_region.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load input imagery: Landsat 7 5-year composite. 5 | image = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012') 6 | # print(image.getInfo()) 7 | 8 | # Load an input region: Sierra Nevada. 9 | region = ee.Feature(ee.FeatureCollection('EPA/Ecoregions/2013/L3') \ 10 | .filter(ee.Filter.eq('us_l3name', 'Sierra Nevada')) \ 11 | .first()) 12 | 13 | # Reduce the region. The region parameter is the Feature geometry. 14 | meanDictionary = image.reduceRegion(**{ 15 | 'reducer': ee.Reducer.mean(), 16 | 'geometry': region.geometry(), 17 | 'scale': 30, 18 | 'maxPixels': 1e9 19 | }) 20 | 21 | # The result is a Dictionary. Print it. 22 | Map.centerObject(region, 9) 23 | Map.addLayer(image, {'bands': ['B4', 'B3', 'B2']}, 'Landsat-7') 24 | Map.addLayer(ee.Image().paint(region, 1, 3), {'palette': 'green'}, 'Mean Image') 25 | print(meanDictionary.getInfo()) 26 | -------------------------------------------------------------------------------- /Reducer/stats_of_columns.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load US cenus data as a FeatureCollection. 5 | census = ee.FeatureCollection('TIGER/2010/Blocks') 6 | 7 | # Filter the collection to include only Benton County, OR. 8 | benton = census.filter( 9 | ee.Filter.And( 10 | ee.Filter.eq('statefp10', '41'), 11 | ee.Filter.eq('countyfp10', '003') 12 | ) 13 | ) 14 | 15 | # Display Benton County cenus blocks. 16 | Map.setCenter(-123.27, 44.57, 13) 17 | Map.addLayer(ee.Image().paint(benton, 1, 3), {}, 'Benten County, OR') 18 | 19 | # Compute sums of the specified properties. 20 | properties = ['pop10', 'housing10'] 21 | sums = benton \ 22 | .filter(ee.Filter.notNull(properties)) \ 23 | .reduceColumns(**{ 24 | 'reducer': ee.Reducer.sum().repeat(2), 25 | 'selectors': properties 26 | }) 27 | 28 | # Print the resultant Dictionary. 29 | print(sums.getInfo()) 30 | 31 | -------------------------------------------------------------------------------- /Reducer/stats_of_image_neighborhoods.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Define a region in the redwood forest. 5 | redwoods = ee.Geometry.Rectangle(-124.0665, 41.0739, -123.934, 41.2029) 6 | 7 | # Load input NAIP imagery and build a mosaic. 8 | naipCollection = ee.ImageCollection('USDA/NAIP/DOQQ') \ 9 | .filterBounds(redwoods) \ 10 | .filterDate('2012-01-01', '2012-12-31') 11 | naip = naipCollection.mosaic() 12 | 13 | # Compute NDVI from the NAIP imagery. 14 | naipNDVI = naip.normalizedDifference(['N', 'R']) 15 | 16 | # Compute standard deviation (SD) as texture of the NDVI. 17 | texture = naipNDVI.reduceNeighborhood(**{ 18 | 'reducer': ee.Reducer.stdDev(), 19 | 'kernel': ee.Kernel.circle(7), 20 | }) 21 | 22 | # Display the results. 23 | Map.centerObject(ee.FeatureCollection(redwoods), 12) 24 | Map.addLayer(naip, {}, 'NAIP input imagery') 25 | Map.addLayer(naipNDVI, {'min': -1, 'max': 1, 'palette': ['FF0000', '00FF00']}, 'NDVI') 26 | Map.addLayer(texture, {'min': 0, 'max': 0.3}, 'SD of NDVI') 27 | 28 | -------------------------------------------------------------------------------- /Reducer/stats_of_image_regions.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load input imagery: Landsat 7 5-year composite. 5 | image = ee.Image('LANDSAT/LE7_TOA_5YEAR/2008_2012') 6 | 7 | # Load a FeatureCollection of counties in Maine. 8 | maineCounties = ee.FeatureCollection('TIGER/2016/Counties') \ 9 | .filter(ee.Filter.eq('STATEFP', '23')) 10 | 11 | # Add reducer output to the Features in the collection. 12 | maineMeansFeatures = image.reduceRegions(**{ 13 | 'collection': maineCounties, 14 | 'reducer': ee.Reducer.mean(), 15 | 'scale': 30, 16 | }) 17 | 18 | feature = ee.Feature(maineMeansFeatures.first()).select(image.bandNames()) 19 | # print(feature.getInfo()) 20 | properties = feature.propertyNames() 21 | 22 | # Print the first feature, to illustrate the result. 23 | print(feature.toDictionary(properties).getInfo()) 24 | 25 | -------------------------------------------------------------------------------- /Reducer/using_weights.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load an input Landsat 8 image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_186059_20130419') 6 | 7 | # Compute cloud score and reverse it such that the highest 8 | # weight (100) is for the least cloudy pixels. 9 | cloudWeight = ee.Image(100).subtract( 10 | ee.Algorithms.Landsat.simpleCloudScore(image).select(['cloud'])) 11 | 12 | # Compute NDVI and add the cloud weight band. 13 | ndvi = image.normalizedDifference(['B5', 'B4']).addBands(cloudWeight) 14 | 15 | # Define an arbitrary region in a cloudy area. 16 | region = ee.Geometry.Rectangle(9.9069, 0.5981, 10.5, 0.9757) 17 | 18 | # Use a mean reducer. 19 | reducer = ee.Reducer.mean() 20 | 21 | # Compute the unweighted mean. 22 | unweighted = ndvi.select(['nd']).reduceRegion(reducer, region, 30) 23 | 24 | # compute mean weighted by cloudiness. 25 | weighted = ndvi.reduceRegion(reducer.splitWeights(), region, 30) 26 | 27 | # Observe the difference as a result of weighting by cloudiness. 28 | print('unweighted:', unweighted.getInfo()) 29 | print('weighted:', weighted.getInfo()) 30 | 31 | -------------------------------------------------------------------------------- /Reducer/weighted_reductions.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 input image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318') 6 | 7 | # Creat an arbitrary region. 8 | geometry = ee.Geometry.Rectangle(-122.496, 37.532, -121.554, 37.538) 9 | 10 | # Make an NDWI image. It will have one band named 'nd'. 11 | ndwi = image.normalizedDifference(['B3', 'B5']) 12 | 13 | # Compute the weighted mean of the NDWI image clipped to the region. 14 | weighted = ndwi.clip(geometry) \ 15 | .reduceRegion(**{ 16 | 'reducer': ee.Reducer.sum(), 17 | 'geometry': geometry, 18 | 'scale': 30}) \ 19 | .get('nd') 20 | 21 | # Compute the UN-weighted mean of the NDWI image clipped to the region. 22 | unweighted = ndwi.clip(geometry) \ 23 | .reduceRegion(**{ 24 | 'reducer': ee.Reducer.sum().unweighted(), 25 | 'geometry': geometry, 26 | 'scale': 30}) \ 27 | .get('nd') 28 | 29 | # Observe the difference between weighted and unweighted reductions. 30 | print('weighted:', weighted.getInfo()) 31 | print('unweighted', unweighted.getInfo()) 32 | 33 | -------------------------------------------------------------------------------- /Reducer/zonal_statistics.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a region representing the United States 5 | region = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017') \ 6 | .filter(ee.Filter.eq('country_na', 'United States')) 7 | 8 | # Load MODIS land cover categories in 2001. 9 | landcover = ee.Image('MODIS/051/MCD12Q1/2001_01_01') \ 10 | .select('Land_Cover_Type_1') 11 | 12 | # Load nightlights image inputs. 13 | nl2001 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F152001') \ 14 | .select('stable_lights') 15 | nl2012 = ee.Image('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F182012') \ 16 | .select('stable_lights') 17 | 18 | # Compute the nightlights decadal difference, add land cover codes. 19 | nlDiff = nl2012.subtract(nl2001).addBands(landcover) 20 | 21 | # Grouped a mean 'reducer': change of nightlights by land cover category. 22 | means = nlDiff.reduceRegion(**{ 23 | 'reducer': ee.Reducer.mean().group(**{ 24 | 'groupField': 1, 25 | 'groupName': 'code', 26 | }), 27 | 'geometry': region.geometry(), 28 | 'scale': 1000, 29 | 'maxPixels': 1e8 30 | }) 31 | 32 | # Print the resultant Dictionary. 33 | print(means.getInfo()) 34 | -------------------------------------------------------------------------------- /Visualization/color_by_attribute.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/color_by_attribute.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | fc = ee.FeatureCollection('TIGER/2018/States') 7 | 8 | print(fc.first().getInfo()) 9 | 10 | # Use this empty image for paint(). 11 | empty = ee.Image().byte() 12 | palette = ['green', 'yellow', 'orange', 'red'] 13 | 14 | states = empty.paint(**{ 15 | 'featureCollection': fc, 16 | 'color': 'ALAND', 17 | }) 18 | 19 | Map.addLayer(states, {'palette': palette}, 'US States') -------------------------------------------------------------------------------- /Visualization/hillshade.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | import math 4 | 5 | 6 | def Radians(img): 7 | return img.toFloat().multiply(math.pi).divide(180) 8 | 9 | 10 | def Hillshade(az, ze, slope, aspect): 11 | """Compute hillshade for the given illumination az, el.""" 12 | azimuth = Radians(ee.Image(az)) 13 | zenith = Radians(ee.Image(ze)) 14 | # Hillshade = cos(Azimuth - Aspect) * sin(Slope) * sin(Zenith) + 15 | # cos(Zenith) * cos(Slope) 16 | return (azimuth.subtract(aspect).cos() 17 | .multiply(slope.sin()) 18 | .multiply(zenith.sin()) 19 | .add( 20 | zenith.cos().multiply(slope.cos()))) 21 | 22 | terrain = ee.Algorithms.Terrain(ee.Image('srtm90_v4')) 23 | slope_img = Radians(terrain.select('slope')) 24 | aspect_img = Radians(terrain.select('aspect')) 25 | 26 | Map.addLayer(Hillshade(0, 60, slope_img, aspect_img)) 27 | -------------------------------------------------------------------------------- /Visualization/image_clipping.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') 5 | centroid = image.geometry().centroid().coordinates() 6 | lon = centroid.get(0).getInfo() 7 | lat = centroid.get(1).getInfo() 8 | 9 | # Create an NDWI image, define visualization parameters and display. 10 | ndwi = image.normalizedDifference(['B3', 'B5']) 11 | ndwiViz = {'min': 0.5, 'max': 1, 'palette': ['00FFFF', '0000FF']} 12 | 13 | # Mask the non-watery parts of the image, where NDWI < 0.4. 14 | ndwiMasked = ndwi.updateMask(ndwi.gte(0.4)) 15 | 16 | # Create visualization layers. 17 | imageRGB = image.visualize(**{'bands': ['B5', 'B4', 'B3'], 'max': 0.5}) 18 | ndwiRGB = ndwiMasked.visualize(**{ 19 | 'min': 0.5, 20 | 'max': 1, 21 | 'palette': ['00FFFF', '0000FF'] 22 | }) 23 | 24 | # Mosaic the visualization layers and display (or export). 25 | mosaic = ee.ImageCollection([imageRGB, ndwiRGB]).mosaic() 26 | Map.setCenter(lon, lat, 10) 27 | Map.addLayer(mosaic, {}, 'mosaic', False) 28 | 29 | # Create a circle by drawing a 20000 meter buffer around a point. 30 | roi = ee.Geometry.Point([-122.4481, 37.7599]).buffer(20000) 31 | 32 | # Display a clipped version of the mosaic. 33 | Map.addLayer(mosaic.clip(roi), {}, "roi") -------------------------------------------------------------------------------- /Visualization/image_color_palettes.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load an image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') 6 | centroid = image.geometry().centroid().coordinates() 7 | lon = centroid.get(0).getInfo() 8 | lat = centroid.get(1).getInfo() 9 | # print(centroid.getInfo()) 10 | 11 | # Create an NDWI image, define visualization parameters and display. 12 | ndwi = image.normalizedDifference(['B3', 'B5']) 13 | ndwiViz = {'min': 0.5, 'max': 1, 'palette': ['00FFFF', '0000FF']} 14 | 15 | # Mask the non-watery parts of the image, where NDWI < 0.4. 16 | ndwiMasked = ndwi.updateMask(ndwi.gte(0.4)) 17 | 18 | Map.setCenter(lon, lat, 10) 19 | Map.addLayer(ndwi, ndwiViz, 'NDWI', False) 20 | Map.addLayer(ndwiMasked, ndwiViz, 'NDWI masked') 21 | -------------------------------------------------------------------------------- /Visualization/image_mosaicking.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') 5 | centroid = image.geometry().centroid().coordinates() 6 | lon = centroid.get(0).getInfo() 7 | lat = centroid.get(1).getInfo() 8 | 9 | # Create an NDWI image, define visualization parameters and display. 10 | ndwi = image.normalizedDifference(['B3', 'B5']) 11 | ndwiViz = {'min': 0.5, 'max': 1, 'palette': ['00FFFF', '0000FF']} 12 | 13 | # Mask the non-watery parts of the image, where NDWI < 0.4. 14 | ndwiMasked = ndwi.updateMask(ndwi.gte(0.4)) 15 | 16 | # Create visualization layers. 17 | imageRGB = image.visualize(**{'bands': ['B5', 'B4', 'B3'], 'max': 0.5}) 18 | ndwiRGB = ndwiMasked.visualize(**{ 19 | 'min': 0.5, 20 | 'max': 1, 21 | 'palette': ['00FFFF', '0000FF'] 22 | }) 23 | 24 | # Mosaic the visualization layers and display (or export). 25 | mosaic = ee.ImageCollection([imageRGB, ndwiRGB]).mosaic() 26 | Map.setCenter(lon, lat, 10) 27 | Map.addLayer(mosaic, {}, 'mosaic') -------------------------------------------------------------------------------- /Visualization/image_rgb_composite.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load an image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1_TOA/LC08_044034_20140318') 6 | 7 | # Define the visualization parameters. 8 | vizParams = { 9 | 'bands': ['B5', 'B4', 'B3'], 10 | 'min': 0, 11 | 'max': 0.5, 12 | 'gamma': [0.95, 1.1, 1] 13 | } 14 | 15 | # Center the map and display the image. 16 | Map.setCenter(-122.1899, 37.5010, 10) # San Francisco Bay 17 | Map.addLayer(image, vizParams, 'false color composite') -------------------------------------------------------------------------------- /Visualization/image_stretch.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load a Landsat 8 raw image. 5 | image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318') 6 | 7 | # Define a RasterSymbolizer element with '_enhance_' for a placeholder. 8 | template_sld = \ 9 | '' + \ 10 | '<_enhance_/>' + \ 11 | '' + \ 12 | '' + \ 13 | 'B5' + \ 14 | '' + \ 15 | '' + \ 16 | 'B4' + \ 17 | '' + \ 18 | '' + \ 19 | 'B3' + \ 20 | '' + \ 21 | '' + \ 22 | '' 23 | 24 | # Get SLDs with different enhancements. 25 | equalize_sld = template_sld.replace('_enhance_', 'Histogram') 26 | normalize_sld = template_sld.replace('_enhance_', 'Normalize') 27 | 28 | # Display the results. 29 | Map.centerObject(image, 10) 30 | Map.addLayer(image, {'bands': ['B5', 'B4', 'B3'], 'min': 0, 'max': 15000}, 'Linear') 31 | Map.addLayer(image.sldStyle(equalize_sld), {}, 'Equalized') 32 | Map.addLayer(image.sldStyle(normalize_sld), {}, 'Normalized') -------------------------------------------------------------------------------- /Visualization/image_thumbnail.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Fetch a digital elevation model. 5 | image = ee.Image('CGIAR/SRTM90_V4') 6 | 7 | # Request a default thumbnail of the DEM with defined linear stretch. 8 | # Set masked pixels (ocean) to 1000 so they map as gray. 9 | thumbnail1 = image.unmask(1000).getThumbURL({ 10 | 'min': 0, 11 | 'max': 3000, 12 | 'dimensions': 500, 13 | 'region': ee.Geometry.Rectangle([-84.6, -55.9, -32.9, 15.7]), 14 | }) 15 | print('Thumbnail:', thumbnail1) 16 | 17 | # # Specify region by GeoJSON, define palette, set size of the larger aspect dimension. 18 | # thumbnail2 = image.getThumbURL({ 19 | # 'min': 0, 20 | # 'max': 3000, 21 | # 'palette': ['00A600','63C600','E6E600','E9BD3A','ECB176','EFC2B3','F2F2F2'], 22 | # 'dimensions': 500, 23 | # 'region': ee.Geometry.Rectangle([-84.6, -55.9, -32.9, 15.7]), 24 | # }) 25 | # print('GeoJSON region, palette, and max dimension:', thumbnail2) 26 | 27 | # # Specify region by list of points and set display CRS as Web Mercator. 28 | # thumbnail3 = image.getThumbURL({ 29 | # 'min': 0, 30 | # 'max': 3000, 31 | # 'palette': ['00A600','63C600','E6E600','E9BD3A','ECB176','EFC2B3','F2F2F2'], 32 | # 'region': [[-84.6, 15.7], [-84.6, -55.9], [-32.9, -55.9]], 33 | # 'crs': 'EPSG:3857' 34 | # }) 35 | # print('Linear ring region and specified crs', thumbnail3) -------------------------------------------------------------------------------- /Visualization/landsat_symbology.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/landsat_symbology.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | geometry = ee.Geometry.Point([-122.30287513732901, 37.441115780341605]) 7 | 8 | landsat = ee.ImageCollection("LANDSAT/LC08/C01/T1") \ 9 | .filterDate('2016-01-01', '2017-01-01') \ 10 | .filterBounds(geometry) 11 | 12 | composite = ee.Algorithms.Landsat.simpleComposite(**{ 13 | 'collection': landsat, 14 | 'asFloat': True 15 | }) 16 | 17 | rgbVis = {'bands': ["B4", "B3", "B2"], 'min':0, 'max': 0.3} 18 | nirVis = {'bands': ["B5", "B4", "B3"], 'min':0, 'max': [0.5, 0.3, 0.3]} 19 | tempVis = {'bands': ["B10"], 'min': 280, 'max': 310, 'palette': ["blue", "red", "orange", "yellow"]} 20 | 21 | Map.addLayer(composite, rgbVis, "RGB") 22 | Map.addLayer(composite, nirVis, "False Color") 23 | Map.addLayer(composite, tempVis, "Thermal") 24 | 25 | Map.centerObject(ee.FeatureCollection(geometry), 10) 26 | -------------------------------------------------------------------------------- /Visualization/ndvi_symbology.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/ndvi_symbology.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | # This function gets NDVI from Landsat 5 imagery. 7 | 8 | 9 | def getNDVI(image): 10 | return image.normalizedDifference(['B4', 'B3']) 11 | 12 | 13 | image1 = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_19900604') 14 | 15 | # Compute NDVI from the scene. 16 | ndvi1 = getNDVI(image1) 17 | 18 | ndviParams = {'palette': ['#d73027', '#f46d43', '#fdae61', 19 | '#fee08b', '#d9ef8b', '#a6d96a', '#66bd63', '#1a9850']} 20 | 21 | Map.centerObject(image1, 10) 22 | Map.addLayer(ndvi1, ndviParams, 'NDVI') 23 | -------------------------------------------------------------------------------- /Visualization/ndwi_symbology.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/ndvi_symbology.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | # This function gets NDVI from Landsat 5 imagery. 7 | 8 | 9 | def getNDWI(image): 10 | return image.normalizedDifference(['B3', 'B5']) 11 | 12 | 13 | image1 = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_044034_19900604') 14 | 15 | # Compute NDVI from the scene. 16 | ndvi1 = getNDWI(image1) 17 | 18 | ndwiParams = {'palette': ['#ece7f2', '#d0d1e6', '#a6bddb', '#74a9cf', '#3690c0', '#0570b0', '#045a8d', '#023858']} 19 | 20 | Map.centerObject(image1, 10) 21 | Map.addLayer(ndvi1, ndwiParams, 'NDWI') 22 | -------------------------------------------------------------------------------- /Visualization/random_color_visualizer.py: -------------------------------------------------------------------------------- 1 | # GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/random_color_visualizer.py 2 | 3 | import ee 4 | from ee_plugin import Map 5 | 6 | dataset = ee.Image('USGS/NLCD/NLCD2016') 7 | landcover = ee.Image(dataset.select('landcover')) 8 | 9 | Map.setCenter(-95, 38, 5) 10 | Map.addLayer(landcover.randomVisualizer(), {}, 'Landcover') 11 | -------------------------------------------------------------------------------- /Visualization/rendering_categorical_maps.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Load 2012 MODIS land cover and select the IGBP classification. 5 | cover = ee.Image('MODIS/051/MCD12Q1/2012_01_01').select('Land_Cover_Type_1') 6 | 7 | # Define a palette for the 18 distinct land cover classes. 8 | igbpPalette = [ 9 | 'aec3d4', # water 10 | '152106', '225129', '369b47', '30eb5b', '387242', # forest 11 | '6a2325', 'c3aa69', 'b76031', 'd9903d', '91af40', # shrub, grass 12 | '111149', # wetlands 13 | 'cdb33b', # croplands 14 | 'cc0013', # urban 15 | '33280d', # crop mosaic 16 | 'd7cdcc', # snow and ice 17 | 'f7e084', # barren 18 | '6f6f6f' # tundra 19 | ] 20 | 21 | # Specify the min and max labels and the color palette matching the labels. 22 | Map.setCenter(-99.229, 40.413, 5) 23 | Map.addLayer(cover, 24 | {'min': 0, 'max': 17, 'palette': igbpPalette}, 25 | 'IGBP classification') -------------------------------------------------------------------------------- /Visualization/us_counties.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | dataset = ee.FeatureCollection('TIGER/2018/Counties') 5 | visParams = { 6 | 'palette': ['purple', 'blue', 'green', 'yellow', 'orange', 'red'], 7 | 'min': 0, 8 | 'max': 50, 9 | 'opacity': 0.8, 10 | } 11 | 12 | # Turn the strings into numbers 13 | dataset = dataset.map(lambda f: f.set('STATEFP', ee.Number.parse(f.get('STATEFP')))) 14 | 15 | image = ee.Image().float().paint(dataset, 'STATEFP') 16 | countyOutlines = ee.Image().float().paint(**{ 17 | 'featureCollection': dataset, 18 | 'color': 'black', 19 | 'width': 1 20 | }) 21 | 22 | Map.setCenter(-99.844, 37.649, 5) 23 | Map.addLayer(image, visParams, 'TIGER/2018/Counties') 24 | Map.addLayer(countyOutlines, {}, 'county outlines') 25 | # Map.addLayer(dataset, {}, 'for Inspector', False) -------------------------------------------------------------------------------- /Visualization/visualizing_geometries.py: -------------------------------------------------------------------------------- 1 | import ee 2 | from ee_plugin import Map 3 | 4 | # Create a geodesic polygon. 5 | polygon = ee.Geometry.Polygon([ 6 | [[-5, 40], [65, 40], [65, 60], [-5, 60], [-5, 60]] 7 | ]) 8 | 9 | # Create a planar polygon. 10 | planarPolygon = ee.Geometry(polygon, {}, False) 11 | 12 | polygon = ee.FeatureCollection(polygon) 13 | planarPolygon = ee.FeatureCollection(planarPolygon) 14 | 15 | # Display the polygons by adding them to the map. 16 | Map.centerObject(polygon) 17 | Map.addLayer(polygon, {'color': 'FF0000'}, 'geodesic polygon') 18 | Map.addLayer(planarPolygon, {'color': '000000'}, 'planar polygon') 19 | 20 | --------------------------------------------------------------------------------