2015-04-19 10 views
19

Ich habe an Erdbebendaten gearbeitet, die lange Werte haben, und ich möchte diese lat langen Werte in räumliche Koordinaten umwandeln.So konvertieren Sie Datenrahmen in räumliche Koordinaten

Angenommen, ich habe folgende Datensatz df:

longitude   latitude 
     128.6979 -7.4197 
     153.0046 -4.7089 
     104.3261 -6.7541 
     124.9019 4.7817 
     126.7328 2.1643 
     153.2439 -5.6500 
     142.8673 23.3882 
     152.6890 -5.5710 

Ich möchte es in räumliche Punkte konvertieren. Etwas wie folgt aus:

lon  lat 
[1,] 2579408.24 1079721.15 
[2,] 2579333.69 1079729.18 
[3,] 2579263.65 1079770.55 
[4,] 2579928.04 1080028.46 
[5,] 2579763.65 1079868.92 
[6,] 2579698.00 1079767.97 

habe ich den folgenden Code:

library(sp) 
df.sp<-df 
coordinates(df.sp)<-~x+y 

Aber ich die folgende Fehlermeldung:

Error in `[.data.frame`(object, , -coord.numbers, drop = FALSE) : 
    undefined columns selected 

Antwort

26

Zuerst nehmen Sie die Spalten von lon und lat und Erstellen Sie ein Objekt für coord. Dann subtrahieren Sie sie vom ursprünglichen Datenrahmen und erstellen ein neues Objekt. Sie verwenden schließlich SpatialPointsDataFrame(), um eine SpatialPointsDataFrame zu erstellen. Wenn Sie eine SpatialPointsDataFrame erstellen, müssen Sie proj4string zuweisen. Wählen Sie eine passende für Sie. In Ihrem Fall haben Sie keine anderen Spalten als lon und lat, die Methode wird nicht funktionieren. Ich verließ absichtlich lon und lat @data.

DATA

mydf <- structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019, 
126.7328, 153.2439, 142.8673, 152.689), latitude = c(-7.4197, 
-4.7089, -6.7541, 4.7817, 2.1643, -5.65, 23.3882, -5.571)), .Names = c("longitude", 
"latitude"), class = "data.frame", row.names = c(NA, -8L)) 


### Get long and lat from your data.frame. Make sure that the order is in lon/lat. 

xy <- mydf[,c(1,2)] 

spdf <- SpatialPointsDataFrame(coords = xy, data = mydf, 
           proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")) 


#> str(spdf) 
#Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots 
#[email protected] data  :'data.frame': 8 obs. of 2 variables: 
#.. ..$ longitude: num [1:8] 129 153 104 125 127 ... 
#.. ..$ latitude : num [1:8] -7.42 -4.71 -6.75 4.78 2.16 ... 
#[email protected] coords.nrs : num(0) 
#[email protected] coords  : num [1:8, 1:2] 129 153 104 125 127 ... 
#.. ..- attr(*, "dimnames")=List of 2 
#.. .. ..$ : NULL 
#.. .. ..$ : chr [1:2] "longitude" "latitude" 
#[email protected] bbox  : num [1:2, 1:2] 104.33 -7.42 153.24 23.39 
#.. ..- attr(*, "dimnames")=List of 2 
#.. .. ..$ : chr [1:2] "longitude" "latitude" 
#.. .. ..$ : chr [1:2] "min" "max" 
#[email protected] proj4string:Formal class 'CRS' [package "sp"] with 1 slot 
#.. .. [email protected] projargs: chr "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0" 
+0

Was wäre das Gegenteil von oben. Ich möchte SpatialPointsDataFrame xy-Koordinaten in LAT LON konvertieren? Vielen Dank! –

+0

@ M.Qasim Wenn Sie das obige Beispiel verwenden, scheint es, dass Sie die folgenden beiden Möglichkeiten haben, lon und lat zu extrahieren. 'spdf @ data', das ist ein Datenrahmen oder' spdf @ coords', was eine Matrix ist. – jazzurro

+0

@jazzurro, vorausgesetzt, Sie haben alle lat, lon einer Stadt kann man ein Grenz-Polygon ähnlich der http://stackoverflow.com/questions/40179966/boundary-polygon-of-lat-lon-collection produzieren Wenn nicht was andere Informationssätze sind erforderlich, um ein Grenzpolygon wie das in der Verbindung gesendete zu erstellen? – MHOOS

4

Mit

structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019, 
126.7328, 153.2439, 142.8673, 152.689), latitude = c(-7.4197, 
-4.7089, -6.7541, 4.7817, 2.1643, -5.65, 23.3882, -5.571)), .Names = c("longitude", "latitude"), class = "data.frame", row.names = c(NA, -8L)) 

Um zu SpatialPointsDataFrame zu konvertieren

coordinates(df) <- cbind(df$longitude , df$latitude) 

Wie darauf hingewiesen @jazzurro Sie wahrscheinlich brauchen ein CRS zu Ihrem räumliches Objekt zuweisen.

proj4string(df) = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0") 

der umgekehrte Prozess SpatialPointsDataFrame original df

df <- data.frame(longitude = coordinates(df)[,1], latitude = coordinates(df)[,2]) 
16

Oder mit sf statt sp Objekte (check out mehr über Simple Features for R oder Migration sp-sfhere):

require(sf) 
my.df <- read.table(text=" 
        longitude latitude 
        128.6979 -7.4197 
        153.0046 -4.7089 
        104.3261 -6.7541 
        124.9019 4.7817 
        126.7328 2.1643 
        153.2439 -5.6500 
        142.8673 23.3882 
        152.6890 -5.5710", 
        header=TRUE) 

my.sf.point <- st_as_sf(x = my.df, 
         coords = c("longitude", "latitude"), 
         crs = "+proj=longlat +datum=WGS84") 
# simple plot 
plot(my.sf.point) 
# interactive map: 
require(mapview) 
mapview(my.sf.point) 

# convert to sp object if needed 
my.sp.point <- as(my.sf.point, "Spatial") 

enter image description here