yahyatamim / pyidw

A standalone python library for inverse distance weighted (idw) interpolation
MIT License
29 stars 8 forks source link

TypeError: cannot convert the series to <class 'float'> #3

Open femdias opened 1 year ago

femdias commented 1 year ago

Hello!

First of all, that's for creating this package!

I'm having a problem that I can't find a solution. My code is the following:

idw.idw_interpolation(
            input_point_shapefile = 'Feijão_2021-10.shp',
            extent_shapefile = "Setores_Censit_SP_WGS84.shp",
            column_name = '2021-10')

And the error is:

Traceback (most recent call last):

  File "C:\Users\Felipe.dias\AppData\Local\Temp\ipykernel_3392\818229306.py", line 1, in <module>
    idw.idw_interpolation(

  File "C:\Users\Felipe.dias\Anaconda3\lib\site-packages\pyidw\idw.py", line 174, in idw_interpolation
    blank_raster(extent_shapefile)

  File "C:\Users\Felipe.dias\Anaconda3\lib\site-packages\pyidw\idw.py", line 110, in blank_raster
    minX = floor(calculationExtent.bounds.minx)

  File "C:\Users\Felipe.dias\Anaconda3\lib\site-packages\pandas\core\series.py", line 207, in wrapper
    raise TypeError(f"cannot convert the series to {converter}")

TypeError: cannot convert the series to <class 'float'>

All files can be downloaded here: https://drive.google.com/drive/folders/19UTkWA4O6kfR-4sIaDTBbO2tc0hTCOjR?usp=sharing

Do you know who can I solve this problem?

Thanks in advance!

syukriyansyah-ipb commented 1 year ago

I have the same problem, have you found a solution?

femdias commented 1 year ago

No :( I decided to switch to R, there I used the function idw from spatstat.

safrul96 commented 1 year ago

No :( I decided to switch to R, there I used the function idw from spatstat.

May i have your source code on your github?

femdias commented 1 year ago

I can not display it publically on GitHub, but if you send me an e-mail (fem.dias@hotmail.com) I can send you my code!

safrul96 commented 1 year ago

this is my email @.***

Pada tanggal Rab, 28 Des 2022 pukul 03.21 Felipe Macedo Dias < @.***> menulis:

I can not display it publically on GitHub, but if you send me an e-mail ( @.***) I can send you my code!

— Reply to this email directly, view it on GitHub https://github.com/yahyatamim/pyidw/issues/3#issuecomment-1366156849, or unsubscribe https://github.com/notifications/unsubscribe-auth/A47BRLUAHUZMTGUTHE4Q5E3WPNFWDANCNFSM6AAAAAARNR6K6Y . You are receiving this because you commented.Message ID: @.***>

femdias commented 1 year ago

I don't know why, but your email is censored to me, it is shown: "@.***".

Nevertheless, I with edit my code to help you understand. For making a IDW in R, I basically follow this guide: https://rpubs.com/Dr_Gurpreet/interpolation_idw_R

library(tidyverse)
library(sf)
# install.packages('gstat')
library(gstat)
#install.packages('spatstat')
library(spatstat)
library(raster)
#install.packages('leaflet')
library(leaflet)
# install.packages("wesanderson")
library(wesanderson)

# Reading your Polygons shapefile 
Polygons<- sf::st_read("PATH\your_shapefile.shp")

# Reading your Points shapefile 
Points = sf::st_read("Monografia\\Outputs\\Estabelecimentos_IPC.shp")

# Let's assume that you have the columns "Latitude" and "Longitude" for the geografical/space position 
# and the column "Value" for the numerical values that you want to intepolate

# Extracting the maximun coordinates of your Polygon shapefile
min_x <- min(na.omit(st_coordinates(Polygons)[,'X']))
max_x <- max(na.omit(st_coordinates(Polygons)[,'X']))
min_y <- min(na.omit(st_coordinates(Polygons)[,'Y']))
max_y <- max(na.omit(st_coordinates(Polygons)[,'Y']))

# Creating an rectangle with the extrem point of your geometry (observation window)
obs_window <- owin(xrange=c(min_x, max_x), yrange=c(min_y, max_y)) 

# Creating a "point pattern object", with your Points shapefile. 
# Documentation: https://www.rdocumentation.org/packages/spatstat/versions/1.64-1/topics/ppp
ppp_points <- spatstat::ppp( x =  Points$Longitude, y = Points$Latitude, 
                                              marks = Points$Values, window = obs_window)

# With your point pattern object, you can do the IDW.
# "power" argument is where you choose which power to you want toin the IDW function (you can see it in the documentation: https://www.rdocumentation.org/packages/spatstat/versions/1.64-1/topics/idw). 
# "at" argument is where you choose if you want to interpolate in the full observation window or only in the points of the Points shapefile.
# "dimyx" is really important: the bigger the number, the highest will the resolution of your interpolation be (3000 = grid of 3000x3000 pixels)
idw_values <- idw(ppp_points , power = 2, at="pixels", dimyx=3000) # dimensão de 3000x3000 pixels

# Converting into a raster 
idw_raster <- raster(idw_values , crs= crs(Polygons))

# Saving raster
writeRaster(idw_raster, stringr::str_interp("PATH\\raster_name.tif"), overwrite=TRUE)

I hope this code can help you! Let me know if you need help

franz-waldner commented 1 year ago

Here is the solution:

calculationExtent = gpd.read_file(bounds_fn)
geom =box(*calculationExtent.total_bounds)
gdf_bounds = gpd.GeoDataFrame({'geometry': [geom]}, crs=calculationExtent.crs)
gdf_bounds.to_file(new_bounds_fn)
safrul96 commented 1 year ago

Thank you!

Pada tanggal Sab, 4 Feb 2023 pukul 05.53 franz-waldner < @.***> menulis:

Here is the solution:

calculationExtent = gpd.read_file(bounds_fn) geom =box(*calculationExtent.total_bounds) gdf_bounds = gpd.GeoDataFrame({'geometry': [geom]}, crs=calculationExtent.crs) gdf_bounds.to_file(new_bounds_fn)

— Reply to this email directly, view it on GitHub https://github.com/yahyatamim/pyidw/issues/3#issuecomment-1416499844, or unsubscribe https://github.com/notifications/unsubscribe-auth/A47BRLSFL4Q57ZR4R7KYJXDWVWD5NANCNFSM6AAAAAARNR6K6Y . You are receiving this because you commented.Message ID: @.***>