DbGeometry
d'parsingr un polygone, qui est présenté en tant DbGeometry
class DbGeometry
(du System.Data.Entity.Spatial
) à une représentation de list, mais il y avait échoue.
J'ai essayé: – de le convertir avec la méthode .ToList <> () – pour parsingr la bibliothèque JSON dans .NET, mais l'exemple de code de différents sites Web a échoué avec la déserialization de DbGeometry
Donc, en ce moment je returnne la geometry comme une string dans mon application API Web:
Si je ne trouvais pas de solution, comment, pour la représenter comme une list de doubles, je devrai l'parsingr manuellement en JavaScript, ce qui, je pense, est très incorrect et il doit y avoir une solution.
J'utilise Entity Framework v.6.1.1, qui a préparé le prochain model:
public partial class Buildings { public int id { get; set; } public Nullable<bool> has_holes { get; set; } public System.Data.Entity.Spatial.DbGeometry polygon_data { get; set; } public System.Data.Entity.Spatial.DbGeometry position_wgs { get; set; } public System.Data.Entity.Spatial.DbGeometry position_mercator { get; set; } public Nullable<int> height { get; set; } public ssortingng street_name { get; set; } public System.Data.Entity.Spatial.DbGeometry holes_data { get; set; } public Nullable<double> angle { get; set; } }
Je l'ai montré, si vous voulez voir une structure de la table de MSSQL CE
(qui est un LocalDb
incorporé, ou LocalDb
, un autre nom).
Donc je veux:
Soyez préparés comme des lists de doubles, donc ma question est: How can I parse DbGeometry instance, which holds a collection of points into List<Point>?
.
J'ai eu un problème similaire. Ce que j'ai fait, c'est une méthode d'extension créée qui parsing datatables géomésortingques données en points. @Erik Philips a également une bonne solution. C'est ce que j'ai trouvé.
public static class ExtensionSsortingng { private static Dictionary<ssortingng, ssortingng> _replacements = new Dictionary<ssortingng, ssortingng>(); static ExtensionSsortingng() { _replacements["LINESTRING"] = ""; _replacements["CIRCLE"] = ""; _replacements["POLYGON"] = ""; _replacements["POINT"] = ""; _replacements["("] = ""; _replacements[")"] = ""; } public static List<Point> ParseGeometryData(this ssortingng s) { var points = new List<Point>(); foreach (ssortingng to_replace in _replacements.Keys) { s = s.Replace(to_replace, _replacements[to_replace]); } ssortingng[] pointsArray = s.Split(','); for (int i = 0; i < pointsArray.Length; i++) { double[] coordinates = new double[2]; //gets x and y coordinates split by space, sortingms whitespace at pos 0, converts to double array coordinates = Array.ConvertAll(pointsArray[i].Remove(0, 1).Split(null), double.Parse); points.Add(new Point() { X = coordinates[0], Y = coordinates[1] }); } return points; } }
Et utilisez-le comme ça
List<System.Drawing.Point> points = geometryDataStr.ParseGeometryData();
Si votre geometry est valide, vous pouvez faire:
class Program { static void Main(ssortingng[] args) { DbGeometry test = DbGeometry.FromText("POLYGON((1 1, 1 2, 3 3, 1 1))"); var foo = test.AsText(); var points = new List<Point>(); Console.WriteLine(foo); if (foo.StartsWith("POLYGON ((") && foo.EndsWith("))")) { foo = foo.Subssortingng(10, foo.Length - 12); var rawPoints = foo.Split(new char[] { ',' }, SsortingngSplitOptions.RemoveEmptyEnsortinges).ToList(); foreach (var rawPoint in rawPoints) { var splitPoint = rawPoint.Split(new char[] { ' ' }, SsortingngSplitOptions.RemoveEmptyEnsortinges); points.Add(new Point() { X = decimal.Parse(splitPoint[1]), Y = decimal.Parse(splitPoint[0]) }); } } foreach (var point in points) { Console.WriteLine(point.ToSsortingng()); } Console.ReadKey(); } } class Point { public decimal X { get; set; } public decimal Y { get; set; } public override ssortingng ToSsortingng() { return ssortingng.Format("[X={0}],[Y={1}]", X, Y); } }
résultat:
POLYGON ((1 1, 1 2, 3 3, 1 1)) [X=1],[Y=1] [X=2],[Y=1] [X=3],[Y=3] [X=1],[Y=1]