Exemple #1
0
        protected virtual Geometry Transform(GeometryCollection geom, Geometry parent)
        {
            GeometryList transGeomList = new GeometryList();

            for (int i = 0; i < geom.NumGeometries; i++)
            {
                Geometry transformGeom = Transform(geom[i]);
                if (transformGeom == null)
                {
                    continue;
                }
                if (pruneEmptyGeometry && transformGeom.IsEmpty)
                {
                    continue;
                }

                transGeomList.Add(transformGeom);
            }

            if (preserveGeometryCollectionType)
            {
                return(geomFactory.CreateGeometryCollection(transGeomList.ToArray()));
            }

            return(geomFactory.BuildGeometry(transGeomList));
        }
        private GeometryCollection EditGeometryCollection(GeometryCollection collection,
                                                          IGeometryEdit operation)
        {
            GeometryCollection newCollection =
                (GeometryCollection)operation.Edit(collection, m_objFactory);

            GeometryList geometries = new GeometryList();

            for (int i = 0; i < newCollection.NumGeometries; i++)
            {
                Geometry geometry = Edit(newCollection.GetGeometry(i), operation);

                if (geometry.IsEmpty)
                {
                    continue;
                }

                geometries.Add(geometry);
            }

            GeometryType geomType = newCollection.GeometryType;

            if (geomType == GeometryType.MultiPoint)
            {
                return(m_objFactory.CreateMultiPoint(geometries.ToPointArray()));
            }

            if (geomType == GeometryType.MultiLineString)
            {
                return(m_objFactory.CreateMultiLineString(
                           geometries.ToLineStringArray()));
            }

            if (geomType == GeometryType.MultiPolygon)
            {
                return(m_objFactory.CreateMultiPolygon(geometries.ToPolygonArray()));
            }

            return(m_objFactory.CreateGeometryCollection(geometries.ToArray()));
        }
Exemple #3
0
        /// <summary>
        /// Creates a GeometryCollection 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 &lt;GeometryCollection Text&gt;.
        /// </param>
        /// <returns>
        /// A GeometryCollection specified by the next token in the stream.
        /// </returns>
        /// <exception cref="GeometryIOException">
        /// If the coordinates used to create a Polygon shell and holes do
        /// not form closed linestrings, or if an unexpected token was
        /// encountered.
        /// </exception>
        /// <exception cref="IOException">
        /// If an I/O error occurs.
        /// </exception>
        private GeometryCollection ReadGeometryCollection(StreamTokenizer tokenizer)
        {
            string nextToken = GetNextEmptyOrOpener(tokenizer);

            if (nextToken.Equals(WktEmpty))
            {
                return(m_objFactory.CreateGeometryCollection(new Geometry[] {}));
            }

            GeometryList geometries = new GeometryList();
            Geometry     geometry   = ReadGeometry(tokenizer);

            geometries.Add(geometry);
            nextToken = GetNextCloserOrComma(tokenizer);
            while (nextToken.Equals(TokenComma))
            {
                geometry = ReadGeometry(tokenizer);

                geometries.Add(geometry);
                nextToken = GetNextCloserOrComma(tokenizer);
            }

            return(m_objFactory.CreateGeometryCollection(geometries.ToArray()));
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        private GeometryCollection ReadGeometryCollection(XmlReader reader)
        {
            string strSRID = null;
            string strGID  = null;
            int    nSRID   = -1;

            if (reader.Name == GeometryGml2.GmlMultiGeometry && reader.HasAttributes)
            {
                ReadGeometryAttributes(reader, ref strSRID, ref strGID);
                if (strSRID != null && strSRID.Length > 0)
                {
                    nSRID = Convert.ToInt32(strSRID);
                }
            }

            GeometryList geometries = new GeometryList();

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == GeometryGml2.GmlMultiGeometry)
                    {
                        if (reader.HasAttributes)
                        {
                            ReadGeometryAttributes(reader, ref strSRID, ref strGID);
                            if (strSRID != null && strSRID.Length > 0)
                            {
                                nSRID = Convert.ToInt32(strSRID);
                            }
                        }
                    }
                    else if (reader.Name == GeometryGml2.GmlPoint)
                    {
                        Point geometry = ReadPoint(reader);
                        if (geometry != null)
                        {
                            geometries.Add(geometry);
                        }
                    }
                    else if (reader.Name == GeometryGml2.GmlLineString)
                    {
                        LineString geometry = ReadLineString(reader);
                        if (geometry != null)
                        {
                            geometries.Add(geometry);
                        }
                    }
                    else if (reader.Name == GeometryGml2.GmlPolygon)
                    {
                        Polygon geometry = ReadPolygon(reader);
                        if (geometry != null)
                        {
                            geometries.Add(geometry);
                        }
                    }
                    else if (reader.Name == GeometryGml2.GmlMultiPoint)
                    {
                        MultiPoint geometry = ReadMultiPoint(reader);
                        if (geometry != null)
                        {
                            geometries.Add(geometry);
                        }
                    }
                    else if (reader.Name == GeometryGml2.GmlMultiLineString)
                    {
                        MultiLineString geometry = ReadMultiLineString(reader);
                        if (geometry != null)
                        {
                            geometries.Add(geometry);
                        }
                    }
                    else if (reader.Name == GeometryGml2.GmlMultiPolygon)
                    {
                        MultiPolygon geometry = ReadMultiPolygon(reader);
                        if (geometry != null)
                        {
                            geometries.Add(geometry);
                        }
                    }
                    else if (reader.Name == GeometryGml2.GmlMultiGeometry)
                    {
                        GeometryCollection geometry = ReadGeometryCollection(reader);
                        if (geometry != null)
                        {
                            geometries.Add(geometry);
                        }
                    }
                }
                else if (reader.NodeType == XmlNodeType.EndElement)
                {
                    if (reader.Name == GeometryGml2.GmlMultiGeometry)
                    {
                        break;
                    }
                }
            }

            if (geometries.Count > 0)
            {
                GeometryCollection geometry = m_objFactory.CreateGeometryCollection(
                    geometries.ToArray());

                if (nSRID > 0 || (strGID != null && strGID.Length > 0))
                {
                    geometry.CreateProperties();
                    if (nSRID > 0)
                    {
                        geometry.Properties.Add("SRID", nSRID);
                    }
                    if (strGID != null && strGID.Length > 0)
                    {
                        geometry.Properties.Add("GID", strGID);
                    }
                }

                return(geometry);
            }

            return(null);
        }
