Projects FarmmapsBulkSatDownload and FarmmapsDataDownload fully updated and tested

Added some extra Tasks to /FarmmapsApi/Services/GeneralService.cs
Added class SatelliteStatistics.cs to /FarmmapsApi/Models
This commit is contained in:
2021-05-26 10:16:29 +02:00
parent c673f8ddc4
commit 1bc326cfd2
14 changed files with 738 additions and 479 deletions

View File

@@ -1,15 +1,18 @@
# ShowGeotiff.r
# I downloaded and calculated the stats for the polygon defined in C:\git\FarmMapsApiClient_WURtest\FarmmapsDataDownload\DataDownloadInput.json
# in which I set "SatelliteBand": "wdvi" and in which in the console I requested image 8 for date '2020-06-14'
# in which I set "SatelliteBand": "wdvi" and in which in the console I requested the image for date '2020-09-22'
# FarmmapsBulkSatDownload generates many files. Here is what I tried when inputing the same field for a number of years using BulkSatDownloadInput.json
# see list below
library(raster)
library(sf)
library(rgdal)
setwd("C:/workdir/groenmonitor/")
# FarmmapsDataDownload generates two files:
setwd("C:/workdir/groenmonitor/DataDownload/")
# FarmmapsDataDownload and BulkSatDownload can be used to download zip files with inside two files:
fileGeotiff <- "wenr.tif"
# fileJpg <- "thumbnail.jpg"
# FarmmapsBulkSatDownload generates many files. Here is what I tried when inputing the same field for a number of years using BulkSatDownloadInput.json
fileJpg <- "thumbnail.jpg"
# Here is what I tried when inputing the same field for a number of years:
# fileGeotiff <- "wheat_fld5641_20210224.tif" # 2 files for year 2021. This file is a nice example having in upperleft corner no data
# fileGeotiff <- "wheat_fld5641_20210331.tif" # 2 files for year 2021
# fileGeotiff <- "wheat_fld5641_20200321.tif" # 14 files for year 2020, earliest
@@ -24,11 +27,11 @@ year <- substr(fileGeotiff,lenfilename-11,lenfilename-8)
imgdate <- substr(fileGeotiff,lenfilename-11,lenfilename-4)
# The thumbnail has the polygon clipped out, has 1 layer, no crs and the mean value is not the mean wdvi we are looking for
# r.thumbnail <- raster(fileJpg)
# plot(r.thumbnail)
# crs(r.thumbnail)
# CRS arguments: NA
# cellStats(r.thumbnail,'mean') #59.91667
r.thumbnail <- raster(fileJpg)
plot(r.thumbnail)
crs(r.thumbnail)
#CRS arguments: NA
cellStats(r.thumbnail,'mean') #87.5128 # nonsense
stk.wenr <- stack(x=fileGeotiff)
# plot(stk.wenr) shows 5 plots (5 bands)
@@ -49,11 +52,7 @@ crs(stk.wenr)
r.wenr.rd.wdvi <- subset(stk.wenr,2)
dev.off()
plot(r.wenr.rd.wdvi,main=paste("wdvi",imgdate),xlab="RDX",ylab="RDY")
cellStats(r.wenr.rd.wdvi,'mean') #0.1654627
# Same as in downloaded file SatelliteDataStatistics_test_satData_wdvi_2020-06-14.csv
# "mean": 0.16564258790046743
# So FindSatelliteItem in C:\git\FarmMapsApiClient_WURtest\FarmmapsApi\Services\GeneralService.cs returns a a stacked geotiff in
# i.e. the cellstats are calculated from the rectangle
cellStats(r.wenr.rd.wdvi,'mean') #0.1350561
# Furthermore we can see
# shows coordinates in RD
@@ -69,6 +68,15 @@ r.wenr.wgs84.wdvi <- projectRaster(r.wenr.rd.wdvi, crs = CRS('+init=EPSG:4326'))
# plot(p1,add=TRUE, col="transparent",border="black")
# Draw the polygon on top of the raster
# Polygon p2 from C:\git\FarmMapsApiClient_WURtest\FarmmapsDataDownload\DataDownloadInput.json
p2 <- data.frame(id = 1, wkt = gsub("\n","",'POLYGON((
4.960707146896585 52.800583669708487,
4.960645975538824 52.800470217610922,
4.962140695752897 52.799177147194797,
4.967523821195745 52.801502400041208,
4.966336768950911 52.802543735879809,
4.961711880764330 52.801009996856429,
4.960707146896585 52.800583669708487))'))
# Polygon p2 from C:\git\FarmMapsApiClient_WURtest\FarmmapsBulkSatDownload\BulkSatDownloadInput.json
p2 <- data.frame(id = 1, wkt = gsub("\n","",'POLYGON((
3.37837807779104 51.3231095796538,
3.38065689232502 51.3212527499355,
@@ -123,11 +131,29 @@ dev.off()
plot(r.wenr.wgs84.wdvi.pol,main=paste("wdvi",imgdate),xlab="LON",ylab="LAT")
plot(p2.wgs84,add=TRUE, col="transparent",border="red")
#That's what we want! Now compare the stats
cellStats(r.wenr.rd.wdvi,'mean') #0.1654627 # Stats from rectangle, RD
cellStats(r.wenr.wgs84.wdvi,'mean') #0.1656399 # Stats from rectangle, WGS84
cellStats(r.wenr.rd.wdvi.pol,'mean') #0.1658702 # Stats from raster clipped by polygon, WGS84
cellStats(r.wenr.wgs84.wdvi.pol,'mean') #0.1658611 # Stats from raster clipped by polygon, WGS84
# Conclusion: in this example little difference, but can be quite different!
cellStats(r.wenr.rd.wdvi,'mean') # [1] 0.1350561 # Stats from rectangle, RD
cellStats(r.wenr.wgs84.wdvi,'mean') # [1] 0.1351411 # Stats from rectangle, WGS84
cellStats(r.wenr.rd.wdvi.pol,'mean') # [1] 0.05723957 # Stats from raster clipped by polygon, RD
cellStats(r.wenr.wgs84.wdvi.pol,'mean') # [1] 0.05723607 # Stats from raster clipped by polygon, WGS84
# file SatelliteDataStatistics_test_satData_wdvi_2020-09-22.csv
# "mean": 0.057430520945401985 # SatelliteDataStatistics_test_satData_wdvi_2020.csv returns stats for the clipped raster (.pol). 'mean' almost the same, maybe
# cellStats cannot return median, just a few stats.
cellStats(r.wenr.wgs84.wdvi.pol,'median') # Error in .local(x, stat, ...) : invalid 'stat'. Should be sum, min, max, sd, mean, or 'countNA'
r.wenr.wgs84.wdvi.vals <- values(r.wenr.wgs84.wdvi)
median(r.wenr.wgs84.wdvi.vals) # [1] NA
median(r.wenr.wgs84.wdvi.vals,na.rm=TRUE) # [1] 0.076
r.wenr.wgs84.wdvi.pol.vals <- values(r.wenr.wgs84.wdvi.pol)
median(r.wenr.wgs84.wdvi.pol.vals) # [1] NA
median(r.wenr.wgs84.wdvi.pol.vals,na.rm=TRUE) # [1] 0.048
# "median": 0.04800000041723251 # SatelliteDataStatistics_test_satData_wdvi_2020.csv returns stats for the clipped raster (.pol).
# An image may contain NA values. Check:
cellStats(r.wenr.wgs84.wdvi,'countNA') # [1] 22956
ncell(r.wenr.wgs84.wdvi) # [1] 221696
cellStats(r.wenr.wgs84.wdvi,'countNA') / ncell(r.wenr.wgs84.wdvi) # [1] 0.1035472 # 10% no data? doesn't show in the plot?
cellStats(r.wenr.wgs84.wdvi.pol,'countNA') # [1] 147387
summary(r.wenr.wgs84.wdvi.pol.vals) # shows the same: NA's: 147387
ncell(r.wenr.wgs84.wdvi.pol) # [1] 221696
cellStats(r.wenr.wgs84.wdvi.pol,'countNA') / ncell(r.wenr.wgs84.wdvi.pol) # [1] 0.6648158 # 66% no data? doesn't show in the plot?
# The project FarmmapsNbs can generate a wenr.tif file, application.tif, uptake.tif (in rtest1.uptake.zip)and shape.shp (in rtest1.taskmap.zip)
r.application <- raster("C:/git/FarmMapsApiClient_WURtest/FarmmapsNbs/bin/Debug/netcoreapp3.1/Downloads/application.tif")