Friday, March 27, 2015

R - Get the Floodplain maximum discharge from CROSSMAX.OUT

This is an output file for Floodplain maximum discharge from Flo-2D modeling. I was running with an issue. I have attached one sheet with 5 Floodplain cross-sections discharge. In the actual sheet, I have 1500 of them. You can download the file from the below mentioned link.

CROSSMAX.OUT file from Flo-2D modeling

This is the code developed to extract flow values from the above mentioned file.

rm(list=ls())

#working directory
workDir <- "C:/Users/PKuranjekar/Desktop/Thumb drive/GIS Toolbox/R/reneedyourhelpmann/"

setwd(workDir)

#read data
inData <- readLines("CROSSMAX2.txt")

#extract lines with the phrase "CROSS SECTION"
inData2 <- inData[grep("CROSS SECTION", inData)]

#remove spaces
inData2 <- gsub(" ", "", inData2)

#split text using ":"
inData2 <- do.call(rbind,strsplit(inData2, ":"))

#identify flow
cfs <- do.call(rbind,strsplit(inData2[,2], "CFS"))[,1]

#identify cross-section number
cs <- do.call(rbind,strsplit(inData2[,1], "SECTION"))[,2]
cs <- gsub("IS", "", cs)

#create output
outData <- data.frame(cs, cfs, stringsAsFactors=FALSE)

#write output
write.table(outData, "CROSSMAX2_output.txt", sep="\t", row.names=FALSE, quote=FALSE, na="")

The output of this code will be

Number of c/s     Discharge
1                        185.94
2                         88.34
3                         344.34
4                         370.94                      
5                         444.72

and so on

Thursday, January 22, 2015

R - Copy out selected LiDAR files into a different folder

Before this, Please refer to the post "LiDAR file Processing in R, C#, MS-DOS, ArcGIS & LAStools"

Once you export the selected attributes into a text file. Then open a text file in a text pad and save that particular column in different text file.


Manually, it will be very difficult to copy out selected files of 2548 files. Here is the R program which helps to copy out all these selected files into a separate folder automatically.

work_dir <- "K:/Resource Info/20140312_CHSTP-Backersfield to Palmdale/Rasters/LIDAR/From_GeoDigital_July_2012/"
setwd(work_dir)

las_file_nos <- readLines("Selected_LAS.csv")
las_file_nos <- paste0(las_file_nos)

las_file_names <- list.files("LAS/", pattern = "BP_")

las_present <- las_file_nos %in% las_file_names

for (eachFile in 1:length(las_present)) {
  if (las_present[eachFile]) {
    file.copy(paste0("LAS/", las_file_nos[eachFile]), 
              "LAS/Output folder/")
  }
}




Web Services (OGC, WMS, WMTS, WCS, WFS, CSW)

WFS = Web Feature Services
WMS = Web Map Services

R - Extract several zipped files into a single folder

# read zipped files and extract tif or jpeg files from the bottom most folder

I have downloaded 4200 1' Orthoimagery raster files from USGS. Each zipped folder has several subfolder and then you will find the raster files.




The goal is to get raster files from all the zipped folder into a single folder.

setwd("I:/J_Prashant's External HD/High Speed Rail/GIS/Bulk Data/Bulk data/Firm Panels/")

output_location <- "output_folder"
dir.create(output_location)

zipped_files <- list.files()
zipped_files <- zipped_files[grepl(".zip", zipped_files)]

for (eachFile in zipped_files) {
  unzip(zipfile = eachFile, exdir = output_location, junkpaths = TRUE)
}


LiDAR file Processing in R, C#, MS-DOS, ArcGIS & LAStools



LiDAR files always comes in .LAS or .LAZ format. These are some of LiDAR files out of 2548 LiDAR files. LiDAR files are the point files. The first question comes to an user's mind How to see the coverage of each of the LiDAR files?



Now open an ArcMAP Document-- Open ArctoolBox --Click on 3D Analyst tools --Click on Conversion -- Click on From File -- Double click on Point File Information


Once the geoprocessing completes then you will get a polygon shapefile in a Table of content of your ArcMAP document.


More Closer look of these LiDAR tiles




If you open an attribute table of this polygon shapefile then you will find the LAS file names are associated with each of these LiDAR tiles.  The attribute table will going to show LiDAR file Names, Number of points in each LiDAR files, Average Point spacing, Lowest and Highest Elevation.



Let's say if i want to get all the LiDAR files with in the red boundary of a project area in a separate folder. Then, First we need to select the attributes with in the red boundary and export the selected rows in a separate shapefile.




Export these selected attributes into a text file and open in a textpad. Now Please refer to "Copy out selected LiDAR files into a different folder in R"










R - Combine text files vertically




Lets say if you want to combine some selected xyz files vertically from a single folder. Then this task can be done in R. Make sure to check whether that particular file is in comma formatted or Space  formatted and you need to make those changes accordingly.

workDir <- "Q:/Jellys Ferry Drainage/LiDAR_Before_Projection/"
setwd(workDir)
inData1 <- read.csv("1132n0266e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData2 <- read.csv("1134n0264e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData3 <- read.csv("1134n0266e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData4 <- read.csv("1134n0268e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData5 <- read.csv("1136n0264e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData6 <- read.csv("1136n0266e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData7 <- read.csv("1136n0268e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData8 <- read.csv("1138n0264e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData9 <- read.csv("1138n0266e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData10 <- read.csv("1138n0268e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData11 <- read.csv("1140n0264e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
inData12 <- read.csv("1140n0266e5k_XYZ.txt", header=FALSE, sep=",", as.is=TRUE)
outData <- rbind(inData1, inData2, inData3, inData4, inData5, inData6, inData7, inData8, inData9, inData10, inData11, inData12)
write.table(outData, "Total_XYZ_Before_Projection.txt", sep=",", col.names=FALSE, row.names=FALSE)