Read a Csv Into a Distance Matrix in R

There are two ways to create a distance matrix for actel: manually, or automatically. While the transmission way is more than decumbent to errors and can be tedious, the automatic way can take some time and requires some GIS knowledge. Information technology is up to y'all to determine which style to use:

The manual way

The manual way is elementary. In your R session, move to the folder where you have your spatial file, and run the command emptyMatrix. This will read your spatial information and create a matrix that is in accordance with it. It is important that you lot create your template this fashion, every bit the cavalcade and row names of the altitude matrix are essential for actel to recognise the data. Below is an example of a distances matrix created past emptyMatrix.

St.1 St.2 St.3 St.4 St.v St.6 Release
St.1 0
St.2 0
St.3 0
St.four 0
St.5 0
St.6 0
Release 0

You can find the correspondence betwixt the standard names (e.k. "St.1", "St.2") and your receivers by running loadSpatial() (encounter the 'Standard.name' cavalcade).

Now, you can either fill in the matrix directly in R or salvage it and edit information technology a spreadsheet editor (brand sure to save the row names too!). To simplify the task, you tin fill in the upper diagonal-half of the matrix, reload into R if you used an external editor, and then run completeMatrix on the edited object.

Here is how it works:

  1. Fill in the elevation part of the matrix:
St.1 St.two St.3 St.4 St.5 St.six Release
St.1 0 1366 3417 6912 8864 9273 2230
St.2 0 2051 5546 7498 7906 3570
St.3 0 3528 5480 5889 5621
St.4 0 1963 2372 9116
St.v 0 409 11068
St.6 0 11476
Release 0
  1. Run completeMatrix() on the half-filled matrix:
St.i St.2 St.3 St.four St.5 St.6 Release
St.1 0 1366 3417 6912 8864 9273 2230
St.2 1366 0 2051 5546 7498 7906 3570
St.iii 3417 2051 0 3528 5480 5889 5621
St.4 6912 5546 3528 0 1963 2372 9116
St.v 8864 7498 5480 1963 0 409 11068
St.6 9273 7906 5889 2372 409 0 11476
Release 2230 3570 5621 9116 11068 11476 0
  1. Relieve the complete matrix as 'distances.csv' in the binder that has the remaining actel data.
Annotation:
The distance values must be in metres!

The automated way

As your studies commencement having more and more receivers, it might get complicated to detect and write down the distances betwixt every unmarried combination of receivers and release sites. To avoid doing this manually, you lot can get R to do it for you. To become R to compute the distances for you, you will need:

  1. A shapefile with a land polygon of your written report area.
  2. The coordinates of your receivers and release sites in the same coordinate organization as the shapefile.

If y'all are not familiar with shapefiles and GIS, it might be a expert idea to ask for a colleague's help.

Preparing the shapefile

To begin, yous need to create a shapefile that extends over all your receivers and release sites. This shapefile is expected to contain polygons of the land masses, which means that your rivers, lakes, fjords and any water bodies must be "empty space". The width of your water channels is important, equally the shapefile will be converted into a raster after on (i.e. a epitome made of pixels). If your pixels represent squares of 30 by 30 metres, and your channel width originally was of five metres, then the river path will be lost during the conversion process. Endeavour to ensure, equally best as possible, that the corners of your shapefile are aligned (i.e. the shape as a rectangular/quadrangular course).

Annotation:
Make sure that the shapefile is in the aforementioned coordinate system every bit the points in your spatial file!

Below is an instance of the study expanse in the left, and the respective shapefile in the correct:

drawing drawing

Once you have your shapefile set up, copy the shapefile and respective auxiliary files to the folder where y'all accept your 'spatial.csv'. Do brand sure to copy the auxiliary files, as they incorporate data regarding the coordinate arrangement of the shapefile!

Preparing the spatial.csv file

In your spatial file, you must include ii columns with the coordinates for your receiver stations and release sites. These coordinates must be in the same coordinate organization every bit the shapefile you just created before. Later on, yous will need to know the cavalcade names of the columns that contain the X and Y coordinates.

Importing the shapefile and preparing it

Now that both your shapefile and your spatial.csv file are ready, lets get dorsum to R. The first affair you need to exercise is import your shapefile and convert to a raster. I have created a role that tin perform these steps for you, called loadShape.

  1. The shape is the proper name of your shapefile, including the ".shp" extension.

  2. The size determines the size of the raster'southward pixels.

    For case, if size = 20, and then each pixel volition have twenty by twenty meters.

    Keep in mind that your pixel size must allow R to notwithstanding capture the presence of your river channels. If the river channels are too slim, yous can become dorsum to your shapefile and artificially overstate them.

  3. The coord.x and coord.y allow actel to search your spatial.csv file and match the station positions with the extent of the shapefile. Write the names of the columns containing the ten and y coordinates in these ii arguments to activate these checks.

  4. The buffer allows yous to request an expansion of the shapefile limits (tin come in handy to ensure your stations are not cut-off from each other).

Once the shapefile is successfully loaded, yous can have a quick look at the resulting raster past running raster::plot(); i.e.:

This should show you lot a plot where only the h2o areas are painted. Some h2o may prove up around the edges of the original shapefile due to the range adjustments made past loadShape. Converting a shapefile into a raster tin can accept a while, specially if the original shapefile is very big and the pixel size is depression.

Creating a transition layer

A transition layer is an object that allows R to estimate least toll paths between two locations. Using the raster generated above, creating a transition layer is straight forward:

  1. The directions determine the complication of R's distance tracking techniques. Information technology can take one of four values: iv, viii or xvi, defaulting to the latter.

    • If directions = 4, the animal tin can simply in the management of the fundamental winds (North, South, East and West).
    • If directions = 8, the brute can also motion in a intercardinal current of air management (Northeast, Northwest, Southeast and Southwest).
    • If directions = 16, then the animal can likewise move in the eight half-winds' directions.

    Increasing the directions improves the distance estimation fidelity, but also increases ciphering time.

Transition layers can take a long time to compute, especially for larger rasters, so you may take to bear with your computer for a bit here. To avert having to go through this procedure multiple times, it may exist a proficient idea to salve the output of loadShape and transitionLayer in a carve up RData file, for later apply.

Creating the distances matrix

Once you have your transition layer, yous can use it to calculate multiple distances matrices, as long equally the target objects fall within the extent of the original shapefile, and all targets are inside water. To create your distances matrix, yous must run distancesMatrix in the same working directory as your 'spatial.csv' file. This will allow it you automatically load the spatial file into the office environs.

The t.layer is the transition layer you lot just calculated. Fill in the column names of the 10 and Y coordinates of your 'spatial.csv' file in coord.x and coord.y. When you run this command, actel will ask you if you want to salvage the output to a 'distances.csv' file. Alternatively, y'all can bank check the output within R and then save the matrix yourself!

Actel will automatically recognise the 'distances.csv' file and activate speed calculations in your next analysis!

Note:
It is of import that you check the results of this process by inspecting your distances matrix. If the distances exercise non look right, you may accept accidentally done something wrong in the steps above.

Here is a minimum instance:

Recall that y'all tin can find more information about each of these functions by visiting their help pages. You can access the assistance pages by running ?loadShape, ?transitionLayer or ?distancesMatrix.

Return to previous page

Dorsum to pinnacle.

tranmookedis.blogspot.com

Source: https://cran.r-project.org/package=actel/vignettes/a-2_distances_matrix.html

0 Response to "Read a Csv Into a Distance Matrix in R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel