How can I solve this "IndexError: index 774 is out of bounds for axis 0 with size 724"?

Question:

Code:

import geopandas as gpd
import rasterio as rio

capital_cities = gpd.read_file(r'C:UsersuserDesktopSHPBangladesh_shapefile_Feature.shp')

capital_cities['Temp']=0

for index, row in capital_cities.iterrows() :
    city= row['DISTNAME']
    longitude=row['geometry'].x
    latitude=row['geometry'].y

    temp_raster = rio.open(r'C:UsersuserDesktopOutput rasterc2019GDP_Clip.tif') 
    temp_data = temp_raster.read(1) 
    rowIndex, colIndex = temp_raster.index(longitude, latitude)
    print(city +':'+str(temp_data[rowIndex, colIndex]))

My primary objective is to export the data in the CSV file.

Asked By: Syed Rafsan Ali

||

Answers:

Your code is fine. It is working on my side…
I understand that what you re printing at the end is what you expect your csv to be (later, for now you re just output in console).
That error "IndexError: index 774 is out of bounds for axis 0 with size 724" make me think to a projection system issue !!
Do both vector and raster layer have projection system defined properly? Are they in same projection system (must be…)?

Even with points that are not within the extent of the raster, you should have retrieved a nodata value (eg: -3.402823e+38 ), not raising an exception…

Answered By: limaNz
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.