error code 3 while trying to upload shapefile to google earth engine - google-earth-engine

I am trying to upload a shapefile as a zip in Google Earth engine
The original zip is here if you need to try it. The error I get is the following one
Asset name: projects/earthengine-legacy/assets/users/derek_corcoran/Coastal
ID: ZU2VC3KLXWKN453JYYLVMKRS
Phase: Failed
Runtime: 19s (started 2022-11-15 19:18:40 +0100)
Attempted 1 time
Error: Projection exception. Ensure the projection is specified correctly and the coordinates are within its valid area. Detailed error: Unable to transform edge (8862777.916769, 7856465.827724 to 8862776.700196, 7856466.166777) from PROJCS["unknown", GEOGCS["GCS_unknown", DATUM["D_WGS_1984", SPHEROID["WGS_1984", 6378137.0, 298.257223563]], PRIMEM["Greenwich", 0.0], UNIT["degree", 0.017453292519943295], AXIS["Longitude", EAST], AXIS["Latitude", NORTH]], PROJECTION["Mollweide"], PARAMETER["semi_minor", 6378137.0], PARAMETER["false_easting", 0.0], PARAMETER["false_northing", 0.0], PARAMETER["central_meridian", 0.0], UNIT["m", 1.0], AXIS["x", EAST], AXIS["y", NORTH]] PLANAR to EPSG:4326.. (Error code: 3)
It seems that the correction is the issue, but the chosen projection is the one I could use in order to get that polygon to work, the source code on building that polygon is here in this repo
I am not sure what is wrong, I am very new using google earth engine

I saw that the .prj file in your shape .zip contains no information on the projection: PROJCS["unknown",GEOGCS["GCS_unknown"...
In the github repo you linked a Mollweide projection is specified here: terra::project("+proj=moll")
Maybe exchange +proj=moll for a EPSG:4326 projection to produce the shapes: +proj=longlat +datum=WGS84 +no_defs

Related

Unable to read shapefiles with st_read() in R

I am trying to read a shapefile
library(sf)
wards <- read_sf("Shape_files/benguluru_ward_boundaries_reprojected.shp")
and I am getting this error
Error in CPL_get_z_range(obj, 3) : z error - expecting three columns;
I googled it and it seems it has to do with z-dimension but I couldn't find a solution. How to solve this? Thanks in advance.
Edit:
This is what I am getting when I load the sf package
> library(sf)
Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
you can get the files here
Edit2: I was able to solve this by running extract z values as mentioned here and there dropping it while exporting the layer. I am able to read that new layer without any issues.

Source could be corrupt or not supported

I am looking to create a map using this dataset:
data
With the shape file of shapefile
Here is the following R code:
library("tidyverse")
library("dplyr")
library("sf")
library("ggplot2")
library("tmap")
library("tmaptools")
library("RColorBrewer")
data = readr::read_csv("C:/Users/amanm/Desktop/annualincomedata.csv")
localauthorities2 <- read_sf("C:/Users/amanm/Desktop/localauthorities2.shp")
map_and_data <- merge(localauthorities,data)
str(map_and_data)
ggplot(map_and_data) =
geom_sf(oes(fill = Percentage), color = NA) +
scale_filla_viridis_c()
I am given a error whilst trying to run the code:
> localauthorities2 <- read_sf("C:/Users/amanm/Desktop/localauthorities2.shp")
Error: Cannot open "C:\Users\amanm\Desktop\localauthorities2.shp"; The source could be corrupt or not supported. See `st_drivers()` for a list of supported formats.
In addition: Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
GDAL Error 4: Unable to open C:\Users\amanm\Desktop\localauthorities2.shx or C:\Users\amanm\Desktop\localauthorities2.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
Any advice would be useful.
Thanks
A shapefile consists of a bunch of files with different extensions - .shp, .dbf and .shx being the most essential. It looks like you've not got the corresponding .shx file:
GDAL Error 4: Unable to open C:\Users\amanm\Desktop\localauthorities2.shx or C:\Users\amanm\Desktop\localauthorities2.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
You can set config options in GDAL as environment variables. I made a shapefile called foo with all its components and deleted the .shx. Reading it produces the error:
> st_read("foo.shp")
Error: Cannot open "foo.shp"; The source could be corrupt or not supported. See `st_drivers()` for a list of supported formats.
In addition: Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
GDAL Error 4: Unable to open foo.shx or foo.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
setting the option in the environment fixes it:
> Sys.setenv(SHAPE_RESTORE_SHX="YES")
> st_read("foo.shp")
Reading layer `foo' from data source `foo.shp' using driver `ESRI Shapefile'
Simple feature collection with 10 features and 1 field
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 1 ymin: 1 xmax: 10 ymax: 10
CRS: NA
However if you've not got the corresponding .dbf file nothing is going to regenerate that from scratch since it contains the attributes. Chances are you'll find that in the same place as the .shx and then you don't need this hack.

R sf st_write error: feature not successfully written

I am saving a shapefile which looks like below:
Simple feature collection with 337152 features and 35 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -137.9625 ymin: 5.495833 xmax: -52.61605 ymax: 62.74232
Geodetic CRS: WGS 84
st_write(sf_dat, 'temp.shp'))
I get 50 warnings which says:
Warning messages:
1: In CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options), ... :
GDAL Message 1: Value 7120014530 of field hybas_id of feature 0 not successfully written.
Possibly due to too larger number with respect to field width
I am not able to understand the error. Could anyone explain why this is happening?
Thank you
Try saving it to a geopackage, temp.pgkg, turn your back away from shapefiles and never look back.
If you want to get rid of this problem, I would suggest trying [I havent ran the code so there proceed with caution]:
Saving in a different format e.g. geojson using st_write(sf_dat, 'temp.geojson'))
Re-formatting the fied to a string before saving it, to then format it back to integer when loading the dataset:
sf_dat$hybas_id <- as.character(sf_dat$hybas_id)
st_write(sf_dat, 'temp.shp'))
## then to re-load data (also using the dplyr library):
sf_dat <- st_read('temp.shp') %>% mutate(hybas_id = as.integer(hybas_id)))
EDIT: the advice provided by #Spacedman is great because it removes the need for handling data classes with every write / read - which can turn very tricky when sharing data with others. See comment for more.

