Example #1
0
        /// <summary>
        /// Creates a new <c>GeometryWrapper</c> that wraps the supplied geometry.
        /// </summary>
        /// <param name="g">The geometry to wrap</param>
        /// <exception cref="ArgumentException">If the geometry can't be mapped to one of the
        /// types defined by the <c>SpatialType</c> enum.</exception>
        internal GeometryWrapper(NTS.Geometry g)
        {
            m_Geometry = g;

            // Throw exception on unexpected type
            SpatialType type = this.SpatialType;
        }
        /// <summary>
        /// Creates a new <c>GeometryWrapper</c> that wraps the supplied geometry.
        /// </summary>
        /// <param name="g">The geometry to wrap</param>
        /// <exception cref="ArgumentException">If the geometry can't be mapped to one of the
        /// types defined by the <c>SpatialType</c> enum.</exception>
        internal GeometryWrapper(NTS.Geometry g)
        {
            m_Geometry = g;

            // Throw exception on unexpected type
            SpatialType type = this.SpatialType;
        }
Example #3
0
        private List <NTS.Geometry> GetBasicGeometries(NTS.Geometry geom)
        {
            List <NTS.Geometry> result = new List <NTS.Geometry>();

            AppendBasicGeometries(result, geom);
            return(result);
        }
Example #4
0
        public static void Write(Geometry geometry, TextWriter writer)
        {
            if (geometry == null)
                return;
            if (writer == null)
                throw new ArgumentNullException("writer", "A valid text writer object is required.");

            JsonTextWriter jwriter = new JsonTextWriter(writer);
            Write(geometry, jwriter);
        }
 /// <summary>
 /// Converts any <see cref="SharpMap.Geometries.Geometry"/> array to the correspondant 
 /// <see cref=GisSharpBlog.NetTopologySuite.Geometries.Geometry"/> array.
 /// </summary>
 /// <param name="geometries"></param>
 /// <param name="factory"></param>
 /// <returns></returns>
 public static Geometry[] ToNTSGeometry(Geometries.Geometry[] geometries,
                                        GeometryFactory factory)
 {
     Geometry[] converted = new Geometry[geometries.Length];
     int index = 0;
     foreach (Geometries.Geometry geometry in geometries)
         converted[index++] = ToNTSGeometry(geometry, factory);
     if ((geometries.Length != converted.Length))
         throw new ApplicationException("Conversion error");
     return converted;
 }
Example #6
0
        internal static GeometryCollection ToNTSGeometryCollection(Geometries.GeometryCollection geometryCollection,
                                                                   GeometryFactory factory)
        {
            Geometry[] geometries = new Geometry[geometryCollection.Collection.Count];
            int        index      = 0;

            foreach (Geometries.Geometry geometry in geometryCollection.Collection)
            {
                geometries[index++] = ToNTSGeometry(geometry, factory);
            }
            return(factory.CreateGeometryCollection(geometries) as GeometryCollection);
        }
Example #7
0
        /// <summary>
        /// Converts any <see cref="SharpMap.Geometries.Geometry"/> array to the correspondant
        /// <see cref=GisSharpBlog.NetTopologySuite.Geometries.Geometry"/> array.
        /// </summary>
        /// <param name="geometries"></param>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static Geometry[] ToNTSGeometry(Geometries.Geometry[] geometries,
                                               GeometryFactory factory)
        {
            Geometry[] converted = new Geometry[geometries.Length];
            int        index     = 0;

            foreach (Geometries.Geometry geometry in geometries)
            {
                converted[index++] = ToNTSGeometry(geometry, factory);
            }
            if ((geometries.Length != converted.Length))
            {
                throw new ApplicationException("Conversion error");
            }
            return(converted);
        }
Example #8
0
        /// <summary>
        /// Converts any <see cref="SharpMap.Geometries.Geometry"/> to the correspondant
        /// <see cref=GisSharpBlog.NetTopologySuite.Geometries.Geometry"/>.
        /// </summary>
        /// <param name="geometry"></param>
        /// <returns></returns>
        public static Geometries.Geometry ToSharpMapGeometry(Geometry geometry)
        {
            if (geometry == null)
            {
                throw new NullReferenceException("geometry");
            }

            if (geometry.GetType() == typeof(GisSharpBlog.NetTopologySuite.Geometries.Point))
            {
                return(ToSharpMapPoint(geometry as GisSharpBlog.NetTopologySuite.Geometries.Point));
            }

            else if (geometry.GetType() == typeof(LineString))
            {
                return(ToSharpMapLineString(geometry as LineString));
            }

            else if (geometry.GetType() == typeof(Polygon))
            {
                return(ToSharpMapPolygon(geometry as Polygon));
            }

            else if (geometry.GetType() == typeof(MultiPoint))
            {
                return(ToSharpMapMultiPoint(geometry as MultiPoint));
            }

            else if (geometry.GetType() == typeof(MultiLineString))
            {
                return(ToSharpMapMultiLineString(geometry as MultiLineString));
            }

            else if (geometry.GetType() == typeof(MultiPolygon))
            {
                return(ToSharpMapMultiPolygon(geometry as MultiPolygon));
            }

            else if (geometry.GetType() == typeof(GeometryCollection))
            {
                return(ToSharpMapGeometryCollection(geometry as GeometryCollection));
            }

            else
            {
                throw new NotSupportedException("Type " + geometry.GetType().FullName + " not supported");
            }
        }
