-1

Possible Duplicate:
R/GIS: How to subset a shapefile by a lat-long bounding box?

I have a dataframe "rivers" of SpatialLinesDataFrame class. summary(rivers) gives:

> summary(rivers)
Object of class SpatialLinesDataFrame
Coordinates:
        min       max
x  66.33333 161.03750
y -10.45208  55.80208
Is projected: NA 
proj4string : [NA]
Data attributes:
     ARCID         FROM_NODE        TO_NODE         Sub_Bas         Maj_Bas             To_Bas                                Sub_Name    
 Min.   : 6087   Min.   : 6127   Min.   : 6227   Min.   : 1001   Min.   :    1.00   Min.   : -999   Tarim He                      : 1584  
 1st Qu.:35584   1st Qu.:37019   1st Qu.:36993   1st Qu.: 5017   1st Qu.:    5.00   1st Qu.: -888   Ch'ing-hai Hu                 : 1323  
 Median :50895   Median :53059   Median :53100   Median :13006   Median :   13.00   Median : 1050   Jo Shui / Gaxun Nur / Sogo Nur: 1301  
 Mean   :48954   Mean   :51014   Mean   :51024   Mean   :20846   Mean   :   21.41   Mean   :12745   Gobi1                         : 1246  
 3rd Qu.:63450   3rd Qu.:66032   3rd Qu.:66049   3rd Qu.:36043   3rd Qu.:   36.00   3rd Qu.:16006   Gobi2                         : 1074  
 Max.   :75961   Max.   :80067   Max.   :80068   Max.   :73006   Max.   :29055.00   Max.   :69014   Qagan Nur                     :  956  
                                                                                                    (Other)                       :42503  
                       Maj_Name        Sub_Area         Maj_Area          Strahler    
 Gobi Interior             :10130   Min.   :     0   Min.   :      0   Min.   :1.000  
 Amur Basin                : 5706   1st Qu.: 15409   1st Qu.: 413869   1st Qu.:1.000  
 Tarim Interior            : 4919   Median : 31063   Median :1584557   Median :1.000  
 Yangtze Basin             : 3716   Mean   : 84786   Mean   :1269122   Mean   :1.781  
 Ganges / Brahmaputra Basin: 3063   3rd Qu.: 88229   3rd Qu.:2079286   3rd Qu.:2.000  
 Plateau of Tibet Interior : 2104   Max.   :620574   Max.   :2165616   Max.   :7.000  
 (Other)                   :20349                                                     

I want to subset this by coordinates so that x is between 70,80 and y between 20,30. Is there any way of doing this using the maptools package?

Community
  • 1
  • 1
user702432
  • 11,898
  • 21
  • 55
  • 70
  • How is this question different from your previous question http://stackoverflow.com/questions/9825221/r-gis-how-to-subset-a-shapefile-by-a-lat-long-bounding-box ? – Andrie Mar 23 '12 at 09:33
  • Andrie... Sorry if this is the same. I am very new to this and am confused by the terms and formats. I'll delete this if you think this is a repost and if a get an answer from the original post. – user702432 Mar 23 '12 at 09:39
  • Do it with rgeos, that already does all the hard stuff – mdsumner Mar 23 '12 at 09:53
  • Hi mdsumner... I tried, but when I do: newdata = gIntersection(polygon, df), where df is of class SpatialLinesDataFrame, newdata comes out of class SpatialLines only, which means I can't export it with writeSpatialShape(). – user702432 Mar 23 '12 at 09:59
  • so concentrate on the actual problem, and report what you tried in the question itself, turning a SpatialLines object into a SpatialLinesDataFrame is trivial SpatialLinesDataFrame(newdata, data.frame(dummy = rep(1, length(newdata@lines)))) or something like that. As it stands this question is not helpful to you or others, and the real answer is coming out in the comments which is not how this works. – mdsumner Mar 23 '12 at 11:47

1 Answers1

0

No, there is no way to do this using the maptools package. You could manually decompose the Spatial object to do the clipping, but that would get complicated keeping track of the original topology, and would just be using the base R functions and the sp package anyway. Much better would be to use the geometry-specific functions suited for this task in rgeos.

mdsumner
  • 29,099
  • 6
  • 83
  • 91