/// <summary> /// Gets the data polygons from a feature. /// </summary> /// <param name="feature">The feature.</param> /// <returns>List with DataPolygons.</returns> private List <DataPolygon> GetDataPolygons(Feature feature) { GeoJSONObjectType polygonType = feature.Geometry.Type; List <DataPolygon> polygons = new List <DataPolygon>(); if (!(polygonType == GeoJSONObjectType.MultiPolygon || polygonType == GeoJSONObjectType.Polygon)) { return(polygons); } DataPolygon dataPolygon; if (polygonType == GeoJSONObjectType.MultiPolygon) { MultiPolygon multiPolygon = (MultiPolygon)feature.Geometry; foreach (Polygon polygon in multiPolygon.Coordinates) { dataPolygon = DataPolygonConverter.ConvertToDataPolygon(polygon); polygons.Add(dataPolygon); } } if (polygonType == GeoJSONObjectType.Polygon) { dataPolygon = DataPolygonConverter.ConvertToDataPolygon((Polygon)feature.Geometry); polygons.Add(dataPolygon); } return(polygons); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { reader.Read(); if (!(reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "type")) { throw new ArgumentException("invalid tokentype: " + reader.TokenType); } reader.Read(); if (reader.TokenType != JsonToken.String) { throw new ArgumentException("invalid tokentype: " + reader.TokenType); } GeoJSONObjectType geometryType = (GeoJSONObjectType)Enum.Parse(typeof(GeoJSONObjectType), (string)reader.Value); switch (geometryType) { case GeoJSONObjectType.Point: Coordinate coordinate = serializer.Deserialize <Coordinate>(reader); return(_factory.CreatePoint(coordinate)); case GeoJSONObjectType.LineString: Coordinate[] coordinates = serializer.Deserialize <Coordinate[]>(reader); return(_factory.CreateLineString(coordinates)); case GeoJSONObjectType.Polygon: List <Coordinate[]> coordinatess = serializer.Deserialize <List <Coordinate[]> >(reader); return(CreatePolygon(coordinatess)); case GeoJSONObjectType.MultiPoint: coordinates = serializer.Deserialize <Coordinate[]>(reader); return(_factory.CreateMultiPoint(coordinates)); case GeoJSONObjectType.MultiLineString: coordinatess = serializer.Deserialize <List <Coordinate[]> >(reader); List <ILineString> strings = new List <ILineString>(); for (int i = 0; i < coordinatess.Count; i++) { strings.Add(_factory.CreateLineString(coordinatess[i])); } return(_factory.CreateMultiLineString(strings.ToArray())); case GeoJSONObjectType.MultiPolygon: List <List <Coordinate[]> > coordinatesss = serializer.Deserialize <List <List <Coordinate[]> > >(reader); List <IPolygon> polygons = new List <IPolygon>(); foreach (List <Coordinate[]> coordinateses in coordinatesss) { polygons.Add(CreatePolygon(coordinateses)); } return(_factory.CreateMultiPolygon(polygons.ToArray())); case GeoJSONObjectType.GeometryCollection: List <IGeometry> geoms = serializer.Deserialize <List <IGeometry> >(reader); return(_factory.CreateGeometryCollection(geoms.ToArray())); } return(null); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { reader.Read(); if (!(reader.TokenType == JsonToken.PropertyName && (string)reader.Value == "geometries")) { throw new Exception(); } reader.Read(); if (reader.TokenType != JsonToken.StartArray) { throw new Exception(); } reader.Read(); List <IGeometry> geoms = new List <IGeometry>(); while (reader.TokenType != JsonToken.EndArray) { JObject obj = (JObject)serializer.Deserialize(reader); GeoJSONObjectType geometryType = (GeoJSONObjectType)Enum.Parse(typeof(GeoJSONObjectType), obj.Value <string>("type")); switch (geometryType) { case GeoJSONObjectType.Point: geoms.Add(_factory.CreatePoint(ToCoordinate(obj.Value <JArray>("coordinates")))); break; case GeoJSONObjectType.LineString: geoms.Add(_factory.CreateLineString(ToCoordinates(obj.Value <JArray>("coordinates")))); break; case GeoJSONObjectType.Polygon: geoms.Add(CreatePolygon(ToListOfCoordinates(obj.Value <JArray>("coordinates")))); break; case GeoJSONObjectType.MultiPoint: geoms.Add(_factory.CreateMultiPoint(ToCoordinates(obj.Value <JArray>("coordinates")))); break; case GeoJSONObjectType.MultiLineString: geoms.Add(CreateMultiLineString(ToListOfCoordinates(obj.Value <JArray>("coordinates")))); break; case GeoJSONObjectType.MultiPolygon: geoms.Add(CreateMultiPolygon(ToListOfListOfCoordinates(obj.Value <JArray>("coordinates")))); break; case GeoJSONObjectType.GeometryCollection: throw new NotSupportedException(); } reader.Read(); } return(geoms); }
public static GeometryType ToGeometryType(this GeoJSONObjectType type) { switch (type) { case GeoJSONObjectType.Polygon: return(GeometryType.Polygon); case GeoJSONObjectType.LineString: return(GeometryType.Line); default: return(GeometryType.Point); } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { IGeometry geom = value as IGeometry; if (geom == null) { return; } writer.WriteStartObject(); GeoJSONObjectType geomType = ToGeoJsonObject(geom); writer.WritePropertyName("type"); writer.WriteValue(Enum.GetName(typeof(GeoJSONObjectType), geomType)); switch (geomType) { case GeoJSONObjectType.Point: serializer.Serialize(writer, geom.Coordinate); break; case GeoJSONObjectType.LineString: case GeoJSONObjectType.MultiPoint: serializer.Serialize(writer, geom.Coordinates); break; case GeoJSONObjectType.Polygon: IPolygon poly = geom as IPolygon; Debug.Assert(poly != null); serializer.Serialize(writer, PolygonCoordiantes(poly)); break; case GeoJSONObjectType.MultiPolygon: IMultiPolygon mpoly = geom as IMultiPolygon; Debug.Assert(mpoly != null); List <List <Coordinate[]> > list = new List <List <Coordinate[]> >(); foreach (IPolygon mempoly in mpoly.Geometries) { list.Add(PolygonCoordiantes(mempoly)); } serializer.Serialize(writer, list); break; case GeoJSONObjectType.GeometryCollection: IGeometryCollection gc = geom as IGeometryCollection; Debug.Assert(gc != null); serializer.Serialize(writer, gc.Geometries); break; default: List <Coordinate[]> coordinates = new List <Coordinate[]>(); foreach (IGeometry geometry in ((IGeometryCollection)geom).Geometries) { coordinates.Add(geometry.Coordinates); } serializer.Serialize(writer, coordinates); break; } writer.WriteEndObject(); }