A program that calculates NDVI (Vegetation Index) using a GIS tool called Arcgis The vegetation index is an index that simply shows the amount and activity status of plants by utilizing the characteristics of light reflection of plants. The editor used is Jupyter and the language is Python
Register to use the Arcgis Rest API by referring to the URL below Client ID Client Secret I will use two of them later.
https://developers.arcgis.com/labs/rest/get-an-access-token/
import pandas as pd
import requests
import urllib.request
#Set so that the display in Pandas is not omitted
pd.set_option("max_columns", 100)
pd.set_option('max_rows',1000)
pd.set_option('max_info_columns',100)
First, import the library
ARCGIS_CLIENT_ID = ''
ARCGIS_CLIENT_SECRET = ''
Register CLIENT_ID and CLIENT_SECRET obtained at the time of registration as environment variables
def get_arcgis_token():
response = requests.get('https://www.arcgis.com/sharing/rest/oauth2/token/',
params = {'client_id' : ARCGIS_CLIENT_ID,
'client_secret': ARCGIS_CLIENT_SECRET,
'grant_type': 'client_credentials'})
arcgis_token = response.json()['access_token']
return arcgis_token
Define a function to get access_token Required when accessing the API
field_center =[[120.095415,40.345695]]
satellite_date = '2019/05/20'
arcgis_token = get_arcgis_token()
・ Field_center Coordinates of the land for which you want to measure NDVI It is also possible to pass multiple coordinates
・ Satellite_date When you want data
・ Arcgis_token Token to access the API
#points
#Used when you want to collect satellite wavelength data at multiple points
#Here we use a European satellite called Sentinel
def get_sentinel_sample(arcgis_token):
response = requests.get('https://sentinel.arcgis.com/arcgis/rest/services/Sentinel2/ImageServer/getSamples',
params = urllib.parse.urlencode({'geometryType' : 'esriGeometryMultiPoint',
'geometry': { "points":field_center, 'spatialReference': { 'wkid': 4326}},
'mosaicRule': {'mosaicMethod': 'esriMosaicAttribute',
'where': 'category = 1 AND cloudcover <= 0.10',
'sortField': 'acquisitionDate',
'sortValue': satellite_date,
'ascending': 'true'
},
'token': arcgis_token,
'returnFirstValueOnly': 'true',
'f': 'json' }))
sent_sample = response.json()
return sent_sample
・ "Points": field_center,'spatialReference': {'wkid': 4326}} Coordinates specified earlier There are various coordinate systems when expressing location information, but wkid defines which coordinate system to use. 4326 is a geographic coordinate system and is a coordinate system obtained by GPS.
・'Category = 1 AND cloudcover <= 0.10' Occasionally a gray image, which is a background image, is returned, so specify the category so that only satellite images are returned. Specify cloud cover and specify satellite image with less than 10% cloud
・'SortValue': satellite_date, The date you specified earlier
・ Token': arcgis_token,'returnFirstValueOnly':'true', The token I got earlier Since the API is very heavy, we specify here to return only the first value
See the official documentation for detailed API specifications (quite confusing) https://developers.arcgis.com/rest/services-reference/get-samples.htm
sent_sample = get_sentinel_sample(arcgis_token)
sat_val = sent_sample['samples'][0][('value')]
df_sat_band = pd.DataFrame(columns=['band1','band2','band3','band4','band5','band6','band7','band8','band8a','band9','band10','band11','band12'])
band_val = pd.Series([int(strip_num.strip()) for strip_num in sat_val.split()],index=df_sat_band.columns)
#append is a return value and returns a new instance, so reassign
df_sat_band = df_sat_band.append(band_val, ignore_index=True)
df_sat_band['NDVI'] = (df_sat_band['band8'] - df_sat_band['band4']) /(df_sat_band['band8'] + df_sat_band['band4'])
Format the returned data, make it a pandas data frame type, calculate NDVI and add it to the column In the case of Sentinel, band 4 is the red wavelength region and band 8 is the near infrared wavelength region.
You can calculate the NDVI like this
References https://developers.arcgis.com/rest/services-reference/get-samples.htm https://qiita.com/boiledorange73/items/b98d3d1ef3abf7299aba
Recommended Posts