0

Hy. I'm working with shapefiles in R. I wrote a script which processes one of my shapefiles. Therefore I wrote mutliple functions and for loops. After processing the shapefile it will be written to another folder. Now i need to apply this to all my shapefiles. Is there an easy way to do this? I'm new to R.

What I've got sofar: Script for one shapefile, which looks like this: Upload the file and set time format:

setwd("")
shape=ReadOGR(dsn=", layer= "Trip_2")
shape$Zeit= as.POSIXct(strptime(shape$Zeit_txt, format= "%d.%m.%Y%H:%M:%S", tz="GMT"), tz="GMT")

First extractions: make Wegtyp NA if Count is not 1

shape$wegtyp[shape$Count != 1] = NA
shape$name_wegty[shape$Count != 1] = NA
shape$OBJECTVAL[shape$Count != 1] = NA
shape$name_weg_1[shape$Count != 1] = NA

First Function:

calculateDistancesWGS = function(long, lat){
  distWGS = long
  distWGS = NA
  for (i in 1:(length(lat)-1)){
    distWGS[i+1] = distCosine( c(long[i+1], lat[i+1]), c(long[i], lat[i]))
  }
  return(distWGS)
}

shape$distanceWGS = calculateDistancesWGS(shape$longitude, shape$latitude)

several more function follow

extract file as shapefile:

 writeOGR(shape, dsn= "", layer= "Trip_03", driver= "ESRI Shapefile" )

Now I need to do this with all my shapefiles and save them with different names. I'm able to import several shapefiles to R creating a list of dataframes like this:

 setwd("")
list_shp <-        list.files(path="", pattern="*.shp", full.names = TRUE,
                       recursive = TRUE, include.dirs = FALSE)
shapes <- lapply(list_shp, function(x) {readOGR(dsn=x,     layer=ogrListLayers(x))})

But when it gets to applying the timeformat and the functions i could not figure out what to do.

Here's a short example of my data:

d1= data.frame(longitude=(10.42206), latitude= (46.60109), Wegtyp= (1),     Zeit_txt= 14.50)
d2= data.frame (longitude= (10.42207), latitude= (46.60108), Wegtyp= 2, Zeit_txt= (14.55))

Any help would be highly appreciated

1 Answer 1

1

You can just lapply a function that does the command you wanted, like the following:

addTime <- function(shape) { 
  shape$Zeit= as.POSIXct(strptime(shape$Zeit_txt, format= "%d.%m.%Y%H:%M:%S", tz="GMT"), tz="GMT")
  return(shape)
}
shapes <- lapply(shapes, addTime)
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks a lot, works perfectly and I now unterstand what it does. Could you also give me an example how to do this for the first function? Do I have to make a function around a function here? Thanks again for your help
Since it's a list of data.frames, you'll want to wrap the part that adds the column to the data.frame in a function, and lapply as above. For example: addDistance <- function(shape) shape$distanceWGS = calculateDistancesWGS(shape$longitude, shape$latitude); return(shape)
Works. Exactly the solution i was looking for. Thanks a lot.
Mhhm, another question. I figured out how to apply your suggestions to all my functions. However I'm now stuck at writing each dataframe as a new shapefile. I'd like the name to be shape_1, shape_2 and so on. I use writeOGR to save as shapefile. Here's my code for one shapefile: writeOGR(shapes, dsn= "", layer= "shape_1", driver= "ESRI Shapefile" ) Since I'm new to programming I don't see a good way to do this. Could you help me out with this?
Are you trying to write each of the dataframes in the list shapes as its own shapefile? For this you'd want to make a vector of the names, e.g., c("shape_1", "shape_2") (or look at the paste function) and use a for loop or mapply to iterate over the elements of the list.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.