Sunday, 13 January 2019

shapefile - writeOGR error: length(dsn) == 1L is not TRUE


I'm running a for loop R that results in a shapefile being written with each iteration. The for loop runs successfully once, with a shapefile being written out, but then on the second iteration I get this error:



Error in writeOGR(temp, outfile, layer = complete_paste, driver = "ESRI Shapefile",  : 
length(dsn) == 1L is not TRUE

For some reason, I cannot find any other question encountering this error with writeOGR(). I don't know what it means. Can anyone explain?


If it helps, here's the entire code I'm running. Basically I'm taking a large shapefile, and exporting smaller shapefiles from it by values in shp1$Vector.


library(rgdal)


shp1<- readOGR("/Users/JohnDoe/Desktop/Zone_Fixup/Z1/z1_scrub/z1_merge.shp")


output_path1<- "/Users/JohnDoe/Desktop/Zone_Fixup/Z1/z1_split/"

#Begin for loop to split shapefile by shp1$Vector values
for(i in 1:length(shp1$Vector)){

#subset by join_id (Vector)
temp<- shp1[shp1$Vector==shp1$Vector[i],]
id_paste<- paste(temp$Vector)

#Create strings for shapefile write-out

outfile<- paste(output_path1, "Zone1_", id_paste, ".shp", sep = "")
complete_paste<- paste("Zone1_", id_paste, sep = "")

#Write out shapefile
writeOGR(temp, outfile, layer = complete_paste, driver = "ESRI Shapefile", overwrite_layer = TRUE)

}

Answer



As you've confirmed in the comments, id_paste is a vector any time temp has more than one row. This means you'd be sending multiple copies of outfile and complete_paste to writeOGR().


run paste('How do you like them ', c('apples', 'apples', 'apples'), '?', sep = '') to see what happens when you supply a vector to paste().



Try id_paste = shp1$Vector[i] instead.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...