Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP KML import feature #1279

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions starsky/starsky.feature.geolookup/Services/GeoCli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task CommandLineAsync(string[] args)
{
inputPath = _appSettings.DatabasePathToFilePath(
ArgsHelper.GetSubPathFormArgs(args)
);
)!;
}
else
{
Expand All @@ -105,7 +105,7 @@ public async Task CommandLineAsync(string[] args)
var dateTime = DateTime.Now.AddDays(( double ) getSubPathRelative);
inputPath = _appSettings.DatabasePathToFilePath(
new StructureService(_iStorage, _appSettings.Structure)
.ParseSubfolders(dateTime),false);
.ParseSubfolders(dateTime),false)!;
}

// used in this session to find the files back
Expand Down
4 changes: 2 additions & 2 deletions starsky/starsky.feature.trash/Services/MoveToTrashService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ await _metaUpdateService.UpdateAsync(changedFileIndexItemName,
return (moveToTrash,changedFileIndexItemName);
}

internal async Task SystemTrashInQueue(List<FileIndexItem> moveToTrash)
private async Task SystemTrashInQueue(List<FileIndexItem> moveToTrash)
{
var fullFilePaths = moveToTrash
.Where(p => p.FilePath != null)
.Select(p => _appSettings.DatabasePathToFilePath(p.FilePath, false))
.Select(p => _appSettings.DatabasePathToFilePath(p.FilePath!, false)).Cast<string>()
.ToList();
_systemTrashService.Trash(fullFilePaths);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ internal string GetRoleAddToUser(string identifier, User user)
return AccountRoles.AppAccountRoles.Administrator.ToString();
}

if ( _appSettings.AccountRolesByEmailRegisterOverwrite
.TryGetValue(identifier, out var emailsForConfig) &&
AccountRoles.GetAllRoles().Contains(emailsForConfig) )
if ( _appSettings.AccountRolesByEmailRegisterOverwrite?
.TryGetValue(identifier, out var emailsForConfig) == true &&
AccountRoles.GetAllRoles().Contains(emailsForConfig) )
{
return emailsForConfig;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public StatusCodesHelper(AppSettings appSettings)

public FileIndexItem.ExifStatus IsReadOnlyStatus(FileIndexItem fileIndexItem)
{
if (fileIndexItem.IsDirectory == true && _appSettings.IsReadOnly(fileIndexItem.FilePath))
if (fileIndexItem.IsDirectory == true && _appSettings.IsReadOnly(fileIndexItem.FilePath!))
{
return FileIndexItem.ExifStatus.DirReadOnly;
}

if ( _appSettings.IsReadOnly(fileIndexItem.ParentDirectory) )
if ( _appSettings.IsReadOnly(fileIndexItem.ParentDirectory!) )
{
return FileIndexItem.ExifStatus.ReadOnly;
}
Expand All @@ -43,7 +43,7 @@ public FileIndexItem.ExifStatus IsReadOnlyStatus(DetailView? detailView)
return FileIndexItem.ExifStatus.DirReadOnly;
}

if ( _appSettings.IsReadOnly(detailView.FileIndexItem?.ParentDirectory) )
if ( _appSettings.IsReadOnly(detailView.FileIndexItem?.ParentDirectory!) )
{
return FileIndexItem.ExifStatus.ReadOnly;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class TelemetryConfigurationHelper
return null;
}

public static TelemetryClient? InitTelemetryClient(string appInsightsConnectionString, string roleName, IWebLogger? logger, TelemetryClient? telemetryClient)
public static TelemetryClient? InitTelemetryClient(string? appInsightsConnectionString, string roleName, IWebLogger? logger, TelemetryClient? telemetryClient)
{
try
{
Expand Down Expand Up @@ -50,7 +50,7 @@ public static class TelemetryConfigurationHelper
}
}

private static TelemetryConfiguration? CreateTelemetryConfiguration(string appInsightsConnectionString)
private static TelemetryConfiguration? CreateTelemetryConfiguration(string? appInsightsConnectionString)
{
var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
telemetryConfiguration.ConnectionString = appInsightsConnectionString;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using starsky.foundation.georealtime.Models.GeoJson;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace starsky.foundation.georealtime.Converter;
public class GeometryBaseModelConverter : JsonConverter<GeometryBaseModel>
{
public override GeometryBaseModel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using var doc = JsonDocument.ParseValue(ref reader);
var root = doc.RootElement;

if ( !root.TryGetProperty("type", out var typeElement) ||
!root.TryGetProperty("coordinates", out var coordinates) ||
coordinates.ValueKind == JsonValueKind.Null ||
coordinates.ValueKind == JsonValueKind.Number )
{
throw new JsonException("Invalid JSON for GeometryBaseModel");
}

var geometryType = typeElement.GetString();
var coordinatesRawText = coordinates.GetRawText();

switch (geometryType)
{
case "Point":
return new GeometryPointModel()
{
Coordinates =
JsonSerializer.Deserialize<List<double>>(
coordinatesRawText)
};
case "LineString":
return new GeometryLineStringModel()
{
Coordinates =
JsonSerializer.Deserialize<List<List<double>>>(
coordinatesRawText)
};
}

throw new JsonException("Invalid JSON for GeometryBaseModel");
}

public override void Write(Utf8JsonWriter writer, GeometryBaseModel value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value, value.GetType(), options);
}
}

