{"id":551,"date":"2021-04-23T10:32:49","date_gmt":"2021-04-23T17:32:49","guid":{"rendered":"https:\/\/blogs.ubc.ca\/tdeenik\/?p=551"},"modified":"2021-05-11T06:33:29","modified_gmt":"2021-05-11T13:33:29","slug":"a-quick-lesson-on-projections","status":"publish","type":"post","link":"https:\/\/blogs.ubc.ca\/tdeenik\/2021\/04\/23\/a-quick-lesson-on-projections\/","title":{"rendered":"A quick lesson on projections"},"content":{"rendered":"<p style=\"text-align: center;\"><span style=\"text-decoration: underline;\"><strong>Geographic coordinate system vs\u00a0 projected coordinate system <\/strong><strong>(La<\/strong><strong>t long vs UTM)<\/strong><\/span><\/p>\n<p style=\"text-align: center;\">Both vector (point, line, polygon) and raster (pixels) data rely on x, y values to situate the data in 2D space to represent where it is on the earth&#8217;s surface. All coordinate systems establish a starting point on the earth (the origin) and measures from there.<\/p>\n<p style=\"text-align: center;\">There are different <strong>datums\u00a0<\/strong>used to model the shape of the earth. It consists of a series of numbers that define the shape and size of the ellipsoid and it&#8217;s orientation in space. There are a large number of datums in use for various areas such as:\u00a0 The WGS84 datum, which is almost identical to the NAD83 datum used in North America and the ETRS89 datum used in Europe. You must select a datum that is best suited for your geographic location to give the best possible fit.<\/p>\n<p style=\"text-align: center;\"><strong>Map projections<\/strong> are mathematical equations used to flatten spherical objects. The projection formulas allow us to convert from lat\/long coordinates (spherical system) to planar x, y coordinates (i.e. UTM). Lat\/long coordinates are stored in <strong>degrees<\/strong> vs UTM the coordinates are stored in <strong>meters<\/strong>. Projected coordinate systems (PCS) are often better suited for large-scale applications because distances are often in meters. They are especially good for raster, because the pixel sizes are in meters rather than decimal degrees.<\/p>\n<p style=\"text-align: center;\">The <strong>Universal Transverse Mercator<\/strong> (UTM) projection uses a grid defined by Easting (vertical lines referenced by a zone\u2019s central meridian) and Northings (horizontal lines measures from the equator) to locate coordinate pairs. The UTM system divides the world into 60 N-S zones, each representing 6\u00b0 (e.g.,<br \/>\n360\u00b0\/60 = zone width of 6\u00b0 longitude). The zones are numbered from 1 to 60, starting at 180\u00b0 W and ending at 180\u00b0 E. In BC we are covered by UTM zone 8, 9, 10 and 11. The Okanagan occurs mostly in UTM zone 11, with a small portion in 10. Example of UTM coordinates: 11U 328058mE, 5534668mN.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-554 aligncenter\" src=\"https:\/\/blogs.ubc.ca\/tdeenik\/files\/2021\/04\/utm_figure2_en-300x247.gif\" alt=\"\" width=\"438\" height=\"361\" srcset=\"https:\/\/blogs.ubc.ca\/tdeenik\/files\/2021\/04\/utm_figure2_en-300x247.gif 300w, https:\/\/blogs.ubc.ca\/tdeenik\/files\/2021\/04\/utm_figure2_en-768x631.gif 768w, https:\/\/blogs.ubc.ca\/tdeenik\/files\/2021\/04\/utm_figure2_en-100x82.gif 100w, https:\/\/blogs.ubc.ca\/tdeenik\/files\/2021\/04\/utm_figure2_en-150x123.gif 150w, https:\/\/blogs.ubc.ca\/tdeenik\/files\/2021\/04\/utm_figure2_en-200x164.gif 200w, https:\/\/blogs.ubc.ca\/tdeenik\/files\/2021\/04\/utm_figure2_en-450x370.gif 450w, https:\/\/blogs.ubc.ca\/tdeenik\/files\/2021\/04\/utm_figure2_en-600x493.gif 600w, https:\/\/blogs.ubc.ca\/tdeenik\/files\/2021\/04\/utm_figure2_en-900x740.gif 900w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/p>\n<p style=\"text-align: center;\">When you are &#8216;projecting&#8217; from a <strong>geographic<\/strong> to a <strong>projected<\/strong> coordinate systems (or vice versa) you are actually applying a mathematical transformation to the data to convert the coordinates from degrees to meters. This will ALWAYS distort the data slightly, which is why for a raster every time we project the data we also resample it slightly because the pixels are being shifted.<\/p>\n<div>\n<p style=\"text-align: center;\">In <strong>R<\/strong>, things work a bit different. When you run: <span style=\"color: #ff6600;\">crs(raster)<\/span>, or any vector, it will return the proj4 string that mathematically defines how the coordinates are stored with the data. This is a proj4 string: &#8220;<span style=\"color: #ff6600;\">+proj=longlat +datum=WGS84 +no_defs<\/span>&#8220;. It tells me my data are in a longlat (so GCS) projection that uses the World Geodetic Survey of 1984 survey (the standard for most GPS). For a raster in R you could transform your data with either <span style=\"color: #ff6600;\">projectRaster<\/span> (can be slow) or <span style=\"color: #ff6600;\">gdal_warp<\/span> (fast).<\/p>\n<div>\n<p style=\"text-align: center;\">You need to tell the function what new projection you want the data in and it will do all the math for you. Let&#8217;s say you have sentinel data &#8216;r&#8217; that are in longlat:<span style=\"color: #ff6600;\">r_proj = projectRaster(r, res = 10, crs = the proj4 string for UTM, method = &#8216;ngb&#8217;)<\/span>What this is doing is setting the pixel size (res) as 10, and you are defining the new crs. The easiest way to get the proj4 string for UTM zone 11 is to call in a layer that already has the correct crs and copy that (you can also google which one is best for your area). It&#8217;s the same idea for vector. If you have a shapefile that isn&#8217;t in the right projection and it is an sf object:<\/p>\n<p style=\"text-align: center;\"><span style=\"color: #ff6600;\">vect_proj = st_transform (old, crs = new proj4 string)<\/span><\/p>\n<div data-tid=\"messageBodyContent\">\n<p style=\"text-align: center;\">With all of these, you must write them to file <span style=\"color: #ff6600;\">(writeraster)<\/span> after you re-project. You don&#8217;t want to be projecting every time because it can take a while.<\/p>\n<p style=\"text-align: center;\">If you are trying to stack a bunch of raster layers, it is important that they have the same <strong>extent <\/strong>(min max xy coords). You can check this using <span style=\"color: #ff6600;\">extent(raster layer)<\/span>. If they are different you can actually set the extent when you project the raster. In <span style=\"color: #ff6600;\">projectRaster<\/span> there is a &#8216;to&#8217; argument. That allows you to project a sentinel raster for example &#8216;to&#8217; the pixels of the other raster.<\/p>\n<p style=\"text-align: center;\">\u200b<span style=\"color: #ff6600;\">r_targ = raster(grid metric)<\/span><\/p>\n<\/div>\n<p style=\"text-align: center;\"><span style=\"color: #ff6600;\">r_proj = projectRaster(sentinel raster, to = r_targ, crs = crs(r_targ))<\/span><\/p>\n<div>\n<div>Note: If you have a bunch of rasters that are all the same extent\/projection, and you need to change them all at once, you can stack it first then apply <span style=\"color: #ff6600;\">projectRaster<\/span> you need to on the stack.<\/div>\n<div style=\"text-align: center;\" data-tid=\"urlPreviewDomain\">\n<div data-tid=\"appBasedLinkUrlPreview\">\n<div style=\"text-align: center;\">\n<p>This post was adapted from a lesson given by my supervisor.<\/p>\n<div style=\"text-align: center;\">\n<p style=\"text-align: center;\">Useful documents include:<\/p>\n<p><a href=\"https:\/\/github.com\/walkerke\/crsuggest\">GitHub &#8211; walkerke\/crsuggest: Get appropriate CRS suggestions for your spatial data in R<\/a><\/p>\n<div>\n<p style=\"text-align: center;\"><a tabindex=\"-1\" title=\"https:\/\/www.rdocumentation.org\/packages\/raster\/versions\/3.4-5\/topics\/projectraster\" href=\"https:\/\/www.rdocumentation.org\/packages\/raster\/versions\/3.4-5\/topics\/projectRaster\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.rdocumentation.org\/packages\/raster\/versions\/3.4-5\/topics\/projectRaster<\/a><\/p>\n<p style=\"text-align: center;\"><a title=\"https:\/\/geocompr.robinlovelace.net\/reproj-geo-data.html\" href=\"https:\/\/geocompr.robinlovelace.net\/reproj-geo-data.html\">https:\/\/geocompr.robinlovelace.net\/reproj-geo-data.html<br \/>\n<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Geographic coordinate system vs\u00a0 projected coordinate system (Lat long vs UTM) Both vector (point, line, polygon) and raster (pixels) data rely on x, y values to situate the data in 2D space to represent where it is on the earth&#8217;s<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/blogs.ubc.ca\/tdeenik\/2021\/04\/23\/a-quick-lesson-on-projections\/\">Read more &#8250;<\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":70819,"featured_media":556,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-551","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blogs"],"_links":{"self":[{"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/posts\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/users\/70819"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/comments?post=551"}],"version-history":[{"count":8,"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/posts\/551\/revisions"}],"predecessor-version":[{"id":589,"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/posts\/551\/revisions\/589"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/media\/556"}],"wp:attachment":[{"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/media?parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/categories?post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.ubc.ca\/tdeenik\/wp-json\/wp\/v2\/tags?post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}