Example #9
0
 private void AppendBasicGeometries(List <NTS.Geometry> result, NTS.Geometry geom)
 {
     if (geom is GeometryCollection)
     {
         // Note that I initially had 'foreach (NTS.Geometry g in gc)', which compiled,
         // but which led to an infinite loop when dealing with multi-polygons.
         GeometryCollection gc = (GeometryCollection)geom;
         NTS.Geometry[]     ga = gc.Geometries;
         foreach (NTS.Geometry g in ga)
         {
             AppendBasicGeometries(result, g); // recurse
         }
     }
     else
     {
         result.Add(geom);
     }
 }
Example #10
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="geometry"></param>
 /// <param name="leWriter"></param>
 /// <param name="beWriter"></param>
 protected void Write(Geometry geometry, BinaryWriter leWriter, BigEndianBinaryWriter beWriter)
 {
     WriteFeatureHeader(geometry, beWriter);             
                   
     if (geometry is Point)                
         shapeWriter.Write(geometry as Point, leWriter);
     else if (geometry is LineString)
         shapeWriter.Write(geometry as LineString, leWriter);
     else if (geometry is Polygon)
         shapeWriter.Write(geometry as Polygon, leWriter);
     else if (geometry is MultiPoint)
         shapeWriter.Write(geometry as MultiPoint, leWriter);
     else if (geometry is MultiLineString)
         shapeWriter.Write(geometry as MultiLineString, leWriter);
     else if (geometry is MultiPolygon)
         shapeWriter.Write(geometry as MultiPolygon, leWriter);
     else throw new NotSupportedException("Unsupported Geometry implementation:" + geometry.GetType());
 }        
Example #11
0
        public ShapeFile(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException();
            }

            m_MapName = fileName;
            IEditSpatialIndex index = new SpatialIndex();

            ShapefileDataReader sfdr = Shapefile.CreateDataReader(fileName, new GeometryFactory());
            ShapefileHeader     hdr  = sfdr.ShapeHeader;
            Envelope            ex   = hdr.Bounds;

            m_Extent = new Window(ex.MinX, ex.MinY, ex.MaxX, ex.MaxY);

            foreach (object o in sfdr)
            {
                // You get back an instance of GisSharpBlog.NetTopologySuite.IO.RowStructure, but
                // that's internal, so cast to the interface it implements (we'll attach this to
                // the geometry we wrap).
                //ICustomTypeDescriptor row = (ICustomTypeDescriptor)o;
                AdhocPropertyList row  = CreatePropertyList(sfdr);
                NTS.Geometry      geom = sfdr.Geometry;
                geom.UserData = row;

                List <NTS.Geometry> geoms = GetBasicGeometries(geom);
                foreach (NTS.Geometry g in geoms)
                {
                    g.UserData = row;
                    if (g is NTS.Point)
                    {
                        index.Add(new PointWrapper((NTS.Point)g));
                    }
                    else
                    {
                        index.Add(new GeometryWrapper(g));
                    }
                }
            }

            // Don't permit any further additions
            m_Index = index;
        }
Example #12
0
        public static void Write(Geometry geometry, JsonTextWriter jwriter)
        {
            if (geometry == null)
                return;
            if (jwriter == null)
                throw new ArgumentNullException("jwriter", "A valid JSON writer object is required.");

            if (geometry is Point)
            {
                Write(geometry as Point, jwriter);
            }
            else if (geometry is LineString)
            {
                Write(geometry as LineString, jwriter);
            }
            else if (geometry is Polygon)
            {
                Write(geometry as Polygon, jwriter);
            }
            else if (geometry is MultiPoint)
            {
                Write(geometry as MultiPoint, jwriter);
            }
            else if (geometry is MultiLineString)
            {
                Write(geometry as MultiLineString, jwriter);
            }
            else if (geometry is MultiPolygon)
            {
                Write(geometry as MultiPolygon, jwriter);
            }
            else if (geometry is GeometryCollection)
            {
                Write(geometry as GeometryCollection, jwriter);
            }
        }
Example #13
0
 private IGeometry BufferUnion(Geometry g0, Geometry g1)
 {
     var factory = g0.Factory;
     IGeometry gColl = factory.CreateGeometryCollection(new Geometry[] { g0, g1 });
     var unionAll = gColl.Buffer(0.0);
     return unionAll;
 }
