-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a custom Kernel density function with 3 methods #586
Comments
Thanks for moving this to Github! Some things that makes issues easier:
And some observations so far: There is no You can do: res = map(step, dims(raster)) And julia> rast = Raster("/home/raf/Downloads/raster.tif")^C
julia> Rasters.crs(rast)
WellKnownText{GeoFormatTypes.CRS}(GeoFormatTypes.CRS(), "PROJCS[\"unknown\",GEOGCS[\"GCS_unknown\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",
\"6326\"]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Mollweide\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\"
,1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]") The other things I need to see the call stack for. |
Hi @rafaqz Is that what you are looking for?
I am also attaching the jupyter notebook where I ran the code, here you have the output of each run With regard to the raster "ras" I tried to import it on a different file but I used the function Rasters.read instead of Rasters.Raster |
? using Downloads
Downloads.download(url, filename) By the "call stack" I mean the stacktrace... the full text off the specific errors that gave you trouble. It lists every function call that led to the error, and from that we can kind of see what happened. Just past it here - like everything from the function you called to the end of the error output.
|
Hi @rafaqz I added the stacktraces() at the end of the script
I am sorry but I am really not able to use the download function as I can't access my google drive from this virtual machine and I cannot work it locally at the moment |
Try to read the message, it tells you the problem. Your methods dont match
your inputs. If you are writing your own functions you need to learn these
outputs
…On Sun, 24 Dec 2023, 19:56 anjelinejeline, ***@***.***> wrote:
Hi @rafaqz <https://github.com/rafaqz> I added the stacktraces() at the
end of the script
julia> include("/storage/panelan/prova.jl")
ERROR: LoadError: MethodError: no method matching rasterKDE(; data::Matrix{Float64}, bw::Int64, res::Int64, ext::Extent{(:X, :Y), Tuple{Tuple{Int64, Int64}, Tuple{Int64, Int64}}}, crs::ESRIWellKnownText{GeoFormatTypes.CRS})
Closest candidates are:
rasterKDE(::Matrix{Real}, ::AbstractString, ::Real, ::Shapefile.Table, ::Real) got unsupported keyword arguments "data", "bw", "res", "ext", "crs"
@ Main /storage/panelan/prova.jl:54
rasterKDE(::Matrix{Real}, ::AbstractString, ::Real, ::AbstractRaster) got unsupported keyword arguments "data", "bw", "res", "ext", "crs"
@ Main /storage/panelan/prova.jl:80
rasterKDE(::Matrix{Real}, ::AbstractString, ::Real, ::Real, ::Extent, ::GeoFormatTypes.CRS) got unsupported keyword arguments "data", "bw", "res", "ext", "crs"
@ Main /storage/panelan/prova.jl:33
Stacktrace:
[1] top-level scope
@ /storage/panelan/prova.jl:128
[2] include(fname::String)
@ Base.MainInclude ./client.jl:478
[3] top-level scope
@ REPL[8]:1
in expression starting at /storage/panelan/prova.jl:128
prova.zip <https://github.com/rafaqz/Rasters.jl/files/13762622/prova.zip>
—
Reply to this email directly, view it on GitHub
<#586 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AATKU6KAAUNTEMOIEEBDXETYLB3FTAVCNFSM6AAAAABBBJ46MOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNRYGU3TMNBWGA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
As I suspected but I do not understand the reason ..
which of this argument I misinterpreted and is causing the issue? |
You probably want
Can I suggest you start systematically checking your object types with Like julia> using GeoFormatTypes
julia> supertype(EPSG)
CoordinateReferenceSystemFormat
julia> supertype(CoordinateReferenceSystemFormat)
GeoFormat
julia> supertype(WellKnownText)
AbstractWellKnownText
julia> supertype(AbstractWellKnownText)
MixedFormat
julia> supertype(MixedFormat)
GeoFormat |
You can also check inheritance like this: julia> Array{Int,2} <: Array{Real,2}
false
julia> Array{Int,2} <: Array{<:Real,2}
true Here you can see which Array type will work in your case. |
Hi @rafaqz using KernelDensity
using Shapefile
using GeoFormatTypes, Rasters, ArchGDAL
using GeoInterface, Extents
using CSV, DataFrames
# Try this out
# Method 1
# Load the CSV file
df = CSV.read("df.csv", DataFrame)
# Discard the first column
df = select(df, Not(1))
# Extract x and y columns
x_values = df.x
y_values = df.y
# Extract x and y coordinates
coordinates = hcat(x_values, y_values)
# Upload the World Shapefile
World=Shapefile.Table("WorldShape/World.shp")
crs=GeoInterface.crs(World)
supertype(typeof(crs))
AbstractWellKnownText{GeoFormatTypes.CRS}
ext = Extent(X = (-17601618, 17601617), Y = (-9018991, 8751339))
supertype(typeof(ext))
Any
supertype(typeof(coordinates))
DenseMatrix{Float64} (alias for DenseArray{Float64, 2})
function rasterKDE(data::DenseMatrix{Float64} ,bw::Signed, res::Signed, ext::Any, crs::AbstractWellKnownText)
lengthx=round(Int,(ext.X[2] + abs(ext.X[1])) / res)
lengthy=round(Int,(ext.Y[2] + abs(ext.Y[1])) / res)
rx = KernelDensity.kde_range((ext.X[1], ext.X[2]), lengthx)
ry = KernelDensity.kde_range((ext.Y[1], ext.Y[2]), lengthy)
# Create a bivariate kernel density estimator
# If the distribution is not specified use the default (normal distribution)
Bkde = KernelDensity.kde(data,(rx,ry), bandwidth=(bw,bw))
# Create a raster file
ras=Rasters.Raster(Bkde.density, (X(rx), Y(reverse(ry))), crs=crs)
return ras
end
rasterKDE (generic function with 1 method) but I still got the same error hope=rasterKDE(data=coordinates, bw=bandwidth, res=res, ext=ext, crs=crs)
MethodError: no method matching rasterKDE(; data::Matrix{Float64}, bw::Int64, res::Int64, ext::Extent{(:X, :Y), Tuple{Tuple{Int64, Int64}, Tuple{Int64, Int64}}}, crs::ESRIWellKnownText{GeoFormatTypes.CRS})
Closest candidates are:
rasterKDE(::DenseMatrix{Float64}, ::Signed, ::Signed, ::Any, ::AbstractWellKnownText) got unsupported keyword arguments "data", "bw", "res", "ext", "crs"
@ Main In[8]:1
Stacktrace:
[1] top-level scope
@ In[10]:1
|
It seems that the issue is due to the unsupported keyword arguments "data", "bw", "res", "ext", "crs" as hope=rasterKDE(data=coordinates, bw=bandwidth, res=res, ext=ext, crs=crs) while this actually works hope=rasterKDE(coordinates, 30000, 1000, ext, crs) |
Hello!
I am trying to create my own Kernel density function to estimate the density of a set of spatial points using the KernelDensity.jl.
I would like to define 3 methods:
resolution, extent and crs of the output.
A couple of comments about my code:
See the difference in R (where the resolution and crs are correctly retrieved) and Julia (what am I doing wrong ??)
R
![R](https://private-user-images.githubusercontent.com/145205734/292657106-89c48792-01a7-40f4-b70f-5f3f7037c590.PNG?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTk5MDg5NDksIm5iZiI6MTcxOTkwODY0OSwicGF0aCI6Ii8xNDUyMDU3MzQvMjkyNjU3MTA2LTg5YzQ4NzkyLTAxYTctNDBmNC1iNzBmLTVmM2Y3MDM3YzU5MC5QTkc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzAyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcwMlQwODI0MDlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT05MjIwYmY4MjNkMWE0YjVjNDE1MTBhYjk5OWVkYWFjYTU3MjEyNmRlYjRkMDJjN2E5MDcxOGY2M2Q5NjBmOTExJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.8vPCMVa8H9gT4XmBD6pDkhWdWpurm-SnTGVPjHD_PQU)
Julia
![julia](https://private-user-images.githubusercontent.com/145205734/292657139-9d839b9c-7820-4545-84a9-4bdb8ef84d0c.PNG?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTk5MDg5NDksIm5iZiI6MTcxOTkwODY0OSwicGF0aCI6Ii8xNDUyMDU3MzQvMjkyNjU3MTM5LTlkODM5YjljLTc4MjAtNDU0NS04NGE5LTRiZGI4ZWY4NGQwYy5QTkc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzAyJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcwMlQwODI0MDlaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1iYmMxZTNiM2MzNDM1M2ZmYjU3NGZmOWUyZjhlZDBiZWY5N2VjZDk3NGE0MTI5NjQwNTZiMmI1ZmJkNGQ3ODJiJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.O6L3tf8F4dT-7KBjZ5RohAW3OtnGgm1Chou7dEurfwU)
method1: I tried out the code with the files attached
but it does not work … I believe that it may be due to a very naive error and some misunderstanding I have about the julia language… Indeed it may be due to the type of data I passed as arguments when I defined the function… the error I got is related to the method … when I defined the different arguments I thought the following:
data::Array{Real,2} should work with every type of 2D Array/Matrix both float and int type
kernel_dist::AbstractStringt is a string as per the KernelDensity.jl and it should be set to the default normal distribution if it is not specified
bw::Real to be flexible and work with both integer and float types
res::Real to be flexible and work with both integer and float types
ext::Extents.Extent
crs::GeoFormatTypes.CRS to be flexible and work with all types of geformat types (given that we cannot retrieve the epsg always)
Can you help me understand what I am doing wrong?
I am really new to Julia and I would like to understand the reasons of these errors,
Thanks
Angela
df.csv
WorldShape.zip
The text was updated successfully, but these errors were encountered: