Visualize plant activity from space using satellite data and Python

Premise

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

Preparation

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/

code

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.

Screen Shot 2019-11-26 at 9.33.42.png

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

Visualize plant activity from space using satellite data and Python
[Python] Random data extraction / combination from DataFrame using random and pandas
From Python to using MeCab (and CaboCha)
[Python] How to read data from CIFAR-10 and CIFAR-100
Data analysis using Python 0
Data cleaning using Python
Visualize network data using Cytoscape from IPython Notebook Part 1
Get files from Linux using paramiko and scp [Python]
Flatten using Python yield from
[Introduction] Artificial satellite data analysis using Python (Google Colab environment)
Scraping desired data from website by linking Python and Excel
Predict gender from name using Gender API and Pykakasi in Python
Graph time series data in Python using pandas and matplotlib
Data analysis using python pandas
Shoot time-lapse from a PC camera using Python and OpenCV
How to get followers and followers from python using the Mastodon API
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Collect product information and process data using Rakuten product search API [Python]
Python: Exclude tags from html data
Data acquisition using python googlemap api
Hit treasure data from Python Pandas
Using Rstan from Python with PypeR
Get data from Quandl in Python
Authentication using tweepy-User authentication and application authentication (Python)
Python, yield, return, and sometimes yield from
Notes on using MeCab from Python
Read and use Python files from Python
About Python, from and import, as
Using Cloud Storage from Python3 (Introduction)
Hashing data in R and Python
Clustering and visualization using Python and CytoScape
Overlay and visualize Geo data and statistical data
Run Ansible from Python using API
Get data from Twitter using Tweepy
Precautions when using phantomjs from python
Access spreadsheets using OAuth 2.0 from Python
Try using Amazon DynamoDB from Python
Easily exchange data between Python, R and Julia using the Feather format
Sensor data acquisition and visualization for plant growth with Intel Edison and Python
Precautions and error handling when calling .NET DLL from python using pythonnet
Make a decision tree from 0 with Python and understand it (4. Data structure)
Data pipeline construction with Python and Luigi
Notes using cChardet and python3-chardet in Python 3.3.1.
[Note] Get data from PostgreSQL with Python
Use PostgreSQL data type (jsonb) from Python
Python: Reading JSON data from web API
Using Python and MeCab with Azure Databricks
Acquisition of plant growth data Acquisition of data from sensors
Python data structure and internal implementation ~ List ~
Get Youtube data in Python using Youtube Data API
Visualize server resources using InfluxDB and Grafana
I tried using UnityCloudBuild API from Python
Python data structure and operation (Python learning memo ③)
Porting and modifying doublet-solver from python2 to python3.
[Python] Web application from 0! Hands-on (4) -Data molding-
[Python] Various data processing using Numpy arrays
Easily graph data in shell and Python
Compress python data and write to sqlite
Collect data using scrapy and populate mongoDB
I'm using tox and Python 3.3 with Travis-CI
Creating Google Spreadsheet using Python / Google Data API