Skip to content

Commit

Permalink
Add 5 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Dec 28, 2019
1 parent 3d7dcca commit 0e54e71
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 3 deletions.
25 changes: 25 additions & 0 deletions FeatureCollection/clipping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/clipping.py

import ee
from ee_plugin import Map

roi = ee.Geometry.Polygon(
[[[-73.99891354682285, 40.74560250077625],
[-73.99891354682285, 40.74053023068626],
[-73.98749806525547, 40.74053023068626],
[-73.98749806525547, 40.74560250077625]]])


fc = ee.FeatureCollection('TIGER/2016/Roads').filterBounds(roi)

clipped = fc.map(lambda f: f.intersection(roi))

Map.centerObject(ee.FeatureCollection(roi), 17)
Map.addLayer(ee.Image().paint(roi, 0, 2), {'palette': 'yellow'}, 'ROI')
# Map.setCenter(-73.9596, 40.7688, 12)
Map.addLayer(ee.Image().paint(clipped, 0, 3), {'palette': 'red'}, 'clipped')
Map.addLayer(fc, {}, 'Census roads', False)




75 changes: 75 additions & 0 deletions Image/band_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/band_stats.py

import ee
from ee_plugin import Map


# get highest value
def maxValue(img, scale=30):
max_value = img.reduceRegion(**{
'reducer': ee.Reducer.max(),
'geometry': img.geometry(),
'scale': scale,
'maxPixels': 1e9
})
return max_value


# get lowest value
def minValue(img, scale=30):
min_value = img.reduceRegion(**{
'reducer': ee.Reducer.min(),
'geometry': img.geometry(),
'scale': scale,
'maxPixels': 1e9
})
return min_value


# get mean value
def meanValue(img, scale=30):
mean_value = img.reduceRegion(**{
'reducer': ee.Reducer.mean(),
'geometry': img.geometry(),
'scale': scale,
'maxPixels': 1e9
})
return mean_value


# get standard deviation
def stdValue(img, scale=30):
std_value = img.reduceRegion(**{
'reducer': ee.Reducer.stdDev(),
'geometry': img.geometry(),
'scale': scale,
'maxPixels': 1e9
})
return std_value


dataset = ee.Image('USGS/NED')
dem = dataset.select('elevation')
# dem = ee.Image('srtm90_v4')
vis_params = {'min': 0, 'max': 3000}
Map.addLayer(dem, vis_params, 'NED', False)

roi = ee.Geometry.Polygon(
[[[-120.18204899532924, 38.53481618819663],
[-120.18204899532924, 36.54889033300136],
[-116.75431462032924, 36.54889033300136],
[-116.75431462032924, 38.53481618819663]]])

image = dem.clip(roi)
Map.centerObject(image, 9)
Map.addLayer(image, vis_params, 'DEM')

scale = image.projection().nominalScale()
print("Resolution: ", scale.getInfo())

scale = 30

print("Minimum value: ", minValue(image, scale).get('elevation').getInfo())
print("Maximum value: ", maxValue(image, scale).get('elevation').getInfo())
print("Average value: ", meanValue(image, scale).get('elevation').getInfo())
print("Standard deviation: ", stdValue(image, scale).get('elevation').getInfo())
14 changes: 14 additions & 0 deletions Image/image_stats_by_band.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/image_stats_by_band.py

import ee
from ee_plugin import Map

image = ee.Image('USDA/NAIP/DOQQ/m_3712213_sw_10_1_20140613')
Map.setCenter(-122.466123, 37.769833, 17)
Map.addLayer(image, {'bands': ['N', 'R','G']}, 'NAIP')

geometry = image.geometry()

means = image.reduceRegions(geometry, ee.Reducer.mean().forEachBand(image), 10)

