You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This episode will review how to import spatial points stored in `.csv` (Comma Separated Value) format into R as an `sf` spatial object. We will also reproject data imported from a shapefile format, export this data as a shapefile, and plot raster and vector data as layers in the same plot.
52
+
This episode will review how to import spatial points stored in `.csv` (Comma
53
+
Separated Value) format into R as an `sf` spatial object. We will also
54
+
reproject data imported from a shapefile format, export this data as a
55
+
shapefile, and plot raster and vector data as layers in the same plot.
53
56
54
57
## Spatial Data in Text Format
55
58
@@ -65,17 +68,18 @@ We would like to:
65
68
66
69
Spatial data are sometimes stored in a text file format (`.txt` or `.csv`). If
67
70
the text file has an associated `x` and `y` location column, then we can
68
-
convert it into an `sf` spatial object. The `sf` object allows us to store both the `x,y` values that represent the coordinate location
69
-
of each point and the associated attribute data - or columns describing each
70
-
feature in the spatial object.
71
+
convert it into an `sf` spatial object. The `sf` object allows us to store both
72
+
the `x,y` values that represent the coordinate location of each point and the
73
+
associated attribute data - or columns describing each feature in the spatial
74
+
object.
71
75
72
-
We will continue using the `sf` and `raster` packages in this episode.
76
+
We will continue using the `sf` and `terra` packages in this episode.
73
77
74
78
## Import .csv
75
79
76
-
To begin let's import a `.csv` file that contains plot coordinate `x, y`
77
-
locations at the NEON Harvard Forest Field Site (`HARV_PlotLocations.csv`) and look at the structure of
78
-
that new object:
80
+
To begin let's import a `.csv` file that contains plot coordinate `x, y`
81
+
locations at the NEON Harvard Forest Field Site (`HARV_PlotLocations.csv`) and
82
+
look at the structure of that new object:
79
83
80
84
```{r read-csv}
81
85
plot_locations_HARV <-
@@ -84,7 +88,11 @@ plot_locations_HARV <-
84
88
str(plot_locations_HARV)
85
89
```
86
90
87
-
We now have a data frame that contains 21 locations (rows) and 16 variables (attributes). Note that all of our character data was imported into R as factor (categorical) data. Next, let's explore the dataframe to determine whether it contains columns with coordinate values. If we are lucky, our `.csv` will contain columns labeled:
91
+
We now have a data frame that contains 21 locations (rows) and 16 variables
92
+
(attributes). Note that all of our character data was imported into R as
93
+
character (text) data. Next, let's explore the dataframe to determine whether
94
+
it contains columns with coordinate values. If we are lucky, our `.csv` will
95
+
contain columns labeled:
88
96
89
97
- "X" and "Y" OR
90
98
- Latitude and Longitude OR
@@ -98,18 +106,19 @@ names(plot_locations_HARV)
98
106
99
107
## Identify X,Y Location Columns
100
108
101
-
Our column names include several fields that might contain spatial information. The `plot_locations_HARV$easting`
102
-
and `plot_locations_HARV$northing` columns contain coordinate values. We can confirm
103
-
this by looking at the first six rows of our data.
109
+
Our column names include several fields that might contain spatial information.
110
+
The `plot_locations_HARV$easting` and `plot_locations_HARV$northing` columns
111
+
contain coordinate values. We can confirm this by looking at the first six rows
112
+
of our data.
104
113
105
114
```{r check-out-coordinates}
106
115
head(plot_locations_HARV$easting)
107
116
head(plot_locations_HARV$northing)
108
117
```
109
118
110
-
We have coordinate values in our data frame. In order to convert our
111
-
data frame to an `sf` object, we also need to know the CRS
112
-
associated with those coordinate values.
119
+
We have coordinate values in our data frame. In order to convert our data frame
120
+
to an `sf` object, we also need to know the CRS associated with those
121
+
coordinate values.
113
122
114
123
There are several ways to figure out the CRS of spatial data in text format.
115
124
@@ -119,8 +128,8 @@ There are several ways to figure out the CRS of spatial data in text format.
119
128
file header or somewhere in the data columns.
120
129
121
130
Following the `easting` and `northing` columns, there is a `geodeticDa` and a
122
-
`utmZone` column. These appear to contain CRS information
123
-
(`datum` and `projection`). Let's view those next.
131
+
`utmZone` column. These appear to contain CRS information (`datum` and
132
+
`projection`). Let's view those next.
124
133
125
134
```{r view-CRS-info}
126
135
head(plot_locations_HARV$geodeticDa)
@@ -140,23 +149,27 @@ we learned about the components of a `proj4` string. We have everything we need
140
149
to assign a CRS to our data frame.
141
150
142
151
To create the `proj4` associated with UTM Zone 18 WGS84 we can look up the
143
-
projection on the [Spatial Reference website](https://www.spatialreference.org/ref/epsg/wgs-84-utm-zone-18n/), which contains a list of CRS formats for each projection. From here, we can extract the [proj4 string for UTM Zone 18N WGS84](https://www.spatialreference.org/ref/epsg/wgs-84-utm-zone-18n/proj4/).
0 commit comments