[R-sig-Geo] Creating a grid
Edzer Pebesma
edzer.pebesma at uni-muenster.de
Wed May 18 18:43:44 CEST 2016
On 18/05/16 18:22, Andres Diaz wrote:
> Hello everybody,
>
>
> I am trying to build a grid from a polygon, using the functions spsample
> and gridded, but it seems there is something wrong with the coordinates in
> longitude and latitude which not work properly.
>
> Someone have an answer?
>
>
>
> #load the packages
> require(SP)
this should be
require(sp)
as package names are case sensitive.
>
> #countries coordinates in longitude, latitude
>
> Albaniax<-c(20.0100272335,19.8572911876,19.2897912464,19.4794450497,19.3072091765,19.5216631874,19.4425003026,19.5991632702,19.3677732887,19.2885363079,19.6515270311,19.8225000867,20.0714181366,20.5251360888,20.5896452275,20.5191631446,20.7408091636,20.9834910775,21.0420822572,20.7919272061,20.6670821107,20.3154181886,20.4133271998,20.2200001113,20.0100272335)
> Albaniay<-c(39.6912001762,40.0434731274,40.4214540464,40.3548539581,40.6453091998,40.9098541347,41.4069450832,41.7797181580,41.8490001200,42.1829091596,42.6231910886,42.4719361250,42.5609091892,42.2130540039,41.8821911156,41.2463821837,40.9095361253,40.8558911066,40.5640270347,40.4315450196,40.0962450871,39.9918000353,39.8201361432,39.6473542729,39.6912001762)
>
> Algeriax<-c(4.2452821739600,3.3319452420500,3.1170091041500,3.2780541307000,3.2330542070000,2.4216631785700,2.2036091403200,1.7897181364400,1.6275003139800,1.1763911453200,1.1708002482500,-4.8061089467500,-8.6667909290000,-8.6666638593500,-8.6672179031500,-7.1262549561800,-6.5833998220400,-6.4002817239600,-5.5306907830400,-4.9151368878800,-3.6222177582600,-3.6001368077500,-3.8238908754500,-3.8134728403300,-2.9994457033400,-2.8594457917000,-1.1805549395600,-1.2504179350800,-1.0118087879800,-1.3827817652700,-1.6666637508500,-1.6544457858300,-1.7933367716900,-1.6925819366500,-1.8549638767700,-1.7575998568500,-2.2094459624300,-1.3693088617100,-0.7916638001530,-0.3731268749630,-0.0522178320053,0.2041631291760,1.1825002116500,2.5725002205000,2.9002091662500,3.9016630348300,4.7887453313600,5.3280540822500,6.3983362842300,6.9204182124000,7.1705543204200,7.2297181517900,7.8766632802500,8.6220270396900,8.1816633033000,8.3763911755000,8.2605541114800,8.4011090727600,8.2527091530000,7.528891075
7300,7.4925002048000,7.7430540668800,8.1577730351600,8.3486091897100,9.0572183036200,9.5371090838500,9.3113911362300,9.8397180851900,9.7902822906800,9.9558362782400,9.7344452366900,9.9305542789000,9.8716631946600,9.4994451694600,9.3983361151900,10.0320820931000,10.0544451784000,10.2522180284000,11.5588911906000,11.9864731952000,7.4637730756100,5.8125002592300,4.2452821739600)
>
> Algeriay<-c(19.1466642562000,18.9763910895000,19.1455541570000,19.4058271711000,19.8171450983000,20.0530542668000,20.2830541935000,20.3129180764000,20.5711090257000,20.7337449375000,21.1008540251000,25.0002731406000,27.2904541814000,27.6666640042000,28.7094450141000,29.6358270373000,29.5683272355000,29.8044451133000,29.9059730951000,30.5098541843000,30.9736091303000,31.0908271967000,31.1615910791000,31.6980541739000,31.8333271951000,32.0865270563000,32.1122182602000,32.3234732339000,32.5055541552000,32.7244450427000,33.2588820638000,34.0836092031000,34.3783271455000,34.4890820940000,34.6143731066000,34.7546450949000,35.0858271660000,35.3129182370000,35.7650000417000,35.9027730493000,35.8061090860000,36.1033360673000,36.5122182134000,36.5891640848000,36.7947820609000,36.9147182109000,36.8938910255000,36.6402732425000,37.0863911554000,36.8843001165000,36.9200001510000,37.0863911554000,36.8475000411000,36.9413641135000,36.5052731361000,36.4201361336000,35.8563820650000,35.1922181604000
,34.6552090685000,34.1043091513000,33.8875001609000,33.2291641937000,33.0280541727000,32.5333361410000,32.0955541984000,30.2343909823000,30.1272181239000,29.1599999713000,28.2705541457000,27.8462452744000,27.3160360852000,26.8597180666000,26.5141640490000,26.3575000756000,26.1533271396000,25.3294450637000,24.8380539771000,24.6058271463000,24.3025001512000,23.5223090810000,20.8566732593000,19.4461090891000,19.1466642562000)
>
> #creating poligons
> Albania = Polygon(cbind(Albaniax, Albaniay))
> Algeria = Polygon(cbind(Algeriax, Algeriay))
>
> AlbaniaPol = Polygons(list(Albania), ID=as.numeric(1))
> AlgeriaPol = Polygons(list(Algeria), ID=as.numeric(2))
>
>
>
> # creating the object spatial polygon
>
> Countries = SpatialPolygons(list(AlbaniaPol,AlgeriaPol), 1:2
> ,proj4string=CRS("+proj=longlat +datum=WGS84")) # here I am specifing the
> G.C
>
>
> # retriving the points from the spatial polygon
>
> Pointlocationcountries <- spsample(Countries, type="regular",
> cellsize=1000, offset = c(0.5, 0.5))
>
> #here is the errror
> Error in .local(obj, ...) :
> cannot derive coordinates from non-numeric matrix
Although I admit that the error message is far from helpful, you're
making a wrong assumption here: you assume cellsize can be in metres,
where the polygon in question has coordinates in degrees long/lat. You
can do two things:
1. define a meaningful cell size in degrees, e.g. cellsize = 0.1
2. project the polygon using spTransform to a reference system where the
units of coordinates are in metres, and then use cellsize = 1000
>
>
>
> #creating the grid
>
> gridded(Pointlocationcountries)<-TRUE
>
>
>
--
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
Journal of Statistical Software: http://www.jstatsoft.org/
Computers & Geosciences: http://elsevier.com/locate/cageo/
Spatial Statistics Society http://www.spatialstatistics.info
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20160518/16e756f6/attachment.bin>
More information about the R-sig-Geo
mailing list