print(means.getInfo())
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ The Table of Contents below mimics the structure of the Google Earth Engine [API
* [Cumulative Cost Mapping](https://github.com/giswqs/qgis-earthengine-examples/blob/master/Image/cumulative_cost_mapping.py)
* [Registering Images](https://github.com/giswqs/qgis-earthengine-examples/blob/master/Image/image_displacement.py)
* Miscellaneous
* [Extract value to points](https://github.com/giswqs/qgis-earthengine-examples/blob/master/Image/extract_value_to_points.py) | [Rename bands](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/rename_bands.py) | [Clipping](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/clipping.py) | [Find image by path and row](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/find_image_by_path_row.py) | [Get image resolution](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_image_resolution.py) | [Get image extent](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_image_extent.py) | [Set image properties](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/set_image_properties.py) | [Select bands](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/select_bands.py) | [Convert bands to ImageCollection](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/convert_bands_to_image_collection.py) | [Reclassify](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/reclassify.py) | [Composite bands](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/composite_bands.py) | [Image smoothing](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/image_smoothing.py) | [Download image](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/download.py) | [Cell statistics](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/cell_statistics.py) | [Image patch area](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/image_patch_area.py) | [Get image id](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_image_id.py) | [Get band name and type](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_band_name_and_type.py) | [Filtering by calendar range](https://github.com/giswqs/qgis-earthengine-examples/tree/master/ImageCollection/filtering_by_calendar_range.py)
* [Band statistics (min, max, mean, std)](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/band_stats.py) | [Image statistics by band](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/image_stats_by_band.py) | [Extract value to points](https://github.com/giswqs/qgis-earthengine-examples/blob/master/Image/extract_value_to_points.py) | [Rename bands](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/rename_bands.py) | [Clipping](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/clipping.py) | [Find image by path and row](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/find_image_by_path_row.py) | [Get image resolution](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_image_resolution.py) | [Get image extent](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_image_extent.py) | [Set image properties](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/set_image_properties.py) | [Select bands](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/select_bands.py) | [Convert bands to ImageCollection](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/convert_bands_to_image_collection.py) | [Reclassify](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/reclassify.py) | [Composite bands](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/composite_bands.py) | [Image smoothing](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/image_smoothing.py) | [Download image](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/download.py) | [Cell statistics](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/cell_statistics.py) | [Image patch area](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/image_patch_area.py) | [Get image id](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_image_id.py) | [Get band name and type](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Image/get_band_name_and_type.py) | [Filtering by calendar range](https://github.com/giswqs/qgis-earthengine-examples/tree/master/ImageCollection/filtering_by_calendar_range.py)

### [ImageCollection](https://github.com/giswqs/qgis-earthengine-examples/tree/master/ImageCollection)

Expand Down Expand Up @@ -92,7 +92,7 @@ The Table of Contents below mimics the structure of the Google Earth Engine [API
* [Reducing a FeatureCollection](https://github.com/giswqs/qgis-earthengine-examples/blob/master/FeatureCollection/reducing_feature_collection.py)
* [Vector to Raster Interpolation](https://github.com/giswqs/qgis-earthengine-examples/blob/master/FeatureCollection/idw_interpolation.py)
* Miscellaneous
* [Add new attribute](https://github.com/giswqs/qgis-earthengine-examples/blob/master/FeatureCollection/add_new_attribute.py) | [Add area column](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/add_area_column.py) | [Add random value column](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/add_random_value_column.py) | [Single column statistics](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/column_statistics.py) | [Multiple column statistics](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/column_statistics_multiple.py) | [Simplify polygons](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/simplify_polygons.py) | [Column statistics by group](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/column_statistics_by_group.py) | [Select by location](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_by_location.py) | [Select by attributes](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_by_attributes.py) | [Select by strings](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_by_strings.py) | [Vector symbology](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/vector_symbology.py) | [Merge FeatureCollection](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/merge_feature_collections.py) | [Search by buffer distance](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/search_by_buffer_distance.py) | [Select columns](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_columns.py) | [Mimimum bounding geometry](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/minimum_bounding_geometry.py)
* [Add new attribute](https://github.com/giswqs/qgis-earthengine-examples/blob/master/FeatureCollection/add_new_attribute.py) | [Add area column](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/add_area_column.py) | [Add random value column](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/add_random_value_column.py) | [Single column statistics](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/column_statistics.py) | [Multiple column statistics](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/column_statistics_multiple.py) | [Simplify polygons](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/simplify_polygons.py) | [Column statistics by group](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/column_statistics_by_group.py) | [Select by location](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_by_location.py) | [Select by attributes](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_by_attributes.py) | [Select by strings](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_by_strings.py) | [Vector symbology](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/vector_symbology.py) | [Merge FeatureCollection](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/merge_feature_collections.py) | [Search by buffer distance](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/search_by_buffer_distance.py) | [Select columns](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/select_columns.py) | [Mimimum bounding geometry](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/minimum_bounding_geometry.py) | [Clipping polygons](https://github.com/giswqs/qgis-earthengine-examples/tree/master/FeatureCollection/clipping.py)

### [Reducer](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Reducer)

Expand Down Expand Up @@ -179,7 +179,7 @@ The Table of Contents below mimics the structure of the Google Earth Engine [API
* [NLCD Land Cover](https://github.com/giswqs/qgis-earthengine-examples/blob/master/Visualization/nlcd_land_cover.py)
* [US Counties](https://github.com/giswqs/qgis-earthengine-examples/blob/master/Visualization/us_counties.py)
* Miscellaneous
* [NDVI symbology](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/ndvi_symbology.py) | [NDWI symbology](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/ndvi_symbology.py) | [Landsat symbology](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/landsat_symbology.py) | [Color by attribute](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/color_by_attribute.py)
* [NDVI symbology](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/ndvi_symbology.py) | [NDWI symbology](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/ndvi_symbology.py) | [Landsat symbology](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/landsat_symbology.py) | [NWI wetlands symbology](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/nwi_wetlands_symbology.py) | [Color by attribute](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/color_by_attribute.py) | [Random color visualizer](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/random_color_visualizer.py)

### [Datasets](https://github.com/giswqs/qgis-earthengine-examples/tree/master/Datasets)

Expand Down
45 changes: 45 additions & 0 deletions Visualization/nwi_wetlands_symbology.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/nwi_wetlands_symbology.py

import ee
from ee_plugin import Map

# NWI legend: https://www.fws.gov/wetlands/Data/Mapper-Wetlands-Legend.html
def nwi_add_color(fc):
emergent = ee.FeatureCollection(
fc.filter(ee.Filter.eq('WETLAND_TY', 'Freshwater Emergent Wetland')))
emergent = emergent.map(lambda f: f.set(
'R', 127).set('G', 195).set('B', 28))
# print(emergent.first())

forested = fc.filter(ee.Filter.eq(
'WETLAND_TY', 'Freshwater Forested/Shrub Wetland'))
forested = forested.map(lambda f: f.set('R', 0).set('G', 136).set('B', 55))

pond = fc.filter(ee.Filter.eq('WETLAND_TY', 'Freshwater Pond'))
pond = pond.map(lambda f: f.set('R', 104).set('G', 140).set('B', 192))

lake = fc.filter(ee.Filter.eq('WETLAND_TY', 'Lake'))
lake = lake.map(lambda f: f.set('R', 19).set('G', 0).set('B', 124))

riverine = fc.filter(ee.Filter.eq('WETLAND_TY', 'Riverine'))
riverine = riverine.map(lambda f: f.set(
'R', 1).set('G', 144).set('B', 191))

fc = ee.FeatureCollection(emergent.merge(
forested).merge(pond).merge(lake).merge(riverine))

base = ee.Image(0).mask(0).toInt8()
img = base.paint(fc, 'R') \
.addBands(base.paint(fc, 'G')
.addBands(base.paint(fc, 'B')))
return img


fromFT = ee.FeatureCollection("users/wqs/Pipestem/Pipestem_HUC10")
Map.addLayer(ee.Image().paint(fromFT, 0, 2), {}, 'Watershed')
huc8_id = '10160002'
nwi_asset_path = 'users/wqs/NWI-HU8/HU8_' + huc8_id + '_Wetlands' # NWI wetlands for the clicked watershed
clicked_nwi_huc = ee.FeatureCollection(nwi_asset_path)
nwi_color = nwi_add_color(clicked_nwi_huc)
Map.centerObject(clicked_nwi_huc, 10)
Map.addLayer(nwi_color, {'gamma': 0.3, 'opacity': 0.7}, 'NWI Wetlands Color')
10 changes: 10 additions & 0 deletions Visualization/random_color_visualizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# GitHub URL: https://github.com/giswqs/qgis-earthengine-examples/tree/master/Visualization/random_color_visualizer.py

import ee
from ee_plugin import Map

dataset = ee.Image('USGS/NLCD/NLCD2016')
landcover = ee.Image(dataset.select('landcover'))

Map.setCenter(-95, 38, 5)
Map.addLayer(landcover.randomVisualizer(), {}, 'Landcover')

0 comments on commit 0e54e71

Please sign in to comment.