using ProjNet.CoordinateSystems.Transformations; using ProjNet.CoordinateSystems; using ProjNet; using System.Collections.Generic; using System.Linq; using NetTopologySuite.Geometries; namespace gehGassi.Common.Data { /// /// Erweiterungen /// public static class Extensions { public static string ToPascalCase(this string value) { // If there are 0 or 1 characters, just return the string. if (value == null) return value; if (value.Length < 2) return value.ToUpper(); var delimiter = new[] { ' ', '-', '_', '.' }; var charArray = new char[value.Length]; var newWord = true; for (var i = 0; i < value.Length; ++i) { var currentChar = value[i]; if (char.IsLetter(currentChar)) { if (newWord || char.IsUpper(currentChar)) { newWord = false; currentChar = char.ToUpper(currentChar); } else { currentChar = char.ToLower(currentChar); } } else if (delimiter.Contains(currentChar)) { newWord = true; } charArray[i] = currentChar; } return new string(charArray); } /// /// Gibt einen Text gekürzt auf eine max. Zeichenzahl zurück. /// Wenn der Text länger als maxLength war, wird "..." angehängt. /// /// Text /// max. Länge /// Text gekürzt public static string Ellipsis(this string text, int maxLength) { if (string.IsNullOrWhiteSpace(text)) return ""; if (maxLength > 0 && text.Length > maxLength) { return text.Substring(0, maxLength) + "..."; } return text; } /// /// Hilfsfunktion die aus einem String "gefährlichen" Text für SQL-Injection Attacken entfernt /// /// Text /// Max. Länge des Textes /// Bereinigter Text public static string SanitizeSqlInjection(this string text, int maxLength) { if (string.IsNullOrWhiteSpace(text)) return ""; if (text.Length > maxLength) text = text.Substring(0, maxLength); var result = text.Trim().Replace(";", ""); result = result.Replace("'", ""); result = result.Replace("--", ""); result = result.Replace("/*", ""); result = result.Replace("*/", ""); result = result.Replace("xp_", ""); return result; } /// /// Hilfsfunktion die aus einem Text einen filter-Text für felxible Like-Abfragen erstellt. /// * wird mit SQL-Wildcard % ersetzt /// - wird mit SQL-Wildcard % ersetzt /// " " wird mit SQL-Wildcard % ersetzt /// /// Text der bearbeitet werden soll /// SQL-Like angepasster Text public static string SqlFilter(this string text) { if (string.IsNullOrWhiteSpace(text)) return ""; var result = text.Trim().Replace("*", "%"); result = result.Replace("-", "%"); result = result.Replace(" ", "%"); return $"%{result}%"; } private static readonly CoordinateSystemServices _coordinateSystemServices = new CoordinateSystemServices( new Dictionary { // Coordinate systems: [4326] = GeographicCoordinateSystem.WGS84.WKT, // This coordinate system covers the area of our data. // Different data requires a different coordinate system. [2855] = @" PROJCS[""NAD83(HARN) / Washington North"", GEOGCS[""NAD83(HARN)"", DATUM[""NAD83_High_Accuracy_Regional_Network"", SPHEROID[""GRS 1980"",6378137,298.257222101, AUTHORITY[""EPSG"",""7019""]], AUTHORITY[""EPSG"",""6152""]], PRIMEM[""Greenwich"",0, AUTHORITY[""EPSG"",""8901""]], UNIT[""degree"",0.01745329251994328, AUTHORITY[""EPSG"",""9122""]], AUTHORITY[""EPSG"",""4152""]], PROJECTION[""Lambert_Conformal_Conic_2SP""], PARAMETER[""standard_parallel_1"",48.73333333333333], PARAMETER[""standard_parallel_2"",47.5], PARAMETER[""latitude_of_origin"",47], PARAMETER[""central_meridian"",-120.8333333333333], PARAMETER[""false_easting"",500000], PARAMETER[""false_northing"",0], UNIT[""metre"",1, AUTHORITY[""EPSG"",""9001""]], AUTHORITY[""EPSG"",""2855""]] ", [3035] = @" PROJCS[""ETRS89-extended / LAEA Europe"", GEOGCS[""ETRS89"", DATUM[""European_Terrestrial_Reference_System_1989"", SPHEROID[""GRS 1980"",6378137,298.257222101], TOWGS84[0,0,0,0,0,0,0]], PRIMEM[""Greenwich"",0], UNIT[""degree"",0.0174532925199433, AUTHORITY[""EPSG"",""9122""]], AUTHORITY[""EPSG"",""4258""]], PROJECTION[""Lambert_Azimuthal_Equal_Area""], PARAMETER[""latitude_of_center"",52], PARAMETER[""longitude_of_center"",10], PARAMETER[""false_easting"",4321000], PARAMETER[""false_northing"",3210000], UNIT[""metre"",1], AUTHORITY[""EPSG"",""3035""]] " }); public static Geometry ProjectTo(this Geometry geometry, int srid) { var transformation = _coordinateSystemServices.CreateTransformation(geometry.SRID, srid); var result = geometry.Copy(); result.Apply(new MathTransformFilter(transformation.MathTransform)); return result; } private class MathTransformFilter : ICoordinateSequenceFilter { private readonly MathTransform _transform; public MathTransformFilter(MathTransform transform) => _transform = transform; public bool Done => false; public bool GeometryChanged => true; public void Filter(CoordinateSequence seq, int i) { var x = seq.GetX(i); var y = seq.GetY(i); var z = seq.GetZ(i); _transform.Transform(ref x, ref y, ref z); seq.SetX(i, x); seq.SetY(i, y); seq.SetZ(i, z); } } } }