4

I have no idea how to, or even is possible in some way, but maybe there is an app, software or someone of you have any idea how to! Please note, that I have no skills in coding or programming.

I will travel around my own country's (Latvia) borders as close as possible, but before I do that, I need some data: min, max, and average distance between my route and border along the whole route.

I guess it could be two gpx tracks – one is my route, other country borders. It means that in every meter of my route the closest point to the border should be found and measured.

I guess I did make myself clear, picture for visual in the link: https://ibb.co/m6L3Z4c

Outcome could be in any format, as long as there is a result with data.

8
  • 1
    Welcome to GIS.SE. It is not clear what your final output should look like, and which data you already have. Can you provide examples? What software do you have access to? – Erik Jan 6 at 8:01
  • Do you want to track your path of Travel? – Yogesh Chavan Jan 6 at 8:13
  • Do you have a dataset like a road network with paths you can use? If not, would OpenStreetMap data be OK for you? Or do you intend also to walk through the wilderness where there are no paths? – Babel Jan 6 at 8:16
  • Thanks, Whole point of this is, that path by the border could not be more than 10km at any place and average should be below 5km. So I have a country borders and a gpx file with path. Output could be any kind. Ir could a csv file, or a graph, it doesn’t really matter, as long as I will get data. Software, actually none, so Im just trying to understand, can this be done with some open or at least low cost software. – Zvir Jan 6 at 8:22
  • I have already made a gpx file of my path – Zvir Jan 6 at 8:23
7

You can do this using QGIS and OpenStreetMap data, so OpenSource/Open data and free to use. The process includes several steps. Using the online QGIS documentation can help you for the steps. When you are stuck, post another question here.

Don't forget to critically reflect how accurate the data is (I propose OpenStreetMap) - however, I guess for the kind of analysis you want to do, this does not play a major role.

The steps involve:

  1. Get a polygon layer for Latvia - you can use Overpass Turbo for this as describede here: https://gis.stackexchange.com/a/368774/88814 or see the link at step 7 as you already find the data there to download.

So you could just skip steps 1 to 11 and get the data ready processed from the link. On the bottom of this anser, you find the min, max and mean value for the GPS track you uploaded. However, I included the steps here to demonstrate how you can do this yourself and get used to work in QGIS.

  1. Also get the coastline for the whole coast with the same method because the country polygon of Latvia includes also the territorial waters of Latvia.
  2. Clip the coastline to include only parts for Latvia (it should protrude a little bit where Latvia borders Estonia and Lithuania).
  3. Select all features and merge it to one line, use Merge Selected Features.
  4. Than use this line the cut the country polygon, using Split with lines.
  5. Delete the part of the polygon that is on the sea. You get a country polygon for Latvia.
  6. Save this Polygon (right click / export) and set the CRS to a local projected CRS. I suggest LKS_1992_Latvia_TM_0 (EPSG:102440). As I prepared the steps 1 to 7, you can find this as a Geopackage file latvia_land.gpkg here: https://drive.switch.ch/index.php/s/pwWZeN2AOG0Vax8
  7. Apply a buffer of 5 km to the layer from step 7 and save it, use Menu Vector / Geoprocessing Tools / Buffer.
  8. Repeat the step with a buffer of -5 km.
  9. Copy the output of step 9 and add it to the layer of step 8. This file can be found at the same URL as above as latvia_buffer_5km.gpkg
  10. Repeat the steps 8 to 10 with a buffer of 10 km - or even better: apply a buffer of 5 km to the united buffers of step 10. You get a buffer of 10km. Find this layer as latvia_buffer_10km.gpkg
  11. Now load your gpx track (red) to QGIS to see where it is inside the 5km (blue) and the 10 km (yellow) buffer. On the screenshot, I addea an OpenStreerMap basemap in the background:

enter image description here

If you zoom in, you see that for example in Riga, you left the 5km buffer (blue) as well as the 10km buffer (yellow).

enter image description here

