/// <summary> /// Reads a stream and converts the shapefile record to an equilivant geometry object. /// </summary> /// <param name="file">The stream to read.</param> /// <param name="geometryFactory">The geometry factory to use when making the object.</param> /// <returns>The Geometry object that represents the shape file record.</returns> public override IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory) { int shapeTypeNum = file.ReadInt32(); type = (ShapeGeometryType) Enum.Parse(typeof(ShapeGeometryType), shapeTypeNum.ToString()); if (type == ShapeGeometryType.NullShape) return geometryFactory.CreateMultiPoint(new IPoint[] { }); if (!(type == ShapeGeometryType.MultiPoint || type == ShapeGeometryType.MultiPointM || type == ShapeGeometryType.MultiPointZ || type == ShapeGeometryType.MultiPointZM)) throw new ShapefileException("Attempting to load a non-multipoint as multipoint."); // Read and for now ignore bounds. int bblength = GetBoundingBoxLength(); bbox = new double[bblength]; for (; bbindex < 4; bbindex++) { double d = file.ReadDouble(); bbox[bbindex] = d; } // Read points int numPoints = file.ReadInt32(); IPoint[] points = new IPoint[numPoints]; for (int i = 0; i < numPoints; i++) { double x = file.ReadDouble(); double y = file.ReadDouble(); IPoint point = geometryFactory.CreatePoint(new Coordinate(x, y)); points[i] = point; } geom = geometryFactory.CreateMultiPoint(points); GrabZMValues(file); return geom; }
/// <summary> /// Converts the GML element to multi point. /// </summary> /// <param name="element">The GML element of the multi point.</param> /// <param name="factory">The geometry factory.</param> /// <returns>The converted multi point.</returns> private static IMultiPoint ToMultiPoint(XElement element, IGeometryFactory factory) { if (element.Elements() == null) { return(factory.CreateMultiPoint()); } List <Coordinate> coordinates = new List <Coordinate>(); foreach (XElement innerElement in element.Elements()) { switch (innerElement.Name.LocalName) { case "coordinates": return(factory.CreateMultiPoint(ConvertCoordinates(innerElement))); case "posList": return(factory.CreateMultiPoint(ConvertPosList(innerElement, GetDimension(element, innerElement)))); case "coord": coordinates.Add(ConvertCoordinate(innerElement.Value.Split(','))); break; } } return(factory.CreateMultiPoint(coordinates)); }
/// <summary> /// Reads a stream and converts the shapefile record to an equilivant geometry object. /// </summary> /// <param name="file">The stream to read.</param> /// <param name="totalRecordLength">Total length of the record we are about to read</param> /// <param name="factory">The geometry factory to use when making the object.</param> /// <returns>The Geometry object that represents the shape file record.</returns> public override IGeometry Read(BigEndianBinaryReader file, int totalRecordLength, IGeometryFactory factory) { int totalRead = 0; int shapeTypeNum = ReadInt32(file, totalRecordLength, ref totalRead); var type = (ShapeGeometryType)EnumUtility.Parse(typeof(ShapeGeometryType), shapeTypeNum.ToString()); if (type == ShapeGeometryType.NullShape) { return(factory.CreateMultiPoint(new IPoint[] { })); } if (type != ShapeType) { throw new ShapefileException(string.Format("Encountered a '{0}' instead of a '{1}'", type, ShapeType)); } // Read and for now ignore bounds. int bblength = GetBoundingBoxLength(); boundingBox = new double[bblength]; for (; boundingBoxIndex < 4; boundingBoxIndex++) { double d = ReadDouble(file, totalRecordLength, ref totalRead); boundingBox[boundingBoxIndex] = d; } // Read points var numPoints = ReadInt32(file, totalRecordLength, ref totalRead); var buffer = new CoordinateBuffer(numPoints, NoDataBorderValue, true); var points = new IPoint[numPoints]; var pm = factory.PrecisionModel; for (var i = 0; i < numPoints; i++) { var x = pm.MakePrecise(ReadDouble(file, totalRecordLength, ref totalRead)); var y = pm.MakePrecise(ReadDouble(file, totalRecordLength, ref totalRead)); buffer.AddCoordinate(x, y); buffer.AddMarker(); } // Trond Benum: We have now read all the points, let's read optional Z and M values GetZMValues(file, totalRecordLength, ref totalRead, buffer); var sequences = buffer.ToSequences(factory.CoordinateSequenceFactory); for (var i = 0; i < numPoints; i++) { points[i] = factory.CreatePoint(sequences[i]); } geom = factory.CreateMultiPoint(points); return(geom); }
private static IGeometry FromShapeFileMultiPoint(EsriShapeBuffer shapeBuffer, out Envelope box) { box = null; if (shapeBuffer == null) { return(null); } var hasZ = EsriShapeBuffer.HasZs(shapeBuffer.shapeType); var hasM = EsriShapeBuffer.HasMs(shapeBuffer.shapeType); using (var reader = new BinaryReader(new MemoryStream(shapeBuffer.shapeBuffer))) { var type = reader.ReadInt32(); if (!(type == 8 || type == 18 || type == 28)) { throw new InvalidOperationException(); } box = createEnvelope(reader); var numPoints = reader.ReadInt32(); IList <IPoint> points = new List <IPoint>(); for (var i = 0; i < numPoints; i++) { var vertex = hasZ ? new Point(reader.ReadDouble(), reader.ReadDouble(), double.NaN) : new Point(reader.ReadDouble(), reader.ReadDouble()); points.Add(vertex); } if (hasZ) { var minZ = reader.ReadDouble(); var maxZ = reader.ReadDouble(); for (var i = 0; i < numPoints; i++) { points[i].Z = reader.ReadDouble(); } } if (points.Count == 1) { return(points[0]); } return(geometryFactory.CreateMultiPoint(points.ToArray())); } }
private IGeometry ReadMultiPoint() { ShapefileGeometryType type = (ShapefileGeometryType)_reader.ReadInt32(); if (type == ShapefileGeometryType.NullShape) { return(_gf.CreateMultiPoint((Coordinate[])null)); } if (type != ShapefileGeometryType.MultiPoint && type != ShapefileGeometryType.MultiPointZ) { throw new Exception("Attempting to load a non-multipoint as multipoint."); } // Read the box double xMin = _reader.ReadDouble(), yMin = _reader.ReadDouble(); ShapeEnvelope.Init(xMin, _reader.ReadDouble(), yMin, _reader.ReadDouble()); Coordinate[] coords = new Coordinate[_reader.ReadInt32()]; for (int i = 0; i < coords.Length; i++) { coords[i] = new Coordinate( _reader.ReadDouble(), _reader.ReadDouble()); } if (type == ShapefileGeometryType.MultiPointZ) { // Don't use the min/max z values. Just read & throw away. _reader.ReadDouble(); _reader.ReadDouble(); for (int i = 0; i < coords.Length; i++) { coords[i].Z = _reader.ReadDouble(); } // Don't use the min/max m values. Just read & throw away. _reader.ReadDouble(); _reader.ReadDouble(); for (int i = 0; i < coords.Length; i++) { coords[i].M = _reader.ReadDouble(); } } return(_gf.CreateMultiPoint(coords)); }
private static IMultiPoint ParseWkbMultiPoint(byte[] blob, ref int offset, IGeometryFactory factory, ReadCoordinatesFunction readCoordinates, GaiaImport gaiaImport) { var getInt32 = gaiaImport.GetInt32; var getDouble = gaiaImport.GetDouble; var number = getInt32(blob, ref offset); var coords = new Coordinate[number]; for (var i = 0; i < number; i++) { if (blob[offset++] != (byte)GaiaGeoBlobMark.GAIA_MARK_ENTITY) { throw new Exception(); } var gt = getInt32(blob, ref offset); if (ToBaseGeometryType((GaiaGeoGeometry)gt) != GaiaGeoGeometry.GAIA_POINT) { throw new Exception(); } coords[i] = new Coordinate(getDouble(blob, ref offset), getDouble(blob, ref offset)); if (gaiaImport.HasZ) { coords[i].Z = getDouble(blob, ref offset); } if (gaiaImport.HasM) { /*coords[i].M =*/ getDouble(blob, ref offset); } } return(factory.CreateMultiPoint(coords)); }
/// <summary> /// Reads a stream and converts the shapefile record to an equilivant geometry object. /// </summary> /// <param name="file">The stream to read.</param> /// <param name="geometryFactory">The geometry factory to use when making the object.</param> /// <returns>The Geometry object that represents the shape file record.</returns> public override IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory) { int shapeTypeNum = file.ReadInt32(); ShapeGeometryTypes shapeType = (ShapeGeometryTypes)Enum.Parse(typeof(ShapeGeometryTypes), shapeTypeNum.ToString()); if (shapeType == ShapeGeometryTypes.NullShape) { return(null); } if (!(shapeType == ShapeGeometryTypes.MultiPoint || shapeType == ShapeGeometryTypes.MultiPointM || shapeType == ShapeGeometryTypes.MultiPointZ || shapeType == ShapeGeometryTypes.MultiPointZM)) { throw new ShapefileException("Attempting to load a non-multipoint as multipoint."); } // Read and for now ignore bounds. double[] box = new double[4]; for (int i = 0; i < 4; i++) { box[i] = file.ReadDouble(); } // Read points int numPoints = file.ReadInt32(); IPoint[] points = new IPoint[numPoints]; for (int i = 0; i < numPoints; i++) { points[i] = geometryFactory.CreatePoint(new Coordinate(file.ReadDouble(), file.ReadDouble())); } return(geometryFactory.CreateMultiPoint(points)); }
private IGeometry BuildMultiPoint() { var points = _ccGeometries .ConvertAll(g => g as IPoint).ToArray(); return(_factory.CreateMultiPoint(points)); }
private void DoMinimumBoundingCircleTest(string wkt, string expectedWKT, Coordinate expectedCentre, double expectedRadius) { var mbc = new MinimumBoundingCircle(reader.Read(wkt)); var exPts = mbc.GetExtremalPoints(); IGeometry actual = geometryFactory.CreateMultiPoint(exPts); double actualRadius = mbc.GetRadius(); var actualCentre = mbc.GetCentre(); //Console.WriteLine(" Centre = " + actualCentre + " Radius = " + actualRadius); var expected = reader.Read(expectedWKT); bool isEqual = actual.Equals(expected); // need this hack because apparently equals does not work for MULTIPOINT EMPTY if (actual.IsEmpty && expected.IsEmpty) { isEqual = true; } if (!isEqual) { Console.WriteLine("Actual = " + actual + ", Expected = " + expected); } Assert.IsTrue(isEqual); if (expectedCentre != null) { Assert.IsTrue(expectedCentre.Distance(actualCentre) < TOLERANCE); } if (expectedRadius >= 0) { Assert.IsTrue(Math.Abs(expectedRadius - actualRadius) < TOLERANCE); } }
/// <summary> /// Creates a <c>MultiPoint</c> using the next token in the stream. /// </summary> /// <param name="tokens"> /// Tokenizer over a stream of text in Well-known Text /// format. The next tokens must form a <MultiPoint Text. /// </param> /// <param name="factory"> </param> /// <returns> /// A <c>MultiPoint</c> specified by the next /// token in the stream.</returns> private IMultiPoint ReadMultiPointText(IEnumerator <Token> tokens, IGeometryFactory factory) { var hasZ = false; var coords = GetCoordinates(tokens, true, ref hasZ); return(factory.CreateMultiPoint(ToPoints(ToSequence(hasZ, coords), factory))); }
private IMultiPoint CreateMultiPoint(int dim, int lrs, Decimal[] elemInfo, int elemIndex, List <Coordinate> coords) { var sOffset = StartingOffset(elemInfo, elemIndex); var etype = EType(elemInfo, elemIndex); var interpretation = Interpretation(elemInfo, elemIndex); if (!(sOffset >= 1) || !(sOffset <= coords.Count)) { throw new ArgumentException("ELEM_INFO STARTING_OFFSET " + sOffset + " inconsistent with ORDINATES length " + coords.Count); } if (etype != SdoEType.Coordinate) { throw new ArgumentException("ETYPE " + etype + " inconsistent with expected POINT"); } if (!(interpretation > 1)) { return(null); } var len = dim + lrs; var start = (sOffset - 1) / len; var end = start + interpretation; var points = _factory.CreateMultiPoint(SubArray(coords, start, end)); return(points); }
/// <summary> /// Creates a new MultiPoint geometry from a MultiPoint shape /// </summary> /// <param name="factory">The IGeometryFactory to use to create the new shape.</param> /// <returns></returns> protected IGeometry FromMultiPoint(IGeometryFactory factory) { if (factory == null) { factory = Geometry.DefaultFactory; } List <Coordinate> coords = new List <Coordinate>(); foreach (PartRange part in _shapeRange.Parts) { int i = part.StartIndex; foreach (Vertex vertex in part) { Coordinate c = new Coordinate(vertex.X, vertex.Y); coords.Add(c); if (M != null && M.Length != 0) { c.M = M[i]; } if (Z != null && Z.Length != 0) { c.Z = Z[i]; } i++; } } return(factory.CreateMultiPoint(coords)); }
public object ToMultiPoint(CoordinateInfo[] coordinates) { if (coordinates.Length == 0) { return(MultiPoint.Empty); } return(_geometryFactory.CreateMultiPoint(coordinates.Select(ToPoint).Cast <IPoint>().ToArray())); }
/// <summary> /// Reads a stream and converts the shapefile record to an equilivant geometry object. /// </summary> /// <param name="file">The stream to read.</param> /// <param name="totalRecordLength">Total length of the record we are about to read</param> /// <param name="geometryFactory">The geometry factory to use when making the object.</param> /// <returns>The Geometry object that represents the shape file record.</returns> public override IGeometry Read(BigEndianBinaryReader file, int totalRecordLength, IGeometryFactory geometryFactory) { int totalRead = 0; int shapeTypeNum = ReadInt32(file, totalRecordLength, ref totalRead); var type = (ShapeGeometryType) EnumUtility.Parse(typeof(ShapeGeometryType), shapeTypeNum.ToString()); if (type == ShapeGeometryType.NullShape) return geometryFactory.CreateMultiPoint(new IPoint[] { }); if (type != ShapeType) throw new ShapefileException(string.Format("Encountered a '{0}' instead of a '{1}'", type, ShapeType)); // Read and for now ignore bounds. int bblength = GetBoundingBoxLength(); boundingBox = new double[bblength]; for (; boundingBoxIndex < 4; boundingBoxIndex++) { double d = ReadDouble(file, totalRecordLength, ref totalRead); boundingBox[boundingBoxIndex] = d; } // Read points var numPoints = ReadInt32(file, totalRecordLength, ref totalRead); var buffer = new CoordinateBuffer(numPoints, NoDataBorderValue, true); var points = new IPoint[numPoints]; var pm = geometryFactory.PrecisionModel; for (var i = 0; i < numPoints; i++) { var x = pm.MakePrecise(ReadDouble(file, totalRecordLength, ref totalRead)); var y = pm.MakePrecise(ReadDouble(file, totalRecordLength, ref totalRead)); buffer.AddCoordinate(x, y); buffer.AddMarker(); } // Trond Benum: We have now read all the points, let's read optional Z and M values GetZMValues(file, totalRecordLength, ref totalRead, buffer); var sequences = buffer.ToSequences(geometryFactory.CoordinateSequenceFactory); for (var i = 0; i < numPoints; i++) points[i] = geometryFactory.CreatePoint(sequences[i]); geom = geometryFactory.CreateMultiPoint(points); return geom; }
/// <summary> /// Reads a <see cref="IMultiPoint"/> from the input stream. /// </summary> /// <param name="reader">The binary reader.</param> /// <param name="factory">The geometry factory to use for geometry creation.</param> /// <returns>The MultiPoint</returns> protected IMultiPoint ReadMultiPoint(BinaryReader reader, IGeometryFactory factory) { int numGeometries = reader.ReadInt32(); var points = new IPoint[numGeometries]; ReadGeometryArray(reader, points); return(factory.CreateMultiPoint(points)); }
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member { 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 = _geoJsonSerializer.Deserialize <Coordinate>(reader); return(_factory.CreatePoint(coordinate)); case GeoJsonObjectType.LineString: Coordinate[] coordinates = _geoJsonSerializer.Deserialize <Coordinate[]>(reader); return(_factory.CreateLineString(coordinates)); case GeoJsonObjectType.Polygon: List <Coordinate[]> coordinatess = _geoJsonSerializer.Deserialize <List <Coordinate[]> >(reader); return(CreatePolygon(coordinatess)); case GeoJsonObjectType.MultiPoint: coordinates = _geoJsonSerializer.Deserialize <Coordinate[]>(reader); return(_factory.CreateMultiPoint(coordinates)); case GeoJsonObjectType.MultiLineString: coordinatess = _geoJsonSerializer.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 = _geoJsonSerializer.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 = _geoJsonSerializer.Deserialize <List <IGeometry> >(reader); return(_factory.CreateGeometryCollection(geoms.ToArray())); } return(null); }
private IMultiPoint CreateMultiPoint(double[][] data) { IPoint[] list = new IPoint[data.Length]; for (int i = 0; i < data.Length; i++) { list[i] = CreatePoint(data[i]); } return(_factory.CreateMultiPoint(list)); }
/// <summary> /// Function to read a <see cref="IMultiPoint"/> from a ShapeFile stream using the specified <paramref name="reader"/>. /// </summary> /// <param name="reader">The reader to use</param> /// <param name="ordinates">The ordinates to read</param> /// <returns>The read polygonal geometry</returns> public IGeometry ReadMultiPoint(BinaryReader reader, Ordinates ordinates) { /*var bbox = */ ReadBoundingBox(reader); // jump boundingbox var numPoints = ReadNumPoints(reader); var buffer = new CoordinateBuffer(numPoints, ShapeFileConstants.NoDataBorder, true); ReadCoordinates(reader, numPoints, new[] { numPoints - 1 }, ordinates, buffer); return _factory.CreateMultiPoint(buffer.ToSequence()); }
private IMultiPoint ReadMultiPoint(int dim, int lrsDim, SdoGeometry sdoGeom) { Double[] ordinates = sdoGeom.OrdinatesArray.Select(d => Convert.ToDouble(d)).ToArray(); ICoordinateSequence cs = ConvertOrdinateArray(ordinates, sdoGeom); IMultiPoint multipoint = factory.CreateMultiPoint(cs); multipoint.SRID = (int)sdoGeom.Sdo_Srid; return(multipoint); }
/// <summary> /// Converts the geometry from Geography Markup Language (GML) representation. /// </summary> /// <param name="source">The source collection of XML elements.</param> /// <param name="geometryFactory">The geometry factory.</param> /// <param name="referenceSystemFactory">The reference system factory.</param> /// <returns>The converted geometry.</returns> /// <exception cref="System.ArgumentNullException"> /// The source is null. /// or /// The geometry factory is null. /// or /// The reference system factory is null. /// </exception> /// <exception cref="System.ArgumentException">The specified source is invalid.</exception> public static IGeometry ToGeometry(this IEnumerable <XElement> source, IGeometryFactory geometryFactory, IReferenceSystemFactory referenceSystemFactory) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (geometryFactory == null) { throw new ArgumentNullException(nameof(geometryFactory)); } if (referenceSystemFactory == null) { throw new ArgumentNullException(nameof(referenceSystemFactory)); } try { List <IGeometry> geometries = new List <IGeometry>(); foreach (XElement element in source) { geometries.Add(ToGeometry(element, geometryFactory, referenceSystemFactory)); } if (geometries.Count == 0) { return(null); } if (geometries.Count == 1) { return(geometries[0]); } if (geometries.All(geometry => geometry is IPoint)) { return(geometryFactory.CreateMultiPoint(geometries.Cast <IPoint>())); } if (geometries.All(geometry => geometry is ILineString)) { return(geometryFactory.CreateMultiLineString(geometries.Cast <ILineString>())); } if (geometries.All(geometry => geometry is IPolygon)) { return(geometryFactory.CreateMultiPolygon(geometries.Cast <IPolygon>())); } return(geometryFactory.CreateGeometryCollection(geometries)); } catch (Exception ex) { throw new ArgumentException(CoreMessages.SourceIsInvalid, nameof(source), ex); } }
private static IGeometry CreateMultiPoint(Ordinates ordinates, bool empty) { if (empty) { return(Factory.CreateMultiPoint((ICoordinateSequence)null)); } int numPoints = Rnd.Next(75, 101); var seq = CsFactory.Create(numPoints, ordinates); for (int i = 0; i < numPoints; i++) { foreach (var o in OrdinatesUtility.ToOrdinateArray(ordinates)) { seq.SetOrdinate(i, o, RandomOrdinate(o, Factory.PrecisionModel)); } } return(Factory.CreateMultiPoint(seq)); }
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); }
private IGeometry ToGeoAPIMultiPoint(SpatialLite.Core.API.IMultiPoint geometry) { var lst = new List <IPoint>(); foreach (var point in geometry.Geometries) { lst.Add((IPoint)ToGeoAPIPoint(point)); } return(_factory.CreateMultiPoint(lst.ToArray())); }
/// <summary> /// Creates a Point using the next token in the stream. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text /// format. The next tokens must form a <Point Text>.</param> /// <param name="factory">The factory to create the result geometry</param> /// <returns>Returns a Point specified by the next token in /// the stream.</returns> /// <remarks> /// ParseException is thrown if an unexpected token is encountered. /// </remarks> private static IMultiPoint ReadMultiPointText(WktStreamTokenizer tokenizer, IGeometryFactory factory) { string nextToken = GetNextEmptyOrOpener(tokenizer); if (nextToken == "EMPTY") { return(factory.CreateMultiPoint((Coordinate[])null)); } var points = new List <Coordinate>(); points.Add(new Coordinate(GetNextNumber(tokenizer), GetNextNumber(tokenizer))); nextToken = GetNextCloserOrComma(tokenizer); while (nextToken == ",") { points.Add(new Coordinate(GetNextNumber(tokenizer), GetNextNumber(tokenizer))); nextToken = GetNextCloserOrComma(tokenizer); } return(factory.CreateMultiPoint(points.ToArray())); }
public void TestWriteMultiPoint() { IPoint[] points = { _factory.CreatePoint(new Coordinate(10, 10, 0)), _factory.CreatePoint(new Coordinate(20, 20, 0)) }; var multiPoint = _factory.CreateMultiPoint(points); Assert.AreEqual("MULTIPOINT ((10 10), (20 20))", _writer.Write(multiPoint)); }
/// <summary> /// Reads a stream and converts the shapefile record to an equilivant geometry object. /// </summary> /// <param name="file">The stream to read.</param> /// <param name="geometryFactory">The geometry factory to use when making the object.</param> /// <returns>The Geometry object that represents the shape file record.</returns> public override IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory) { int shapeTypeNum = file.ReadInt32(); type = (ShapeGeometryType)EnumUtility.Parse(typeof(ShapeGeometryType), shapeTypeNum.ToString()); if (type == ShapeGeometryType.NullShape) { return(geometryFactory.CreateMultiPoint(new IPoint[] { })); } if (!(type == ShapeGeometryType.MultiPoint || type == ShapeGeometryType.MultiPointM || type == ShapeGeometryType.MultiPointZ || type == ShapeGeometryType.MultiPointZM)) { throw new ShapefileException("Attempting to load a non-multipoint as multipoint."); } // Read and for now ignore bounds. int bblength = GetBoundingBoxLength(); bbox = new double[bblength]; for (; bbindex < 4; bbindex++) { double d = file.ReadDouble(); bbox[bbindex] = d; } // Read points int numPoints = file.ReadInt32(); IPoint[] points = new IPoint[numPoints]; for (int i = 0; i < numPoints; i++) { double x = file.ReadDouble(); double y = file.ReadDouble(); IPoint point = geometryFactory.CreatePoint(new Coordinate(x, y)); points[i] = point; } geom = geometryFactory.CreateMultiPoint(points); GrabZMValues(file); return(geom); }
public void ParseMultiPoint() { string multipoint = "MULTIPOINT ((20.564 346.3493254), (45 32), (23 54))"; var geom = GeomFromText(multipoint) as IMultiPoint; Assert.IsNotNull(geom); Assert.AreEqual(20.564, ((IPoint)geom[0]).X); Assert.AreEqual(54, ((IPoint)geom[2]).Y); Assert.AreEqual(multipoint, geom.AsText()); Assert.IsTrue(GeomFromText("MULTIPOINT EMPTY").IsEmpty); Assert.AreEqual("MULTIPOINT EMPTY", Factory.CreateMultiPoint((Coordinate[])null).AsText()); }
internal static NTSMultiPoint ToNTSMultiPoint(Geometries.MultiPoint geom, IGeometryFactory factory) { NTSPoint[] points = new NTSPoint[geom.Points.Count]; int index = 0; foreach (Geometries.Point point in geom.Points) { points[index++] = ToNTSPoint(point, factory); } return(factory.CreateMultiPoint(points) as NTSMultiPoint); }
/// <summary> /// Transforms a <see cref="GeoAPI.Geometries.IMultiPoint"/>. /// </summary> /// <param name="points">MultiPoint to transform</param> /// <param name="from">Source Projection</param> /// <param name="to">Target Projection</param> /// <param name="toFactory">The factory to create geometries for <paramref name="to"/></param> /// <returns>Transformed MultiPoint</returns> public static IMultiPoint TransformMultiPoint(IMultiPoint points, ProjectionInfo from, ProjectionInfo to, IGeometryFactory toFactory) { try { var seq = toFactory.CoordinateSequenceFactory.Create(points.Coordinates); var toSeq = TransformSequence(seq, from, to, toFactory.CoordinateSequenceFactory); return(toFactory.CreateMultiPoint(toSeq)); } catch { return(null); } }
private IMultiPoint ReadMultiPoint(SdoGeometry sdoGeom) { if (sdoGeom.OrdinatesArray.Length == 0) { return(MultiPoint.Empty); } double[] ordinates = sdoGeom.OrdinatesArrayOfDoubles; ICoordinateSequence cs = ConvertOrdinateArray(ordinates, sdoGeom.Dimensionality); IMultiPoint multipoint = _factory.CreateMultiPoint(cs); multipoint.SRID = (int)sdoGeom.Sdo_Srid; return(multipoint); }
/// <summary> /// Creates a new MultiPoint geometry from a MultiPoint shape. /// </summary> /// <param name="factory">The IGeometryFactory to use to create the new shape.</param> /// <returns></returns> protected IGeometry FromMultiPoint(IGeometryFactory factory) { if (factory == null) { factory = Geometry.DefaultFactory; } var coords = new List <Coordinate>(); foreach (var part in _shapeRange.Parts) { GetCoordinates(part, coords); } return(factory.CreateMultiPoint(coords.ToArray())); }
/// <summary> /// Finalizes the result. /// </summary> protected override IGeometry FinalizeResult() { if (Source.VertexCount == 0) { return(null); } // if only points are in the graph if (_lines.Count == 0 && _polygons.Count == 0) { if (_points.Count == 1) { return(_points.First()); } return(_factory.CreateMultiPoint(_points)); } // if no faces are extracted else if (_polygons.Count == 0) { if (_points.Count == 0) { if (_lines.Count == 1) { return(_lines.First()); } return(_factory.CreateMultiLineString(_lines)); } return(_factory.CreateGeometryCollection(_points.Concat <IGeometry>(_lines))); } // if faces are extracted if (_points.Count == 0 && _lines.Count == 0) { if (_polygons.Count == 1) { return(_polygons.First()); } else { return(_factory.CreateMultiPolygon(_polygons)); } } else { return(_factory.CreateGeometryCollection(_points.Concat <IGeometry>(_lines).Concat <IGeometry>(_polygons))); } }
/// <summary> /// Reads a stream and converts the shapefile record to an equilivant geometry object. /// </summary> /// <param name="file">The stream to read.</param> /// <param name="geometryFactory">The geometry factory to use when making the object.</param> /// <returns>The Geometry object that represents the shape file record.</returns> public override IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory) { int shapeTypeNum = file.ReadInt32(); ShapeGeometryTypes shapeType = (ShapeGeometryTypes) Enum.Parse(typeof(ShapeGeometryTypes), shapeTypeNum.ToString()); if ( ! ( shapeType == ShapeGeometryTypes.MultiPoint || shapeType == ShapeGeometryTypes.MultiPointM || shapeType == ShapeGeometryTypes.MultiPointZ || shapeType == ShapeGeometryTypes.MultiPointZM)) throw new ShapefileException("Attempting to load a non-multipoint as multipoint."); // Read and for now ignore bounds. double[] box = new double[4]; for (int i = 0; i < 4; i++) box[i] = file.ReadDouble(); // Read points int numPoints = file.ReadInt32(); IPoint[] points = new IPoint[numPoints]; for (int i = 0; i < numPoints; i++) points[i] = geometryFactory.CreatePoint(new Coordinate(file.ReadDouble(), file.ReadDouble())); return geometryFactory.CreateMultiPoint(points); }
/// <summary> /// Transforms a <see cref="GeoAPI.Geometries.IMultiPoint"/>. /// </summary> /// <param name="points">MultiPoint to transform</param> /// <param name="from">Source Projection</param> /// <param name="to">Target Projection</param> /// <param name="toFactory">The factory to create geometries for <paramref name="to"/></param> /// <returns>Transformed MultiPoint</returns> public static IMultiPoint TransformMultiPoint(IMultiPoint points, ProjectionInfo from, ProjectionInfo to, IGeometryFactory toFactory) { try { var seq = toFactory.CoordinateSequenceFactory.Create(points.Coordinates); var toSeq = TransformSequence(seq, from, to, toFactory.CoordinateSequenceFactory); return toFactory.CreateMultiPoint(toSeq); } catch { return null; } }
private static IMultiPoint RandomMultiPoint(IGeometryFactory geometryFactory) { return geometryFactory.CreateMultiPoint(RandomCoordinates(geometryFactory)); }
internal static NTSMultiPoint ToNTSMultiPoint(Geometries.MultiPoint geom, IGeometryFactory factory) { NTSPoint[] points = new NTSPoint[geom.Points.Count]; int index = 0; foreach (Geometries.Point point in geom.Points) points[index++] = ToNTSPoint(point, factory); return factory.CreateMultiPoint(points) as NTSMultiPoint; }
/// <summary> /// Transforms a <see cref="MultiPoint" /> object. /// </summary> /// <param name="factory"></param> /// <param name="points"></param> /// <param name="transform"></param> /// <returns></returns> public static IMultiPoint TransformMultiPoint(IGeometryFactory factory, IMultiPoint points, IMathTransform transform) { List<double[]> pointList = new List<double[]>(points.Geometries.Length); foreach (IPoint p in points.Geometries) pointList.Add(ToArray(p.X, p.Y)); pointList = transform.TransformList(pointList); IPoint[] array = new IPoint[pointList.Count]; for (int i = 0; i < pointList.Count; i++) array[i] = ToNTS(factory, pointList[i][0], pointList[i][1]); return factory.CreateMultiPoint(array); }
/// <summary> /// Creates a new MultiPoint geometry from a MultiPoint shape /// </summary> /// <param name="factory">The IGeometryFactory to use to create the new shape.</param> /// <returns></returns> protected IGeometry FromMultiPoint(IGeometryFactory factory) { if (factory == null) factory = Geometry.DefaultFactory; List<Coordinate> coords = new List<Coordinate>(); foreach (PartRange part in _shapeRange.Parts) { int i = part.StartIndex; foreach (Vertex vertex in part) { Coordinate c = new Coordinate(vertex.X, vertex.Y); coords.Add(c); if (M != null && M.Length != 0) c.M = M[i]; if (Z != null && Z.Length != 0) c.Z = Z[i]; i++; } } return factory.CreateMultiPoint(coords); }
/// <summary> /// Creates a <c>MultiPoint</c> using the next token in the stream. /// </summary> /// <param name="tokens"> /// Tokenizer over a stream of text in Well-known Text /// format. The next tokens must form a <MultiPoint Text. /// </param> /// <param name="factory"> </param> /// <returns> /// A <c>MultiPoint</c> specified by the next /// token in the stream.</returns> private IMultiPoint ReadMultiPointText(IEnumerator<Token> tokens, IGeometryFactory factory) { var hasZ = false; var coords = GetCoordinates(tokens, true, ref hasZ); return factory.CreateMultiPoint(ToPoints(ToSequence(hasZ, coords), factory)); }
/// <summary> /// Transforms a <see cref="MultiPoint" /> object. /// </summary> /// <param name="factory"></param> /// <param name="points"></param> /// <param name="transform"></param> /// <returns></returns> public static IMultiPoint TransformMultiPoint(IGeometryFactory factory, IMultiPoint points, IMathTransform transform) { //We assume the first point holds all the ordinates var firstPoint = (IPoint) points.GetGeometryN(0); var ordinateFlags = firstPoint.CoordinateSequence.Ordinates; var ordinates = OrdinatesUtility.ToOrdinateArray(ordinateFlags); var coordSequence = factory.CoordinateSequenceFactory.Create(points.NumPoints, ordinateFlags); for (var i = 0; i < points.NumGeometries; i++) { var currPoint = (IPoint) points.GetGeometryN(i); var seq = currPoint.CoordinateSequence; foreach (var ordinate in ordinates) { double d = seq.GetOrdinate(0, ordinate); coordSequence.SetOrdinate(i, ordinate, d); } } var transPoints = transform.Transform(coordSequence); return factory.CreateMultiPoint(transPoints); }
/// <summary> /// See http://www.gaia-gis.it/gaia-sins/BLOB-Geometry.html /// for the specification of the spatialite BLOB geometry format /// Derived from WKB, but unfortunately it is not practical to reuse existing /// WKB encoding/decoding code /// </summary> /// <param name="spatialliteGeom">The geometry blob</param> /// <param name="factory">The factory to create the result geometry</param> /// <returns>A geometry</returns> public static IGeometry Parse(byte[] spatialliteGeom, IGeometryFactory factory) { var nBytes = spatialliteGeom.Length; if (spatialliteGeom.Length < 44 || spatialliteGeom[0] != 0 || spatialliteGeom[38] != 0x7C || spatialliteGeom[nBytes - 1] != 0xFE) throw new ApplicationException("Corrupt SpatialLite geom"); bool isLittleEndian = spatialliteGeom[1] == 0x01; if (spatialliteGeom[1] != 0x00 && spatialliteGeom[1] != 0x01) throw new ApplicationException("Corrupt SpatialLite geom"); int idx = 39; int nGType = ReadUInt32(spatialliteGeom, ref idx, isLittleEndian); if (nGType < 1 || nGType > 7) throw new ApplicationException("Unsupported geom type!"); /* -------------------------------------------------------------------- */ /* Point */ /* -------------------------------------------------------------------- */ if (nGType == 1) { return factory.CreatePoint(ReadPoint(spatialliteGeom, ref idx, isLittleEndian)); } /* -------------------------------------------------------------------- */ /* LineString */ /* -------------------------------------------------------------------- */ else if (nGType == 2) { return ReadLineString(spatialliteGeom, ref idx, isLittleEndian, factory); } /* -------------------------------------------------------------------- */ /* Polygon */ /* -------------------------------------------------------------------- */ else if (nGType == 3) { return ReadPolygon(spatialliteGeom, ref idx, isLittleEndian, factory); } /* -------------------------------------------------------------------- */ /* MultiPoint */ /* -------------------------------------------------------------------- */ else if (nGType == 4) { List<GeoAPI.Geometries.IPoint> pts = new List<GeoAPI.Geometries.IPoint>(); int numGeoms = ReadUInt32(spatialliteGeom, ref idx, isLittleEndian); for (int i = 0; i < numGeoms; i++) { if (spatialliteGeom[idx] != 0x69) throw new ApplicationException("FormatError in SpatiaLIteGeom"); idx++; int gt = ReadUInt32(spatialliteGeom, ref idx, isLittleEndian); if (gt != 1) throw new ApplicationException("MultiPoint must Contain Point entities"); pts.Add(factory.CreatePoint(ReadPoint(spatialliteGeom, ref idx, isLittleEndian))); } return factory.CreateMultiPoint(pts.ToArray()); } /* -------------------------------------------------------------------- */ /* MultiLineString */ /* -------------------------------------------------------------------- */ else if (nGType == 5) { List<GeoAPI.Geometries.ILineString> lss = new List<GeoAPI.Geometries.ILineString>(); int numGeoms = ReadUInt32(spatialliteGeom, ref idx, isLittleEndian); for (int i = 0; i < numGeoms; i++) { if (spatialliteGeom[idx] != 0x69) throw new ApplicationException("FormatError in SpatiaLIteGeom"); idx++; int gt = ReadUInt32(spatialliteGeom, ref idx, isLittleEndian); if (gt != 2) throw new ApplicationException("MultiLineString must contain LineString Entities"); lss.Add(ReadLineString(spatialliteGeom, ref idx, isLittleEndian, factory)); } return factory.CreateMultiLineString(lss.ToArray()); } /* -------------------------------------------------------------------- */ /* MultiPolygon */ /* -------------------------------------------------------------------- */ else if (nGType == 6) { List<GeoAPI.Geometries.IPolygon> polys = new List<GeoAPI.Geometries.IPolygon>(); int numPolys = ReadUInt32(spatialliteGeom, ref idx, isLittleEndian); for (int i = 0; i < numPolys; i++) { if (spatialliteGeom[idx] != 0x69) throw new ApplicationException("FormatError in SpatiaLIteGeom"); idx++; int gt = ReadUInt32(spatialliteGeom, ref idx, isLittleEndian); if (gt != 3) throw new ApplicationException("Multipolygon must contain Polygon Entities"); polys.Add(ReadPolygon(spatialliteGeom, ref idx, isLittleEndian, factory)); } return factory.CreateMultiPolygon(polys.ToArray()); } return null; }
/// <summary> /// Creates a new MultiPoint geometry from a MultiPoint shape /// </summary> /// <param name="factory">The IGeometryFactory to use to create the new shape.</param> /// <returns></returns> protected IGeometry FromMultiPoint(IGeometryFactory factory) { if (factory == null) factory = Geometry.DefaultFactory; var coords = new List<Coordinate>(); foreach (var part in _shapeRange.Parts) { GetCoordinates(part, coords); } return factory.CreateMultiPoint(coords); }
/// <summary> /// Creates a <c>MultiPoint</c> using the next token in the stream. /// </summary> /// <param name="tokens"> /// Tokenizer over a stream of text in Well-known Text /// format. The next tokens must form a <MultiPoint Text. /// </param> /// <param name="factory"> </param> /// <returns> /// A <c>MultiPoint</c> specified by the next /// token in the stream.</returns> private IMultiPoint ReadMultiPointText(IEnumerator<Token> tokens, IGeometryFactory factory) { return factory.CreateMultiPoint(ToPoints(GetCoordinates(tokens, true), factory)); }
private static IMultiPoint CreateWKBMultiPoint(BinaryReader reader, WkbByteOrder byteOrder, IGeometryFactory factory) { // Get the number of points in this multipoint. var numPoints = (int) ReadUInt32(reader, byteOrder); // Create a new array for the points. var points = new IPoint[numPoints]; // Loop on the number of points. for (var i = 0; i < numPoints; i++) { // Read point header reader.ReadByte(); ReadUInt32(reader, byteOrder); // TODO: Validate type // Create the next point and add it to the point array. points[i] = CreateWKBPoint(reader, byteOrder, factory); } return factory.CreateMultiPoint(points); }
/// <summary> /// Creates a Point using the next token in the stream. /// </summary> /// <param name="tokenizer">Tokenizer over a stream of text in Well-known Text /// format. The next tokens must form a <Point Text>.</param> /// <param name="factory">The factory to create the result geometry</param> /// <returns>Returns a Point specified by the next token in /// the stream.</returns> /// <remarks> /// ParseException is thrown if an unexpected token is encountered. /// </remarks> private static IMultiPoint ReadMultiPointText(WktStreamTokenizer tokenizer, IGeometryFactory factory) { string nextToken = GetNextEmptyOrOpener(tokenizer); if (nextToken == "EMPTY") return factory.CreateMultiPoint((Coordinate[])null); var points = new List<Coordinate>(); points.Add(new Coordinate(GetNextNumber(tokenizer), GetNextNumber(tokenizer))); nextToken = GetNextCloserOrComma(tokenizer); while (nextToken == ",") { points.Add(new Coordinate(GetNextNumber(tokenizer), GetNextNumber(tokenizer))); nextToken = GetNextCloserOrComma(tokenizer); } return factory.CreateMultiPoint(points.ToArray()); }
/// <summary> /// Transforms a <see cref="GeoAPI.Geometries.IMultiPoint"/>. /// </summary> /// <param name="points">MultiPoint to transform</param> /// <param name="transform">MathTransform</param> /// <param name="targetFactory">The factory to create the target geometry</param> /// <returns>Transformed MultiPoint</returns> public static IMultiPoint TransformMultiPoint(IMultiPoint points, IMathTransform transform, IGeometryFactory targetFactory) { return targetFactory.CreateMultiPoint(TransformCoordinates(points.Coordinates, transform)); }
private static IMultiPoint ParseWkbMultiPoint(byte[] blob, ref int offset, IGeometryFactory factory, ReadCoordinatesFunction readCoordinates, GaiaImport gaiaImport) { var getInt32 = gaiaImport.GetInt32; var getDouble = gaiaImport.GetDouble; var number = getInt32(blob, ref offset); var coords = new Coordinate[number]; for (var i = 0; i < number; i++) { if (blob[offset++] != (byte)GaiaGeoBlobMark.GAIA_MARK_ENTITY) throw new Exception(); var gt = getInt32(blob, ref offset); if (ToBaseGeometryType((GaiaGeoGeometry)gt) != GaiaGeoGeometry.GAIA_POINT) throw new Exception(); coords[i] = new Coordinate(getDouble(blob, ref offset), getDouble(blob, ref offset)); if (gaiaImport.HasZ) coords[i].Z = getDouble(blob, ref offset); if (gaiaImport.HasM) /*coords[i].M =*/ getDouble(blob, ref offset); } return factory.CreateMultiPoint(coords); }