Solved

What is the preferred way to work with Planet data in Python?

  • 21 April 2023
  • 3 replies
  • 220 views

Badge +2

Hi.
I am new to Planet data and I am doing some exploratory work and research using Python. Usually I use libraries such as rasterio to manipulate raster data. However, it seems that with the way Planet data is delivered (I am using PSScene 1B data), it is hard to read in the raster with the metadata directly. I find myself trying to parse the relevant information from the XML metadata file manually.

Is there a recommended way to solve this issue, such that I don’t have to write my own parsing functions?

Any tips on working with Planet data in python in general would be much appreciated.

icon

Best answer by Miguel Castro Gomez 27 April 2023, 11:47

View original

3 replies

Badge +4

Hi Fergus,

To better understand your situation, how are you getting the Planet data? Via Explorer or APIs (Data or Orders)?

Once you have your Planet data as a raster file in your local system you can read it with rasterio with:

# Import module
import rasterio

# Open raster file
img = rasterio.open(“path_to_raster”)

Having the raster as a `rasterio object` allows you to explore some basic metadata with:

# Get metadata
img_mtd = img_obj.meta
print(img_mtd)

You can access other relevant metadata using rasterio builtin functionalities with:

# Get raster resolution
img_res = img_obj.res
print(img_res)

# Get product bounding box
img_bbx = img_obj.bounds
print(img_bbx)

If you want to access additional metadata for a given product, you will have to use the metadata files available for download.

If you are using Planet’s APIs to search and download your products, you can get plenty of metadata about a product from the API response. Can you share more of your workflow to guide you on that?

 

Additionally, have a look to our GitHub repo (also here) where you can find plenty of Notebooks on how to read and process Planet data with Python

Badge +2

Hi Miguel,

Thanks for your reply. For the time being I am just downloading some sample images with the Planet Explorer in order to test the rest of the pipeline. Specifically I downloaded data of Planet bundle_type “basic_analytic_udm2” and item_id “20201124_031355_67_1064” and “20201126_031738_79_105d”.

I know that these are PLANETSCOPE BASIC ANALYTIC SCENE PRODUCTS, so they are not cartographically projected. However, in the metadata.xml the image corner coordinates are given in EPSG:4326, which would allow an approximate projection at least. When I open the tif files in QGIS it automatically recognizes the cordinate reference system and displays the images correctly. In rasterio no crs is recognized at all.

The end goal of all of this is to use these images for stereo analysis and test whether or not we can reconstruct a local DSM using the Planet data. That is why I am using the Basic Analytic Scene Product, because we need the RPC values. 

Currently I am trying to parse the relevant metadata from the xml file manually, in order to reconstruct the cartographic projection using GDAL. However, it’s a bit tedious. Seeing as QGIS is able to read the metadata automatically, I was hoping maybe you know of some way to do the same in Python.

Best,

Fergus 

Badge +4

Hi Fergus,

QGIS may by default read additional metadata from the xml files but if you want to accomplish that in Python you will have to code it. The advantage would be that, as Planet data follows a standard structure, you could write a function an get that information for any given product. 

If you choose to get the data with the Data API for example, before activating and downloading a product you get information (see below) where it would be easier to extract metadata (provided as a dict).

{'_links': {'_self': 'https://api.planet.com/data/v1/item-types/PSScene/items/20230225_063225_94_2475',
'assets': 'https://api.planet.com/data/v1/item-types/PSScene/items/20230225_063225_94_2475/assets/',
'thumbnail': 'https://tiles.planet.com/data/v1/item-types/PSScene/items/20230225_063225_94_2475/thumb'},
'_permissions': ['assets.basic_analytic_4b:download',
'assets.basic_analytic_4b_rpc:download',
'assets.basic_analytic_4b_xml:download',
'assets.basic_analytic_8b:download',
'assets.basic_analytic_8b_xml:download',
'assets.basic_udm2:download',
'assets.ortho_analytic_4b:download',
'assets.ortho_analytic_4b_sr:download',
'assets.ortho_analytic_4b_xml:download',
'assets.ortho_analytic_8b:download',
'assets.ortho_analytic_8b_sr:download',
'assets.ortho_analytic_8b_xml:download',
'assets.ortho_udm2:download',
'assets.ortho_visual:download'],
'assets': ['basic_analytic_4b',
'basic_analytic_4b_rpc',
'basic_analytic_4b_xml',
'basic_analytic_8b',
'basic_analytic_8b_xml',
'basic_udm2',
'ortho_analytic_4b',
'ortho_analytic_4b_sr',
'ortho_analytic_4b_xml',
'ortho_analytic_8b',
'ortho_analytic_8b_sr',
'ortho_analytic_8b_xml',
'ortho_udm2',
'ortho_visual'],
'geometry': {'coordinates': [[[55.11886521973737, 25.15859199695998],
[55.07899425546471, 24.97703764034052],
[55.4126054348449, 24.916315069779486],
[55.45289067296136, 25.096670823823267],
[55.11886521973737, 25.15859199695998]]],
'type': 'Polygon'},
'id': '20230225_063225_94_2475',
'properties': {'acquired': '2023-02-25T06:32:25.946409Z',
'anomalous_pixels': 0,
'clear_confidence_percent': 92,
'clear_percent': 100,
'cloud_cover': 0,
'cloud_percent': 0,
'ground_control': True,
'gsd': 3.9,
'heavy_haze_percent': 0,
'instrument': 'PSB.SD',
'item_type': 'PSScene',
'light_haze_percent': 0,
'pixel_resolution': 3,
'provider': 'planetscope',
'published': '2023-02-25T10:11:15Z',
'publishing_stage': 'finalized',
'quality_category': 'standard',
'satellite_azimuth': 99.7,
'satellite_id': '2475',
'shadow_percent': 0,
'snow_ice_percent': 0,
'strip_id': '6312017',
'sun_azimuth': 136.2,
'sun_elevation': 45.4,
'updated': '2023-02-27T02:43:59Z',
'view_angle': 4.5,
'visible_confidence_percent': 73,
'visible_percent': 100},
'type': 'Feature'}
Miguel

 

 

 

Reply