예제 #1
0
        /// <summary>
        /// Converts a Geometry to its well-known text representation.
        /// </summary>
        /// <param name="geometry"> a Geometry to process
        /// </param>
        /// <returns>
        /// A &lt;Geometry Tagged Text&gt; string (see the OpenGIS Simple
        /// Features Specification)
        /// </returns>
        private void WriteFormatted(Geometry geometry, bool isFormatted,
                                    TextWriter writer)
        {
            m_bIsFormatted = isFormatted;

            if (m_bIncludeSRID)
            {
                // try writing..."SRID=4326;GEOMETRY(...)"
                IGeometryProperties properties = geometry.Properties;
                if (properties != null && properties.Count > 0 &&
                    properties.Contains("SRID"))
                {
                    int nSRID = (int)properties["SRID"];
                    if (nSRID > 0)
                    {
                        writer.Write("SRID=");
                        writer.Write(nSRID.ToString());
                        writer.Write(";");
                    }
                }
            }

            WriteGeometry(geometry, 0, writer);
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="strGeometry"></param>
        /// <param name="geometry"></param>
        /// <param name="addSRID"></param>
        /// <returns></returns>
        private bool WriteStartElement(XmlWriter writer, string strGeometry,
                                       Geometry geometry, bool addSRID)
        {
            bool usePrefix = false;

            if (m_bUsePrefix && (m_strPrefix != null && m_strPrefix.Length != 0))
            {
                usePrefix = true;
            }

            if (usePrefix)
            {
                writer.WriteStartElement(m_strPrefix, strGeometry, null);
            }
            else
            {
                writer.WriteStartElement(strGeometry);
            }

            IGeometryProperties properties = null;

            if (geometry != null)
            {
                properties = geometry.Properties;
            }
            if (properties != null && properties.Count > 0)
            {
                if (properties.Contains("GID"))
                {
                    string strGID = (string)properties["GID"];
                    if (strGID != null && strGID.Length > 0)
                    {
                        if (usePrefix)
                        {
                            writer.WriteAttributeString(m_strPrefix,
                                                        GeometryGml2.GmlGid, null, strGID);
                        }
                        else
                        {
                            writer.WriteAttributeString(GeometryGml2.GmlGid, strGID);
                        }
                    }
                }

                if (addSRID && properties.Contains("SRID"))
                {
                    int nSRID = (int)properties["SRID"];
                    if (nSRID > 0)
                    {
                        string strSRID = m_bUseEpsgSrs ?
                                         GeometryGml2.GmlAttrEpsgSrsname + nSRID.ToString() :
                                         nSRID.ToString();
                        if (usePrefix)
                        {
                            writer.WriteAttributeString(m_strPrefix,
                                                        GeometryGml2.GmlAttrSrsname, null, strSRID);
                        }
                        else
                        {
                            writer.WriteAttributeString(
                                GeometryGml2.GmlAttrSrsname, strSRID);
                        }
                    }
                }
            }

            return(usePrefix);
        }
예제 #3
0
        private bool WritePostGIS(Geometry geometry)
        {
            // 1. Determine the geometry parameters
            CoordinateType      coordType   = CoordinateType.Default;
            IGeometryProperties geomProp    = geometry.Properties;
            GeometryFactory     geomFactory = geometry.Factory;

            int          nDimension = geometry.CoordinateDimension;
            GeometryType geomType   = geometry.GeometryType;

            if (geomFactory != null)
            {
                coordType = geomFactory.CoordinateType;
            }
            int nSRID = -1;

            if (geomProp != null && geomProp.Contains("SRID"))
            {
                nSRID = Convert.ToInt32(geomProp["SRID"]);
            }

            // 2. Write the bye order
            WriteByteOrder();

            // 3. Write the geometry type word
            int  geomWkbType  = (int)GetWkbType(geomType);
            uint geomTypeWord = (uint)geomWkbType;

            if (nDimension > 2)
            {
                geomTypeWord |= 0x80000000;
            }
            if (coordType == CoordinateType.Measured)
            {
                geomTypeWord |= 0x40000000;
            }
            if (nSRID > 0)
            {
                geomTypeWord |= 0x20000000;
            }

            WriterGeometryType((int)geomTypeWord);

            // 4. Write the spatial reference ID, if any
            if (nSRID > 0)
            {
                WriteInt(nSRID);
            }

            if (geomType == GeometryType.Point)
            {
                return(WritePostGIS((Point)geometry));
            }
            else if (geomType == GeometryType.LineString)
            {
                return(WritePostGIS((LineString)geometry));
            }
            else if (geomType == GeometryType.Polygon)
            {
                return(WritePostGIS((Polygon)geometry));
            }
            else if (geomType == GeometryType.MultiPoint)
            {
                return(WritePostGIS((MultiPoint)geometry));
            }
            else if (geomType == GeometryType.MultiLineString)
            {
                return(WritePostGIS((MultiLineString)geometry));
            }
            else if (geomType == GeometryType.MultiPolygon)
            {
                return(WritePostGIS((MultiPolygon)geometry));
            }
            else if (geomType == GeometryType.GeometryCollection)
            {
                return(WritePostGIS((GeometryCollection)geometry));
            }
            else
            {
                throw new GeometryIOException("The well-known binary format for "
                                              + "this Geometry does not exist: " + geometry.Name);
            }
        }
예제 #4
0
        public byte[] Write(Geometry geometry, BytesOrder order)
        {
            if (geometry == null)
            {
                throw new ArgumentNullException("geometry");
            }

            m_objWriter.Initialize();
            m_objWriter.Order = order;

            GeometryWkbMode mode = GeometryWkbMode.Standard;

            GeometryFactory factory = geometry.Factory;

            if (factory != null)
            {
                CoordinateType coordType = factory.CoordinateType;
                if (coordType == CoordinateType.Measured)
                {
                    mode = GeometryWkbMode.PostGIS;
                }
                else
                {
                    IGeometryProperties properties = geometry.Properties;
                    if (properties != null && properties.Contains("SRID"))
                    {
                        mode = GeometryWkbMode.PostGIS;
                    }
                    else
                    {
                        int dim = factory.CoordinateDimension;
                        if (dim == 3)
                        {
                            mode = GeometryWkbMode.Proposed;
                        }
                    }
                }
            }

            bool bResult = false;

            byte[] geomBytes = null;

            switch (mode)
            {
            case GeometryWkbMode.Standard:
                bResult = WriteStandard(geometry);
                break;

            case GeometryWkbMode.Proposed:
                bResult = WriteProposed(geometry);
                break;

            case GeometryWkbMode.PostGIS:
                bResult = WritePostGIS(geometry);
                break;

            case GeometryWkbMode.Custom:
                bResult = WriteCustom(geometry);
                break;
            }

            if (bResult)
            {
                geomBytes = m_objWriter.GetBuffer();
            }

            m_objWriter.Uninitialize(); // reset the buffer

            return(geomBytes);
        }