Rdgal don't working well to save a shapefile

I'm trying to save a shapefile format with detection rates using the "rgdal" packages, but the following error occurs:
Error in showSRID(uprojargs, format = "PROJ", multiline = "NO") :
Can't parse PROJ.4-style parameter string
+proj=longlat +ellps=WGS84+datum=WGS84+no_def
How to fix?
Reproductive example
library(rgdal)
library(sp)
library(camtrapR)
Long<-c(-53.59390711, -53.58148303)
Lat<-c(-4.633924739, -4.6340598)
df1<-data.frame(Long,Lat)
df1$Station<-NA
df1[df1$Long=="-53.59390711",]$Station<-"w"
df1[df1$Long=="-53.58148303",]$Station<-"z"
Station<-c("w","w","w","w","w","w","w","w","w","w","z","z","z","z","z","z","z","z","z","z")
Species<-c("a","a","a","b","b","b","c","c","c","c","a","b","b","b","b","b","c","c","c","c")
df2<-data.frame(Station, Species)
detectionMaps(CTtable=df1,recordTable=df2,Xcol="Long",Ycol="Lat",stationCol="Station",speciesCol="Species",speciesToShow="a",richnessPlot=FALSE,speciesPlots=FALSE,
writeShapefile=TRUE,shapefileDirectory=tempdir(),shapefileName="sp_a", shapefileProjection="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_def")
#The shapefile should be created in this directory: C:\Users\...\AppData\Local\Temp\
This is an issue connected to the change from PROJ4 to PROJ6 in rgdal/sp. For you the issue is a bit deeper because the camtrapR package is not yet updated to deal with PROJ6.You can read more about the transition to PROJ6 here.
Without going too deep into it the solution for you would be to downgrade to an older version of sp and rgdal.
For sp (< 1.4-2) and rgdal (< 1.5-8).
EDIT: Since you can define the PROJ String yourself you can also first try: "+init=epsg:4326". This might still work.

Shapefiles in R

I'm relatively new to using Shapefiles in R. I want to create visualizations of country and district level data for India for which I'm using maptools in R. For the shapefiles, I got the data from gadm.org which includes state/district/subdistrict level data for India.
However, I'm trying to load the file in R and it's just not happening.This is my code :
library(maptools)
dist <- readShapePoly("IND_adm2.shp")
Here IND_adm2.shp is the district level shape file for India.
R is unable to read the file, giving me this error:
"Error in getinfo.shape(filen) : Error opening SHP file"
I'm not really sure what the problem is.I have R 3.0.1, have the files in the appropriate directory but something's just not right. I'm also new to making maps in R so it's been a bit annoying. Any ideas on how to fix this?
You could use the package rgdal
library(rgdal)
library(sp)
shp <- readOGR("path/to/shpfiles", "IND_adm2")
plot(shp)

Resources