If you want to calculate max, min and mean distance on your route from the border, you should create points at a regular distance on your path, let's say every meter (even though you get a huge amount of data: 1'932'477 points; maybe 10 or even 100 meters or 1 km would be enough). Thus, you continue as follows:

  1. Menu Processing / Toolbox / Points along geometry an set the distance as discussed before: 1m, 10m, 100m, 1 km or whatever you like.

  2. The output from step 13 is a point layer. You can now calculate for each point the distance to the border. For this, use the field calculator and add a new field with this expression. Be sure you have the Polygon layer from 6, Latvia without the sea, loaded in QGIS and named as Latvia - otherwise, replace Latvia in the following expression with the name the layer has in you project:

length (
    shortest_line( 
        $geometry, 
        boundary (
            geometry (
                get_feature_by_id (
                    'Latvia', 
                    1
                )
            )
        )
    )
)

This is how to do the calculation:

enter image description here

  1. In the attribute table, you now have a new field with the distance of each point on your route to the border and you can sort (max/min) and aggregate it (mean).

You can now click on the column header to sort the distance field and get the smallest and highest value on the top. With a right click on the field you can choose Zoom to Feature and the map will zoom to this point.

Thus, the point closest to the border (using 1 km interval) is at 57.97702976, 25.48272307, 6.93 m from the border with Estonia about 13 km to the northeast of Rūjiena. The farthest away from the border is at Krišjāņa Valdemāra iela in Riga, nearby a park (Neatkarības laukums) at 56.9528571, 24.1042171, 11.26 km from the border.

  1. You can now use Menu Vector / Analysis Tools / Basic statistics for fields and in the dialog set the Input layer to the interpolated points (interval 1km or whatever you chose) and as Field to calculate statistics on choose the distance we calculated in step 14:

enter image description here

You get an output in html format (open it in a web browser) that has some information that also tells you that the mean distance from the border is 1.906 km, median: 1.285 km, standard deviation: 1.83, see next screenshot. You find the whole html file at the same link: https://drive.switch.ch/index.php/s/pwWZeN2AOG0Vax8 - use google translate as my QGIS is in german.

enter image description here

I now run the whole process to calculate the statistics (steps 13 to 16) again, creating points at a 1 m distance. As there are more than a million points, calculation of the distance quite takes a while and it is better not to open the attribute table - you can very well use the field calculater directly, without opening the attribute table. Also rendering the points on the canvas may take a bit, so better uncheck the layer in the layer panel. To see the points closest and farthest away from the border, first right click on the layer and filter (see below, last screenshot).

You can see the difference between the 1000 m and 1 m interval datasets (the whole basic statistics sheet can be found again at the same link):

Closest point to the border: 0.1 m at 56.286329338, 21.488531447 (in fact, the border crossing from Latvia to Lithuania near Skuodas, thus technically, there must also be some point with 0 m distance to the border). This point is the only major difference to the result above with a point interval of 1 km.

Farthest point from the border: 11.29 km at 56.95512506, 24.10960105, the intersection of Kalpaka bulvāris and Krišjāņa Valdemāra iela in Riga

  • Mean distance: 1.904 km from the border

  • Median: 1.295 km

  • Standard deviation: 1.8

28 km

So you see, the values changed only for a few meters and did not significantly alter the result, apart from the closest point. In fact, there are several points very close to the border. You can check this by filtering the interpolated points: right click on the layer / Filter..., than define "dist_from_border" < 2 to only show points closer than 2 meters from the border: as you can see on the next screenshot, there are 7 regions where this is the case, four in the southwest (coast, border to Lithuania) and three in the north, on the border to Estonia:

enter image description here

2
  • Thanks for your answer, this seems really good step by step instructions. Will try, hopefully will succeed and get necessary data for me. Really, thanks a lot for your help. – Zvir Jan 6 at 10:34
  • 1
    Truly excellent answer. Well done! – Eric Duminil Jan 6 at 19:24
5

With QGIS you can use the following processing tools:

GPS route as input:

  • Densify by interval (add vertices every 1 m)
  • Extract vertices (create a point layer from the vertices)

With country border and extracted vertices (from above):

  • Align points to features (to get distance between each point and border)
  • Basic statistics for fields with distance field as input

enter image description here

2
  • Thanks for your help, seems that I will have to learn new software :) – Zvir Jan 6 at 10:35
  • Nice! You mean QGIS? (the model designer is not necessary, you can execute the tools manually) – BERA Jan 6 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.