Input File, XY min,max file

I have a XYZ .csv file for a large portion of an area. I have another txt file which contains Box numbers, Xmin, Ymin, Xmax, Ymax. I would like to export a CSV file for each boxes from subsetting a larger CSV file.

enter image description here

I am seeking python or R Code.

closed as off-topic by PolyGeo Oct 21 '17 at 20:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.


Here is a suggestion using R. I have used the over function from the sp package to extract the points that fall within the polygons of the bounding boxes and assign the respective ID of the bounding box.


#read in points from csv 
input <- read.csv("input.csv", header=F)
names(input) <- c("x","y","z")

# create spatial points object
pts <- SpatialPoints(input[,1:2])

# read in bounding box coords
boxes <- read.table("XY(min,max).txt", sep=",")
names(boxes) <- c("id", "xmin", "ymin","xmax", "ymax") 

# expand to coordinates for bounding box rectangles
poly.coords <- cbind(boxes$xmin, boxes$ymin, 
                 boxes$xmin, boxes$ymax, 
                 boxes$xmax, boxes$ymax, 
                 boxes$xmax, boxes$ymin, 
                 boxes$xmin, boxes$ymin)
ID <- boxes$id

# create polygons for bounding box rectangles
polys <- SpatialPolygons(mapply(function(poly, id) {
  xy <- matrix(poly, ncol=2, byrow=TRUE)
  Polygons(list(Polygon(xy)), ID=id)
}, split(poly.coords, row(poly.coords)), ID))

polys.df <- SpatialPolygonsDataFrame(polys, data.frame(id=ID, row.names=ID))

# overlay points on polygons, get polygon ID and add to input csv
input <- cbind(input, over(pts, polys.df))

# write out csv only for points that fall within bounding box polygons
output <- input[!is.na(input$id),]
>              x       y       z id
> 102423 6521081 2056150 2544.94 B1
> 102424 6521054 2056184 2544.61 B1
> 102425 6521030 2056193 2544.75 B1
> 102427 6521073 2056161 2544.91 B1
> 102429 6521035 2056142 2545.27 B1
> 102432 6521046 2056171 2544.81 B1

# write out three separate files, no quotes around the ID column
for (i in unique(output$id)){
  write.csv(output[output$id == i,], paste0("output",i,".csv"), row.names=F, quote=F)
  • Thank you!!...What if i want the separate CSV file for each one of the bounding polygons (B1, B2, B3) and each one of those csv file only have xyz coordinates. How does the code will get affected. Thx again – user37082 Oct 31 '14 at 20:07
  • One more thing I have observed that when you write a output.csv, then its comes out to be 6521080.83,2056150.3,2544.94,"B1". In the 4th Column I don't want inverted comma for B1.What changes i need to have in the code – user37082 Oct 31 '14 at 20:36
  • @Prashant Kuranjekar: This is just standard R subsetting. And if you look up the write.csv command you will find the different options for writing out a csv. I updated my example accordingly. – cengel Oct 31 '14 at 22:43
  • Thank you!! One last thing, Instead of manually putting last 3 lines to generate 3 csv files, Will it be possible to generate multiple csv with one statement? – user37082 Nov 1 '14 at 6:35
  • @Prashant Kuranjekar: of course. See above. Also, if my answer was helpful for you, would you accept it? Thanks. – cengel Nov 1 '14 at 19:06