Skip to content

Commit

Permalink
Fix GPX import when no internet
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjore committed Apr 10, 2022
1 parent 88942a6 commit d9fadd9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
49 changes: 34 additions & 15 deletions ImportGPX/ImportGPX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class Import
{
/**///Why do we need this as global variables
public static Android.App.Dialog dialog = null;
public static int progress = 0;
public static int progress = 0;
public static Google.Android.Material.TextView.MaterialTextView progressBarText2 = null;

public static ILayer GetRoute()
Expand All @@ -56,8 +56,8 @@ public static ILayer GetRoute()
if (gpxData == null)
return;
//Only ask if we have routes and/or tracks and/or Waypoints to import
if (gpxData.Routes.Count > 0 || gpxData.Tracks.Count > 0 || gpxData.Waypoints.Count > 0)
//Only ask if we have routes and/or tracks and/or Waypoints to import, and we have internet access. Most don't have a local tile server
if ((gpxData.Routes.Count > 0 || gpxData.Tracks.Count > 0 || gpxData.Waypoints.Count > 0) && Connectivity.NetworkAccess == Xamarin.Essentials.NetworkAccess.Internet)
{
//Does the user want maps downloaded for offline usage?
Show_Dialog msg1 = new Show_Dialog(MainActivity.mContext);
Expand Down Expand Up @@ -99,7 +99,7 @@ public static ILayer GetRoute()
return null;
}

public static void AddGPXWayPoint(wptType wptType, bool DownloadOfflineMap)
public static void AddGPXWayPoint(wptType wptType, bool DownloadOfflineMap)
{
try
{
Expand Down Expand Up @@ -417,7 +417,7 @@ public static void AddTracksToMap()
Serilog.Log.Error(ex, $"Import - AddTracksToMap()");
}
}

public static void AddPOIToMap()
{
try
Expand Down Expand Up @@ -573,7 +573,7 @@ public static (string, float, int, int, List<Position>) GPXtoRoute(rteType route
if (j >= 1)
{
var p1 = new Position((float)rtePteExt.rpt[j - 1].lat, (float)rtePteExt.rpt[j - 1].lon, 0);
p2 = new Position((float)rtePteExt.rpt[j].lat, (float)rtePteExt.rpt[j].lon,0);
p2 = new Position((float)rtePteExt.rpt[j].lat, (float)rtePteExt.rpt[j].lon, 0);
mapDistance_m += (float)p.CalculateDistance(p1, p2, DistanceType.Meters);
}
}
Expand All @@ -598,13 +598,17 @@ public static (string, float, int, int, List<Position>) GPXtoRoute(rteType route
//Convert the list to a string
string mapRoute = ConvertLatLonListToLineString(ListLatLon);

//Get elevation data
if (getAscentDescent)
//Get elevation data? (Requires internet access)
if (getAscentDescent && Connectivity.NetworkAccess == Xamarin.Essentials.NetworkAccess.Internet)
{
ListLatLon = GetElevationData(ListLatLon);
var a = CalculateAscentDescent(ListLatLon);
ascent = a.Item1;
descent = a.Item2;

if (ListLatLon != null)
{
var a = CalculateAscentDescent(ListLatLon);
ascent = a.Item1;
descent = a.Item2;
}
}

return (mapRoute, mapDistance_m, ascent, descent, ListLatLon);
Expand Down Expand Up @@ -666,6 +670,11 @@ private static List<Position> GetElevationData(List<Position> ListLatLon)
}

var a = DownloadElevationData(LatLon);

//Do we have elevation datae?
if (a == null)
return ListLatLon;

ElevationData = ElevationData.Concat(a).ToList();
LatLon = String.Empty;
}
Expand All @@ -682,6 +691,11 @@ private static List<Position> GetElevationData(List<Position> ListLatLon)
if (LatLon != String.Empty)
{
var a = DownloadElevationData(LatLon);

//Do we have elevation datae?
if (a == null)
return ListLatLon;

ElevationData = ElevationData.Concat(a).ToList();
}

Expand All @@ -692,20 +706,24 @@ private static List<Position> GetElevationData(List<Position> ListLatLon)
Log.Error(ex, $"Import - GetElevationData()");
}

return null;
return ListLatLon;
}

private static List<Position> DownloadElevationData(string LatLon)
{
var Pre = $"https://api.opentopodata.org/v1/aster30m?locations=";
var Post = $"&interpolation=bilinear&nodata_value=null";
List <Position> ElevationData = new List<Position>();
List<Position> ElevationData = new List<Position>();

try
{
//Get the data
var eleJSON = DownloadElevationDataAsync(Pre + LatLon + Post);

//Do we have elevation data?
if (eleJSON == null)
return null;

//Convert from string to JSON
Models.Elevation.ElevationData elevationData = JsonSerializer.Deserialize<Models.Elevation.ElevationData>(eleJSON);

Expand Down Expand Up @@ -822,7 +840,7 @@ private static async Task<GpxClass> PickAndParse()
}

GpxClass gpx = GpxClass.FromXml(contents);
var bounds = gpx.GetBounds();
var bounds = gpx.GetBounds();

string r = (gpx.Routes.Count == 1) ? "route" : "routes";
string t = (gpx.Tracks.Count == 1) ? "track" : "tracks";
Expand All @@ -846,7 +864,8 @@ public static ILayer CreateRouteLayer(string strRoute, IStyle style = null)
{
var features = new Features();

try {
try
{
var GPSlineString = (LineString)Geometry.GeomFromText(strRoute);

//Lines between each waypoint
Expand Down
1 change: 1 addition & 0 deletions Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
Expand Down
12 changes: 12 additions & 0 deletions hajk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hajk", "hajk.csproj", "{DA2
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GPXTest", "..\GPXTest\GPXTest\GPXTest.csproj", "{A5EE7455-9A2D-4BC7-9F19-D002D99309E4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeoTiffCOGConsole", "Libraries\GeoTiffCOG\GeoTiffCOGConsole\GeoTiffCOGConsole.csproj", "{CF1DD1D1-29E6-455B-8382-46494C0DFE3E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeoTiffCOG", "Libraries\GeoTiffCOG\GeoTiffCOG\GeoTiffCOG.csproj", "{AFA8AAEF-79C9-49BA-96F7-31E8079A20E5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -23,6 +27,14 @@ Global
{A5EE7455-9A2D-4BC7-9F19-D002D99309E4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A5EE7455-9A2D-4BC7-9F19-D002D99309E4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A5EE7455-9A2D-4BC7-9F19-D002D99309E4}.Release|Any CPU.Build.0 = Release|Any CPU
{CF1DD1D1-29E6-455B-8382-46494C0DFE3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF1DD1D1-29E6-455B-8382-46494C0DFE3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF1DD1D1-29E6-455B-8382-46494C0DFE3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF1DD1D1-29E6-455B-8382-46494C0DFE3E}.Release|Any CPU.Build.0 = Release|Any CPU
{AFA8AAEF-79C9-49BA-96F7-31E8079A20E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AFA8AAEF-79C9-49BA-96F7-31E8079A20E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AFA8AAEF-79C9-49BA-96F7-31E8079A20E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AFA8AAEF-79C9-49BA-96F7-31E8079A20E5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit d9fadd9

Please sign in to comment.