Example #14
0
        public void FromXML(XmlReader reader)
        {
            if (reader.IsStartElement("Feature"))
            {
                reader.ReadStartElement("Feature");

                //Read the geometry
                if (reader.IsStartElement("Geometry"))
                {
                    reader.ReadStartElement("Geometry");
                    string wkt = reader.ReadString();
                    WKTReader wktReader = new WKTReader();
                    Shape = wktReader.Read(wkt);
                    reader.ReadEndElement();//Read [Geometry] end
                }

                //Read the attributes
                if (Attributes != null)
                {
                    Attributes.FromXML(reader);
                }
                else
                {
                    throw new NullReferenceException("Cannot read attribute values into feature. The [Attributes] property is NULL.");
                }

                reader.ReadEndElement();//Read [Feature] end
            }
        }
Example #15
0
        private bool CompareGeometries(Geometry a, Geometry b)
        {
            if (a != null && b != null && a.GetType().Name == b.GetType().Name)
            {
                Geometry aClone = (Geometry)a.Clone();
                Geometry bClone = (Geometry)b.Clone();

                aClone.Normalize();
                bClone.Normalize();

                return aClone.EqualsExact(bClone, m_dTolerance);
            }

            return false;
        }
Example #16
0
        /// <summary>
        /// Converts any <see cref="SharpMap.Geometries.Geometry"/> to the correspondant 
        /// <see cref=GisSharpBlog.NetTopologySuite.Geometries.Geometry"/>.
        /// </summary>
        /// <param name="geometry"></param>
        /// <returns></returns>
        public static Geometries.Geometry ToSharpMapGeometry(Geometry geometry)
        {
            if (geometry == null)
                throw new NullReferenceException("geometry");

            if (geometry.GetType() == typeof (GisSharpBlog.NetTopologySuite.Geometries.Point))
                return ToSharpMapPoint(geometry as GisSharpBlog.NetTopologySuite.Geometries.Point);

            else if (geometry.GetType() == typeof (LineString))
                return ToSharpMapLineString(geometry as LineString);

            else if (geometry.GetType() == typeof (Polygon))
                return ToSharpMapPolygon(geometry as Polygon);

            else if (geometry.GetType() == typeof (MultiPoint))
                return ToSharpMapMultiPoint(geometry as MultiPoint);

            else if (geometry.GetType() == typeof (MultiLineString))
                return ToSharpMapMultiLineString(geometry as MultiLineString);

            else if (geometry.GetType() == typeof (MultiPolygon))
                return ToSharpMapMultiPolygon(geometry as MultiPolygon);

            else if (geometry.GetType() == typeof (GeometryCollection))
                return ToSharpMapGeometryCollection(geometry as GeometryCollection);

            else throw new NotSupportedException("Type " + geometry.GetType().FullName + " not supported");
        }
Example #17
0
 internal static GeometryCollection ToNTSGeometryCollection(Geometries.GeometryCollection geometryCollection,
                                                            GeometryFactory factory)
 {
     Geometry[] geometries = new Geometry[geometryCollection.Collection.Count];
     int index = 0;
     foreach (Geometries.Geometry geometry in geometryCollection.Collection)
         geometries[index++] = ToNTSGeometry(geometry, factory);
     return factory.CreateGeometryCollection(geometries) as GeometryCollection;
 }
 /// <summary>
 /// Returns a list containing a Coordinate from each Polygon, LineString, and Point
 /// found inside the specified point. Thus, if the specified point is
 /// not a GeometryCollection, an empty list will be returned.
 /// </summary>
 public static IList GetCoordinates(Geometry geom)
 {
     IList pts = new ArrayList();
     geom.Apply(new ConnectedElementPointFilter(pts));
     return pts;
 }
Example #19
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="geometry"></param>
 /// <returns></returns>
 protected ShapeGeometryType GetShapeType(Geometry geometry)
 {
     if (geometry is Point)
         return ShapeGeometryType.Point;
     else if (geometry is LinearRing)
         return ShapeGeometryType.LineString;
     else if (geometry is LineString)
         return ShapeGeometryType.LineString;
     else if (geometry is Polygon)
         return ShapeGeometryType.Polygon;
     else if (geometry is MultiPoint)
         return ShapeGeometryType.MultiPoint;
     else if (geometry is MultiLineString)
         return ShapeGeometryType.LineString;
     else if (geometry is MultiPolygon)
         return ShapeGeometryType.Polygon;
     else throw new NotSupportedException("Unsupported Geometry implementation:" + geometry.GetType());            
 }
Example #20
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="geometry"></param>
 /// <param name="beWriter"></param>
 protected void WriteFeatureHeader(Geometry geometry, BigEndianBinaryWriter beWriter)
 {            
     // Write record number (big endian)
     beWriter.WriteIntBE((int)++recordCounter);            
     // Write content length (big endian) 
     int contentLength = shapeWriter.GetBytesLength(geometry);
     beWriter.WriteIntBE((int)(contentLength / 2));  // Write length in 16 bit words!
 }