60 changes: 60 additions & 0 deletions starsky/starsky.foundation.georealtime/Helpers/DefaultGeoJson.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using starsky.foundation.georealtime.Models.GeoJson;

namespace starsky.foundation.georealtime.Helpers;

public static class DefaultGeoJson
{
public static FeatureCollectionModel CreateDefaultGeoJson(List<List<double>> coordinates, GeometryType geometryType = GeometryType.LineString)
{

switch ( geometryType )
{
case GeometryType.Point:
{
return new FeatureCollectionModel
{
Type = FeatureCollectionType.FeatureCollection,
Features = new List<FeatureModel>
{
new FeatureModel
{
Type = FeatureType.Feature,
Geometry = new GeometryPointModel()
{
Coordinates = coordinates.FirstOrDefault() ?? new List<double>()
},
Properties = new PropertiesModel
{
}
}
}
};
}
case GeometryType.LineString:
{
return new FeatureCollectionModel
{
Type = FeatureCollectionType.FeatureCollection,
Features = new List<FeatureModel>
{
new FeatureModel
{
Type = FeatureType.Feature,
Geometry = new GeometryLineStringModel()
{
Coordinates = coordinates
},
Properties = new PropertiesModel()
}
}
};

}
default:
throw new ArgumentOutOfRangeException(nameof(geometryType), geometryType, null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using starsky.foundation.georealtime.Models;
using starsky.foundation.georealtime.Models.GeoJson;

namespace starsky.foundation.georealtime.Helpers;

public static class IntermediateModelConverter
{
public static FeatureCollectionModel Covert2GeoJson(
List<LatitudeLongitudeAltDateTimeModel> inputModel, bool addPoints = true)
{

// todo add splliter by day - inactive

var featureCollection = ConvertLineGeoJson(inputModel, new FeatureCollectionModel
{
Type = FeatureCollectionType.FeatureCollection,
Features = new List<FeatureModel>()
});

return !addPoints ? featureCollection : ConvertPointsGeoJson(inputModel, featureCollection);
}

private static FeatureCollectionModel ConvertLineGeoJson(
List<LatitudeLongitudeAltDateTimeModel> inputModel,
FeatureCollectionModel featureCollection)
{
var lineStringModel = new FeatureModel
{
Type = FeatureType.Feature,
Geometry = new GeometryLineStringModel()
{
Type = GeometryType.LineString,
Coordinates =
new List<List<double>>()
},
Properties = new PropertiesModel
{
DateTime = inputModel.FirstOrDefault()?.DateTime ?? DateTime.UtcNow
}
};

foreach ( var model in inputModel )
{
(lineStringModel.Geometry as GeometryLineStringModel)!.Coordinates!.Add(new List<double>
{
model.Latitude,
model.Longitude,
model.Altitude ?? 0
});
}

featureCollection.Features.Add(lineStringModel);

return featureCollection;
}

private static FeatureCollectionModel ConvertPointsGeoJson(List<LatitudeLongitudeAltDateTimeModel> inputModel, FeatureCollectionModel featureCollection)
{
foreach ( var model in inputModel )
{
featureCollection.Features.Add(new FeatureModel
{
Type = FeatureType.Feature,
Geometry = new GeometryPointModel()
{
Type = GeometryType.Point,
Coordinates =
new List<double>
{
model.Latitude,
model.Longitude,
model.Altitude ?? 0
}
},
Properties = new PropertiesModel
{
DateTime = model.DateTime
}
});
}

return featureCollection;
}

public static string ConvertToGpx(List<LatitudeLongitudeAltDateTimeModel> waypoints)
{
var settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true,
Encoding = Encoding.UTF8
};

using var stringWriter = new StringWriter();
using var xmlWriter = XmlWriter.Create(stringWriter, settings);

// Modify the encoding attribute in the XML declaration
xmlWriter.WriteRaw($"<?xml version=\"1.0\" encoding=\"{settings.Encoding.WebName}\"?>");

xmlWriter.WriteStartElement("gpx", "http://www.topografix.com/GPX/1/1");
xmlWriter.WriteAttributeString("version", "1.1");
xmlWriter.WriteAttributeString("creator", $"Starsky {Assembly.GetExecutingAssembly().GetName().Version?.ToString()}");

foreach (var waypoint in waypoints)
{
xmlWriter.WriteStartElement("wpt");
xmlWriter.WriteAttributeString("lat", waypoint.Latitude.ToString(CultureInfo.InvariantCulture));
xmlWriter.WriteAttributeString("lon", waypoint.Longitude.ToString(CultureInfo.InvariantCulture));

if (waypoint.Altitude.HasValue)
{
xmlWriter.WriteElementString("ele", waypoint.Altitude.Value.ToString(CultureInfo.InvariantCulture));
}

xmlWriter.WriteElementString("time", waypoint.DateTime.ToString("yyyy-MM-ddTHH:mm:ssZ"));

xmlWriter.WriteEndElement(); // Close wpt element
}

xmlWriter.WriteEndElement(); // Close gpx element
xmlWriter.Flush();

return stringWriter.ToString();
}
}
Loading
Loading