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:
- 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.
- 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.
- Clip the coastline to include only parts for Latvia (it should protrude a little bit where Latvia borders Estonia and Lithuania).
- Select all features and merge it to one line, use
Merge Selected Features
.
- Than use this line the cut the country polygon, using Split with lines.
- Delete the part of the polygon that is on the sea. You get a country polygon for Latvia.
- 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
- Apply a buffer of 5 km to the layer from step 7 and save it, use
Menu Vector / Geoprocessing Tools / Buffer
.
- Repeat the step with a buffer of -5 km.
- 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
- 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
- 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:
If you zoom in, you see that for example in Riga, you left the 5km buffer (blue) as well as the 10km buffer (yellow).
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:
Menu Processing / Toolbox / Points along geometry
an set the distance as discussed before: 1m, 10m, 100m, 1 km or whatever you like.
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:
- 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.
- 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:
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.
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
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: