Get the geographical coordinates from NetCDF file using Python

EmailTwitterLinkedInFacebookWhatsAppShare
Raster Coordinates

Introduction and Background

Geographical information systems (GIS) often involve working with spatial data, including raster files that represent data on a grid. NetCDF (Network Common Data Form) is a popular format for storing multidimensional scientific data, including raster datasets. In this tutorial, we’ll explore how to extract geographical coordinates from a NetCDF raster file using Python.

Following dataset is used in the tutorial:
NetCDF dataset

1. NetCDF Format

NetCDF is a self-describing, machine-independent data format for representing scientific data. It is commonly used for climate and weather data, satellite imagery, and various other geospatial datasets. NetCDF files can store multidimensional data, making them well-suited for applications where data has dimensions such as time, latitude, and longitude.

2. Python Libraries Used

  • xarray: Xarray is a powerful library designed for working with labeled multidimensional arrays, particularly NetCDF files. It simplifies data manipulation and analysis, providing a high-level interface for working with labeled data.
  • numpy: Numpy is a fundamental package for scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with mathematical functions to operate on these arrays.
  • pandas: Pandas is a data manipulation and analysis library. It provides data structures such as DataFrame, which is ideal for organizing and analyzing structured data.
  • geopandas: Geopandas is geospatial manipulation library for vector data processing. Here we are using it to convert raster coordinates into shapefile
  • rioxarray: Rioxarray extends xarray’s capabilities by providing geospatial functionality, such as reprojecting, getting coordinate reference system (CRS) information, and handling spatial data.

3. Tutorial Steps

Step 1: Install Required Libraries

This command installs the necessary Python libraries (xarray, matplotlib, numpy, pandas, and rioxarray) using the conda

Step 2: Import Libraries

These lines import the required libraries for working with NetCDF files (xarray), numerical operations (numpy), and data manipulation (pandas and geopandas).

Step 3: Read NetCDF File Using xarray

ds = xr.open_dataset("HLSTimeSeries.nc", decode_coords="all")

Here, xr.open_dataset is used to open the NetCDF file named “HLSTimeSeries.nc”. The decode_coords="all" argument ensures that coordinate decoding is performed for compatibility with rioxarray.

Step 4: Check Coordinate Reference System (CRS) of NetCDF

ds.rio.crs

This line checks and prints the Coordinate Reference System (CRS) of the NetCDF file using the rioxarray extension of xarray.

Step 5: Reproject NetCDF to EPSG:4326

ds_4326 = ds.rio.reproject("EPSG:4326")

This code snippet reprojects the NetCDF file to EPSG:4326, which is a common geographic coordinate reference system.

Step 6: Get XY Coordinates in 32756 Projection

x, y = np.meshgrid(ds.x, ds.y)

This section creates a meshgrid of XY coordinates in the original projection (32756) using NumPy’s meshgrid function.

Step 7: Get All Variables in the NetCDF File

variables = list(ds.var())

These lines list all the variables present in the NetCDF file.

Step 8: Convert Variables and Bands to DataFrame

This section selects the first variable, extracts the raster values for the first band, and creates a pandas DataFrame (df) containing the x, y coordinates, and raster values.

Step 9: Convert Dataframe to GeoDataFrame

Feel free to customize the code and explanations based on your specific needs and dataset.

Complete Code:

I hope this tutorial will create a good foundation for you. If you want tutorials on another GIS topic or you have any queries, please send an email at contact@spatial-dev.guru.

We also offer freelancing services. Please email us at contact@spatial-dev.guru for any query.

Leave a ReplyCancel reply

Discover more from Spatial Dev Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Spatial Dev Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading