Skip to content

Instantly share code, notes, and snippets.

View girija2204's full-sized avatar
🏠
Working from home

Girija Shankar Behera girija2204

🏠
Working from home
View GitHub Profile
@girija2204
girija2204 / landsat_data.py
Last active September 1, 2020 04:24
Accessing landsat data
gis = GIS()
# Landsat data - https://www.arcgis.com/home/item.html?id=d9b466d6a9e647ce8d1dd5fe12eb434b
landsat_data = gis.content.get("d9b466d6a9e647ce8d1dd5fe12eb434b") # id from the URL
landsat_data.layers # [<ImageryLayer url:"https://landsat2.arcgis.com/arcgis/rest/services/Landsat/MS/ImageServer">]
landsat_data.type # 'Image Service'
HTML(landsat_data.description) # prints the description of the landsat data
# printing all the properties available on the landsat imagery layer - a total of 55 properties will be printed
@girija2204
girija2204 / printing_rasterFunctions.py
Created August 25, 2020 19:05
printing raster functions
llayer = landsat_data.layers[0]
for rasterFunction in llayer.properties['rasterFunctionInfos']:
print(f"{rasterFunction.get('name')}")
# Agriculture with DRA
# Bathymetric with DRA
# Color Infrared with DRA
# Natural Color with DRA
# Short-wave Infrared with DRA
@girija2204
girija2204 / applying_ndvi.py
Last active August 27, 2020 04:26
applying NDVI Colorized raster function on imagery and clipping it to visualize the interested region
llayer_colorized = apply(llayer, "NDVI Colorized") # applying NDVI Colorized on the imagery
delhi_clipped = clip(llayer_colorized, delhi_geom)
map = gis.map()
map.add_layer(delhi_clipped)
map.export_to_html(f'xsd.html')
delhi_image = llayer.filter_by("OBJECTID=2752790") # 2017-10-15 - imagery of single day selected over delhi region
delhi_image_colorized = apply(delhi_image, "NDVI Colorized")
delhi_clipped = clip(delhi_image_colorized, delhi_geom)
map = gis.map()
@girija2204
girija2204 / delhi_boundaries.py
Last active September 1, 2020 18:02
accessing delhi boundaries from a feature dataset
gis = GIS("gis developer profile details")
boundaries = gis.content.get("3013e4533dc64a848159cafe7bc2de84")
state_boundaries = boundaries.layers[1]
state_boundaries.query().sdf # prints the dataframe
# OBJECTID ID NAME AREA TOTPOP_CY ISO_CODE ISO_SUB ISO2_CC ISO3_CC Shape__Area Shape__Length SHAPE
# 0 1 01 Jammu & Kashmir 106337.915332 14150564 INJK JK IN IND 10.355640 24.807143 {"rings": [[[76.553503226, 35.927019016], [76....
# 1 2 02 Himachal Pradesh 55388.071495 7675650 INHP HP IN IND 5.282314 15.582886 {"rings": [[[76.8309960000001, 33.250837], [76...
# 2 3 03 Punjab 50563.578593 31014086 INPB PB IN IND 4.767880 16.249509 {"rings": [[[75.838508, 32.504755], [75.846393...
# 3 4 04 Chandigarh 114.429678 1182432 INCH CH IN IND 0.010776 0.583075 {"rings": [[[76.7713440000001, 30.795139000000...
delhi_ndviraw = apply(delhi_image,"NDVI Raw")
delhi_clip = clip(delhi_ndviraw, delhi_geom)
# 0.4-0.5 is mapped to 1, and 0.5-1 is mapped to 2
delhi_clip_remapped = remap(delhi_clip,
input_ranges=[0.4, 0.5, 0.5, 1],
output_values=[1, 2])
# 1 is mapped to (124, 252, 0), and 2 is mapped to (0, 102, 0)
delhi_clip_colormapped = colormap(delhi_clip_remapped,colormap=[[1, 124, 252, 0], [2, 0, 102, 0]], astype='u8')
@girija2204
girija2204 / visualizing_different_rasters.py
Last active August 29, 2020 04:49
Rasters functions are applied on the imagery layer
# apply(llayer, 'NDVI Raw')
apply(llayer, 'NDVI Colorized')
apply(llayer, 'Agriculture with DRA')
# apply(llayer, 'Color Infrared with DRA')
apply(llayer, 'Normalized Difference Moisture Index Colorized')
apply(llayer, 'Natural Color with DRA')
@girija2204
girija2204 / natural_color_raster.py
Created August 30, 2020 15:18
Querying the imagery and selecting the imagery on a single day, and applying raster functions on that imagery, and then clipping it for Delhi area
selected = llayer.filter_by(where="(CloudCover <= 0.05)",
time=[datetime(2015, 1, 1),datetime(2017, 12, 31)],
geometry=arcgis.geometry.filters.intersects(area['extent']))
df = selected.query(out_fields="AcquisitionDate, GroupName, CloudCover, DayOfYear",
order_by_fields="AcquisitionDate").sdf
df['AcquisitionDate'] = pd.to_datetime(df['AcquisitionDate'], unit='ms')
df
# prints a dataframe consisting of the fields selected, ordered by Acquisition Date
delhi_image = llayer.filter_by("OBJECTID=2752790") # Extracting the imagery on 2017-10-15 - a date randomly selected from above dataframe
@girija2204
girija2204 / geojson_for_shapes.py
Created September 28, 2020 03:28
Getting the coordinates in json format for a district in a shapefile
import geojson
x = geojson.Polygon(coordinates=geometry[0]['coordinates'])
final_products = self.api.query(footprint,
date=(products_df_sorted['beginposition'][0].date()-datetime.timedelta(days=4),products_df_sorted['beginposition'][0].date()+datetime.timedelta(days=5)),
area_relation='Intersects',
platformname='Sentinel-2')
final_products_ids_list = list(final_products)
from sentinelsat.sentinel import SentinelAPI, read_geojson, geojson_to_wkt
# geojson_file consists of the manual coordinates around the district Birbhum, not the original coordinates,
# as with the original coordinates the query becomes huge, and the API throws error. Then it filters the imageries,
# around this location in geojson between the 6months from Jan to June 2020 and gives all images which falls within this range.
api = SentinelAPI('user', 'password')
footprint = geojson_to_wkt(read_geojson(geojson_file))
products = self.api.query(footprint,
date=(date(2020,1,1), date(2020,6,30)),
area_relation='Intersects',