0% found this document useful (0 votes)
14 views

Grupo 1 Accessing Satellite Imagery Using Python

Uploaded by

ricardocheke23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Grupo 1 Accessing Satellite Imagery Using Python

Uploaded by

ricardocheke23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Accessing Satellite Imagery Using Python | by LAW... https://medium.com/@kimutai.lawrence19/accessing-s...

Open in app

Search

Get unlimited access to the best of Medium for less than $1/week. Become a member

Accessing Satellite Imagery Using Python


LAWRENCE KIMUTAI · Follow
4 min read · May 6, 2022

Listen Share More

As a Geospatial Analyst, you may want to access and analyze satellite imagery
using python and carry out your analysis from there. This article is meant to give
you an understanding of how you can do that simply using Python. This may be
carried out in either Jupyter Notebooks or Google Colab.

NB: I have provided a link to the Github repository at the end of this article from

1 de 14 6/06/24, 17:38
Accessing Satellite Imagery Using Python | by LAW... https://medium.com/@kimutai.lawrence19/accessing-s...

where you can access the code. You can run this code on Google Colab as we go
along.

Here, I will use an example of accessing Sentinel 2 Imagery using Python.

Accessing data: We will use Sentinel 2 data. There are many options to access
Sentinel 2 images and most of them will require you to access through website
interaction whether directly via a downloading service utility or via the cloud.
However, since we are using Jupyter notebook, we will access them right here
using, sentinelsat a python library which makes searching, retrieving and
downloading Sentinel satellite images easy. So let us start installing sentinelsat
through pip. We also install other packages that we will use as we continue.

!pip install sentinelsat

!pip install folium

!pip install descartes

!pip install rasterio

!pip install wget

!pip install geopandas

!pip install pandas

import folium

import os

import numpy as np

from sentinelsat import SentinelAPI, read_geojson, geojson_to_wkt

import geopandas as gpd

import pandas as pd

import numpy as np

import rasterio

import matplotlib.pyplot as plt

from shapely.geometry import MultiPolygon, Polygon

import fiona

import wget

2 de 14 6/06/24, 17:38
Accessing Satellite Imagery Using Python | by LAW... https://medium.com/@kimutai.lawrence19/accessing-s...

Before we are able to use sentinelsat, we need to register a username in


Copernicus Open Access Hub and note down your username and password and
paste them here inside the code.

from sentinelsat import SentinelAPI

user = '<your username>'

password = '<your password>'

api = SentinelAPI(user, password, 'https://scihub.copernicus.eu/


dhus')

You are set to use sentinelsat and download Sentinel Satellite images.We then use
define the study area in this article we are going to use boundary data from Lagos
City, Nigeria.(I uploaded the boundary for Lagos city,Nigeria here on google
colab) . We thus access the boundary data(Lagos shapefile) via the Lagos
shapefile path and we will read it with Geopandas and visualize it with Folium
python library.

print("Installing geopandas...")

# We need to install geopandas and descartes using PIP because


they are

# not installed on Jupyter by default.

!pip install geopandas

!pip install descartes

import geopandas as gpd

# Next, print out the shapefile.

# It won't look like much because we are looking at geographic


coordinates.

print("Loading Shapefile...")

# If using your files, replace below filename ("/content/


Lagosnigeria.shp") with the

# shapefile filename you uploaded.

shapefile = gpd.read_file("/content/Lagosnigeria.shp")

# The "head" function prints out the first five rows in full, so

3 de 14 6/06/24, 17:38
Accessing Satellite Imagery Using Python | by LAW... https://medium.com/@kimutai.lawrence19/accessing-s...

you can see

# the columns in the data set too!

shapefile.head()

The shapefile is read and displayed as follows:

Lagos Shapefile.

One last step before we can search and download sentinel 2 images is to create a
footprint from the Lagos Shapefile Geometry. Here we will use Shapely Python
library since our data is in Shapefiles and have read it already as Geopandas
GeodataFrame. (Note that if you have Geojson data, sentinelsatprovides a handy
way to convert your data into a proper format in the query).

from shapely.geometry import MultiPolygon, Polygon

footprint = None

for i in shapefile['geometry']:

footprint = i

Now we can run a query on the api we have created above. There are different
ways you can construct your query here depending on your use case. In this
example, we will create a query for Sentinel 2 images Level 2A with cloud
coverage between 0 and 10 that fall or intersect with the footprint (Area of
study:”in our case Lagos City”). For the time period, we are interested only in
Sentinel Level 2A satellite images taken between ‘20220101’ and ‘20220120’ (For
reference on valid search queries please refer to scihub).

products = api.query(footprint,

date = ('20190101', '20190110'),

4 de 14 6/06/24, 17:38
Accessing Satellite Imagery Using Python | by LAW... https://medium.com/@kimutai.lawrence19/accessing-s...

platformname = 'Sentinel-2',

processinglevel = 'Level-2A',

cloudcoverpercentage = (0,10)

Sorting the Sentinel 2 data

products_gdf = api.to_geodataframe(products)

products_gdf_sorted =
products_gdf.sort_values(['cloudcoverpercentage'],
ascending=[True])

products_gdf_sorted

The results/output is as follows:

Available images fetched depending on the query.

From the results that are acquired above, you can now download your Sentinel 2
data (specific) from the list of outputs.

api.download("ab40b37d-d1ae-4592-bb01-3ed0adf2527a")

5 de 14 6/06/24, 17:38
Accessing Satellite Imagery Using Python | by LAW... https://medium.com/@kimutai.lawrence19/accessing-s...

Sentinel Imagery Download.

You can conduct analysis from the Satellite Imagery you have just accessed!

Link to the Github Repository: https://github.com/KimutaiLawrence/Geospatial-


Data-Science

There’s also a Youtube video(Playlist: Geospatial Data Science) explaining step by


step what goes on. Don’t forget to Like, Share, Subcribe and turn on notifications
to the Youtube Channel(Geospatial Primetech) so that you may get up-to-date
with Geospatial Data Science: https://youtube.com/channel/
UCisEE1cl7HB9y8aFRD7z0Og

Geospatial Data Science Satellite Imagery Python Sentinel Data Science

Follow

Written by LAWRENCE KIMUTAI


286 Followers

Geospatial Data Science, Python, Java, AI, ML, GIS. Email: [email protected] and my Portfolio
Website: https://lawrence65.carrd.co/com +254759629059

More from LAWRENCE KIMUTAI

6 de 14 6/06/24, 17:38

You might also like