Exemple #5
0
        /// <summary>
        /// Build an appropriate Geometry, MultiGeometry, or GeometryCollection
        /// to contain the <see cref="Geometry"/> instances in it.
        /// </summary>
        /// <param name="geometryList"> the <see cref="Geometry"/> instances to combine
        /// </param>
        /// <returns>           a Geometry of the "smallest", "most
        /// type-specific" class that can contain the elements of geomList.
        /// </returns>
        /// <remarks>
        /// For example:
        /// <list type="number">
        /// <item>
        /// <description>
        /// If geomList Contains a single Polygon, the Polygon is returned.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// If geomList Contains several Polygons, a MultiPolygon is returned.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// If geomList Contains some Polygons and some LineStrings, a GeometryCollection is
        /// returned.
        /// </description>
        /// </item>
        /// <item>
        /// <description>
        /// If geomList is empty, an empty GeometryCollection is returned.
        /// </description>
        /// </item>
        /// </list>
        /// Note that this method does not "flatten" Geometries in the input, and hence if
        /// any MultiGeometries are contained in the input a GeometryCollection containing
        /// them will be returned.
        /// </remarks>
        public virtual Geometry BuildGeometry(GeometryList geometryList)
        {
            if (geometryList == null)
            {
                throw new ArgumentNullException("geometryList");
            }

            GeometryType geomClass       = GeometryType.None;
            bool         isHeterogeneous = false;

            int nCount = geometryList.Count;

            for (int i = 0; i < nCount; i++)
            {
                Geometry     geom      = geometryList[i];
                GeometryType partClass = geom.GeometryType;
                if (geomClass == GeometryType.None)
                {
                    geomClass = partClass;
                }

                if (partClass != geomClass)
                {
                    isHeterogeneous = true;
                }
            }

            // for the empty geometry, return an empty GeometryCollection
            if (geomClass == GeometryType.None)
            {
                return(CreateGeometryCollection(null));
            }

            if (isHeterogeneous)
            {
                return(CreateGeometryCollection(geometryList.ToArray()));
            }
            // at this point we know the collection is hetereogenous.
            // Determine the type of the result from the first Geometry in the list
            // this should always return a geometry, since otherwise an empty collection would have already been returned
//			IGeometryEnumerator iTemp = geometryList.GetEnumerator();
//			iTemp.MoveNext();
//			Geometry geom0 = iTemp.Current;
            Geometry geom0        = geometryList[0];
            bool     isCollection = nCount > 1;

            if (isCollection)
            {
                GeometryType geomType = geom0.GeometryType;

                if (geomType == GeometryType.Polygon)
                {
                    return(CreateMultiPolygon(geometryList.ToPolygonArray()));
                }
                else if (geomType == GeometryType.LineString ||
                         geomType == GeometryType.LinearRing)
                {
                    return(CreateMultiLineString(geometryList.ToLineStringArray()));
                }
                else if (geomType == GeometryType.Point)
                {
                    return(CreateMultiPoint(geometryList.ToPointArray()));
                }
                Debug.Assert(false, "Should never reach here");
            }

            return(geom0);
        }