Example #1
0
 /// <summary>
 /// Returns the geometry corresponding to the Object ID
 /// </summary>
 /// <param name="oid">Object ID</param>
 /// <returns>geometry</returns>
 public IGeometry GetGeometryByID(int oid)
 {
     GeoAPI.Geometries.IGeometry geom = null;
     using (System.Data.OleDb.OleDbConnection conn = new OleDbConnection(_ConnectionString))
     {
         string strSQL = "Select " + this.XColumn + ", " + this.YColumn + " FROM " + this.Table + " WHERE " + this.ObjectIdColumn + "=" + oid.ToString();
         using (System.Data.OleDb.OleDbCommand command = new OleDbCommand(strSQL, conn))
         {
             conn.Open();
             using (System.Data.OleDb.OleDbDataReader dr = command.ExecuteReader())
             {
                 if (dr.Read())
                 {
                     //If the read row is OK, create a point geometry from the XColumn and YColumn and return it
                     if (dr[0] != DBNull.Value && dr[1] != DBNull.Value)
                     {
                         geom = SharpMap.Converters.Geometries.GeometryFactory.CreatePoint((double)dr[0], (double)dr[1]);
                     }
                 }
             }
             conn.Close();
         }
     }
     return(geom);
 }
Example #2
0
        /// <summary>
        /// Returns the data associated with all the geometries that are intersected by 'geom'
        /// </summary>
        /// <param name="geometry">Geometry to intersect with</param>
        /// <param name="ds">FeatureDataSet to fill data into</param>
        public void ExecuteIntersectionQuery(IGeometry geometry, FeatureDataSet ds)
        {
            if (CoordinateTransformation != null)
            {
#if !DotSpatialProjections
                if (ReverseCoordinateTransformation != null)
                {
                    geometry = GeometryTransform.TransformGeometry(geometry, ReverseCoordinateTransformation.MathTransform,
                                                                   GeometryServiceProvider.Instance.CreateGeometryFactory((int)ReverseCoordinateTransformation.TargetCS.AuthorityCode));
                }
                else
                {
                    CoordinateTransformation.MathTransform.Invert();
                    geometry = GeometryTransform.TransformGeometry(geometry, CoordinateTransformation.MathTransform,
                                                                   GeometryServiceProvider.Instance.CreateGeometryFactory((int)CoordinateTransformation.SourceCS.AuthorityCode));
                    CoordinateTransformation.MathTransform.Invert();
                }
#else
                geometry = GeometryTransform.TransformGeometry(geometry,
                                                               CoordinateTransformation.Target,
                                                               CoordinateTransformation.Source,
                                                               CoordinateTransformation.SourceFactory);
#endif
            }

            lock (_dataSource)
            {
                _dataSource.Open();
                _dataSource.ExecuteIntersectionQuery(geometry, ds);
                _dataSource.Close();
            }
        }
Example #3
0
        /// <summary>
        /// Builds from the given provider.
        /// </summary>
        /// <param name="provider">
        /// The base <see cref="SharpMap.Data.Providers.IProvider"/>
        /// from witch initialize the <see cref="NtsProvider"/> instance.
        /// </param>
        private void BuildFromProvider(IProvider provider)
        {
            // Features list initialization
            _features = new List <Feature>(provider.GetFeatureCount());

            try
            {
                // Load all features from the given provider
                provider.Open();
                Collection <uint> ids = provider.GetObjectIDsInView(provider.GetExtents());
                foreach (uint id in ids)
                {
                    FeatureDataRow dataRow = provider.GetFeature(id);
                    GeoAPI.Geometries.IGeometry geometry   = GeometryConverter.ToNTSGeometry(dataRow.Geometry, _geometryFactory);
                    AttributesTable             attributes = new AttributesTable();
                    foreach (DataColumn column in dataRow.Table.Columns)
                    {
                        if (dataRow[column] == null || dataRow[column] is DBNull)
                        {
                            throw new ApplicationException("Null values not supported");
                        }
                        attributes.AddAttribute(column.ColumnName, dataRow[column]);
                    }
                    _features.Add(new Feature(geometry, attributes));
                }
            }
            finally
            {
                if (provider.IsOpen)
                {
                    provider.Close();
                }
            }
        }
Example #4
0
        /// <summary>
        /// Method to insert a new feature to the tree
        /// </summary>
        /// <param name="fid">The feature's id</param>
        /// <param name="geometry">The feature's geometry</param>
        public void Insert(uint fid, GeoAPI.Geometries.IGeometry geometry)
        {
            Interval x, y, z, m;

            GeometryMetricExtensions.GetMetric(geometry, out x, out y, out z, out m);
            Insert(fid, geometry.EnvelopeInternal, z, m);
        }
        public static SqlGeometry ToSqlGeometry(SMGeometry smGeometry)
        {
            SqlGeometryBuilder builder = new SqlGeometryBuilder();

            builder.SetSrid(smGeometry.SRID);

            SharpMapGeometryToSqlGeometry(builder, smGeometry);

            SqlGeometry g = builder.ConstructedGeometry;

            if (!g.STIsValid())
            {
                try
                {
                    g = g.Reduce(ReduceTolerance);
                    g = g.MakeValid();
                }
                catch (Exception ex)
                {
                    throw new SqlGeometryConverterException(ex, smGeometry);
                }
            }

            if (!g.STIsValid())
            {
                throw new SqlGeometryConverterException(smGeometry);
            }

            return(g);
        }
Example #6
0
        /// <summary>
        /// Returns the geometry corresponding to the Object ID
        /// </summary>
        /// <param name="oid">Object ID</param>
        /// <returns>geometry</returns>
        public override Geometry GetGeometryByID(uint oid)
        {
            Geometry geom = null;

            using (var conn = new OracleConnection(ConnectionString))
            {
                string strSql = "SELECT g." + GeometryColumn + ".Get_WKB() FROM " + Table + " g WHERE " + ObjectIdColumn +
                                "='" + oid.ToString(CultureInfo.InvariantCulture) + "'";
                conn.Open();
                using (var command = new OracleCommand(strSql, conn))
                {
                    using (OracleDataReader dr = command.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            if (dr[0] != DBNull.Value)
                            {
                                geom = GeometryFromWKB.Parse((byte[])dr[0], Factory);
                            }
                        }
                    }
                }
                conn.Close();
            }
            return(geom);
        }
Example #7
0
        /// <summary>
        /// Returns the geometry corresponding to the Object ID
        /// </summary>
        /// <param name="oid">Object ID</param>
        /// <returns>geometry</returns>
        public override Geometry GetGeometryByID(uint oid)
        {
            Geometry geom = null;

            using (var conn = new OracleConnection(ConnectionString))
            {
                string strSql = "SELECT g." + GeometryColumn + " as Geom FROM " + Table + " g WHERE " + ObjectIdColumn +
                                "='" + oid.ToString(CultureInfo.InvariantCulture) + "'";
                conn.Open();
                using (var command = new OracleCommand(strSql, conn))
                {
                    using (OracleDataReader dr = command.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            if (dr[0] != DBNull.Value)
                            {
                                var sdoGeom = dr[0] as SdoGeometry;
                                if (sdoGeom != null)
                                {
                                    geom = sdoGeom.AsGeometry();
                                }
                            }
                        }
                    }
                }
                conn.Close();
            }
            return(geom);
        }
Example #8
0
        /// <summary>
        /// Returns the features that intersects with 'geom'
        /// </summary>
        /// <param name="geom">Geometry</param>
        /// <returns>FeatureDataTable</returns>
        public FeatureDataTable ExecuteIntersectionQuery(Geometry geom)
        {
            var fds = new FeatureDataSet();

            ExecuteIntersectionQuery(geom, fds);
            return(fds.Tables[0]);
        }
Example #9
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="other"></param>
 /// <param name="tolerance"></param>
 /// <returns></returns>
 public override bool EqualsExact(GeoAPI.Geometries.IGeometry other, double tolerance)
 {
     if (!IsEquivalentClass(other))
     {
         return(false);
     }
     return(base.EqualsExact(other, tolerance));
 }
Example #10
0
 /// <summary>
 /// Returns the data associated with all the geometries that are intersected by 'geom'
 /// </summary>
 /// <param name="geom">Geometry to intersect with</param>
 /// <param name="ds">FeatureDataSet to fill data into</param>
 protected override void OnExecuteIntersectionQuery(Geometry geom, FeatureDataSet ds)
 {
     using (var ogrGeometry = OgrGeometry.CreateFromWkb(GeometryToWKB.Write(geom)))
     {
         _ogrLayer.SetSpatialFilter(ogrGeometry);
         ExecuteIntersectionQuery(ds);
     }
 }
Example #11
0
        void mapBox1_GeometryDefined(GeoAPI.Geometries.IGeometry geometry)
        {
            MessageBox.Show("Geometry defined!\r\n" + geometry);

            geoProvider.Geometries.Add(geometry);

            this.mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan;
            this.mapBox1.Refresh();
        }
Example #12
0
        /// <summary>
        /// Returns the data associated with all the geometries that are intersected by 'geom'
        /// </summary>
        /// <param name="geometry">Geometry to intersect with</param>
        /// <param name="ds">FeatureDataSet to fill data into</param>
        public void ExecuteIntersectionQuery(IGeometry geometry, FeatureDataSet ds)
        {
            geometry = ToSource(geometry);

            lock (_dataSource)
            {
                _dataSource.Open();
                _dataSource.ExecuteIntersectionQuery(geometry, ds);
                _dataSource.Close();
            }
        }
        /*
         * private static OpenGisGeometryType ParseGeometryType(string stGeometryType)
         * {
         *  switch (stGeometryType.ToUpper())
         *  {
         *      case "POINT":
         *          return OpenGisGeometryType.Point;
         *      case "LINESTRING":
         *          return OpenGisGeometryType.LineString;
         *      case "POLYGON":
         *          return OpenGisGeometryType.Polygon;
         *      case "MULTIPOINT":
         *          return OpenGisGeometryType.MultiPoint;
         *      case "MULTILINESTRING":
         *          return OpenGisGeometryType.MultiLineString;
         *      case "MULTIPOLYGON":
         *          return OpenGisGeometryType.MultiPolygon;
         *      case "GEOMETRYCOLLECTION":
         *          return OpenGisGeometryType.GeometryCollection;
         *  }
         *  throw new ArgumentException(String.Format("Invalid geometrytype '{0}'!", stGeometryType), "stGeometryType");
         * }
         */

        private static SMGeometryCollection SqlGeometryToSharpMapGeometryCollection(SqlGeometry geometry, Factory factory)
        {
            var fact  = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
            var geoms = new SMGeometry[(int)geometry.STNumGeometries()];

            for (var i = 1; i <= geometry.STNumGeometries(); i++)
            {
                geoms[i - 1] = ToSharpMapGeometry(geometry.STGeometryN(i), fact);
            }
            return(fact.CreateGeometryCollection(geoms));
        }
Example #14
0
        /// <summary>
        /// Returns the features that intersects with 'geom'
        /// </summary>
        /// <param name="geom"></param>
        /// <param name="ds">FeatureDataSet to fill data into</param>
        protected override void OnExecuteIntersectionQuery(Geometry geom, FeatureDataSet ds)
        {
            using (var conn = new OracleConnection(ConnectionString))
            {
                string strGeom = "MDSYS.SDO_GEOMETRY('" + geom.AsText() + "', #SRID#)";

                strGeom = strGeom.Replace("#SRID#", SRID > 0 ? SRID.ToString(Map.NumberFormatEnUs) : "NULL");

                strGeom = "SDO_RELATE(g." + GeometryColumn + ", " + strGeom +
                          ", 'mask=ANYINTERACT querytype=WINDOW') = 'TRUE'";

                string strSql = "SELECT g.* , g." + GeometryColumn + ").Get_WKB() As sharpmap_tempgeometry FROM " +
                                Table + " g WHERE ";

                if (!String.IsNullOrEmpty(_definitionQuery))
                {
                    strSql += DefinitionQuery + " AND ";
                }

                strSql += strGeom;

                using (var adapter = new OracleDataAdapter(strSql, conn))
                {
                    conn.Open();
                    adapter.Fill(ds);
                    conn.Close();
                    if (ds.Tables.Count > 0)
                    {
                        var fdt = new FeatureDataTable(ds.Tables[0]);
                        foreach (DataColumn col in ds.Tables[0].Columns)
                        {
                            if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry")
                            {
                                fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression);
                            }
                        }
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            var fdr = fdt.NewRow();
                            foreach (DataColumn col in ds.Tables[0].Columns)
                            {
                                if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry")
                                {
                                    fdr[col.ColumnName] = dr[col];
                                }
                            }
                            fdr.Geometry = GeometryFromWKB.Parse((byte[])dr["sharpmap_tempgeometry"], Factory);
                            fdt.AddRow(fdr);
                        }
                        ds.Tables.Add(fdt);
                    }
                }
            }
        }
Example #15
0
 /// <summary>
 /// Converts any <see cref="SharpMap.Geometries.Geometry"/> array to the correspondant 
 /// <see cref="GisSharpBlog.NetTopologySuite.Geometries.Geometry" /> array.
 /// </summary>
 public static NTSGeometry[] ToNTSGeometry(Geometries.Geometry[] geometries,
     IGeometryFactory factory)
 {
     NTSGeometry[] converted = new NTSGeometry[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 #16
0
        internal static NTSGeometryCollection ToNTSGeometryCollection(Geometries.GeometryCollection geom,
                                                                      IGeometryFactory factory)
        {
            NTSGeometry[] geometries = new NTSGeometry[geom.Collection.Count];
            int           index      = 0;

            foreach (Geometries.Geometry geometry in geom.Collection)
            {
                geometries[index++] = ToNTSGeometry(geometry, factory);
            }
            return(factory.CreateGeometryCollection(geometries) as NTSGeometryCollection);
        }
Example #17
0
 /// <summary>
 /// Returns the data associated with all the geometries that are intersected by 'geom'
 /// </summary>
 /// <param name="geom">Geometry to intersect with</param>
 /// <param name="ds">FeatureDataSet to fill data into</param>
 protected override void OnExecuteIntersectionQuery(Geometry geom, FeatureDataSet ds)
 {
     using (var ogrGeometry = OgrGeometry.CreateFromWkb(GeometryToWKB.Write(geom)))
     {
         OgrDataSource ogrDataSource;
         using (var ogrLayer = GetLayer(_layerIndex, out ogrDataSource))
         {
             ogrLayer.SetSpatialFilter(ogrGeometry);
             ExecuteIntersectionQuery(ds, ogrLayer);
             ogrDataSource.Dispose();
         }
     }
 }
Example #18
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="geom"></param>
        /// <param name="ds"></param>
        public void ExecuteIntersectionQuery(Geometry geom, FeatureDataSet ds)
        {
            GeoAPI.Geometries.IGeometry geometry  = GeometryConverter.ToNTSGeometry(geom, _geometryFactory);
            FeatureDataTable            dataTable = CreateFeatureDataTable();

            foreach (Feature feature in _features)
            {
                if (feature.Geometry.Intersects(geometry))
                {
                    CreateNewRow(dataTable, feature);
                }
            }

            ds.Tables.Add(dataTable);
        }
Example #19
0
        /// <summary>
        /// Converts any <see cref="SharpMap.Geometries.Geometry"/> array to the correspondant
        /// <see cref="GisSharpBlog.NetTopologySuite.Geometries.Geometry" /> array.
        /// </summary>
        public static NTSGeometry[] ToNTSGeometry(Geometries.Geometry[] geometries,
                                                  IGeometryFactory factory)
        {
            NTSGeometry[] converted = new NTSGeometry[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 #20
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="geom"></param>
        /// <param name="ds"></param>
        protected override void OnExecuteIntersectionQuery(Geometry geom, FeatureDataSet ds)
        {
            FeatureDataTable dataTable = CreateFeatureDataTable();

            dataTable.BeginLoadData();
            foreach (Feature feature in _features)
            {
                if (PreparedGeometry.Intersects(feature.Geometry))
                {
                    CreateNewRow(dataTable, feature);
                }
            }
            dataTable.EndLoadData();

            ds.Tables.Add(dataTable);
        }
        private void transform_Click(object sender, EventArgs e)
        {
            if (vlay == null)
            {
                return;
            }
            if (vlay.DataSource == null)
            {
                MessageBox.Show("请先载入shp文件");
            }
            else
            {
                if (!vlay.DataSource.IsOpen)
                {
                    vlay.DataSource.Open();
                }
                SharpMap.Data.Providers.ShapeFile shapefile = (SharpMap.Data.Providers.ShapeFile)vlay.DataSource;
                int    geoCount  = shapefile.GetFeatureCount();
                string writefile = null;
                saveFileDialog1.Filter = "(*.txt*)|*.txt";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    writefile = saveFileDialog1.FileName;
                }
                if (writefile == null)
                {
                    MessageBox.Show("输出文件不能为空,请正确选择");
                    return;
                }
                FileStream   fs = new FileStream(writefile, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                for (int i = 0; i < geoCount; i++)
                {
                    GeoAPI.Geometries.IGeometry geo = shapefile.GetGeometryByID(Convert.ToUInt32(i));
                    sw.Write(geo + "\n");
                }

                //   Console.WriteLine(geo);
                vlay.DataSource.Close();



                sw.Close();
                fs.Close();
                MessageBox.Show("文件已转换完毕!");
            }
        }
Example #22
0
        /// <summary>
        /// Throws NotSupportedException.
        /// </summary>
        /// <param name="geom"></param>
        public IEnumerable <IFeature> GetFeatures(GeoAPI.Geometries.IGeometry geom)
        {
            throw new NotSupportedException("GetFeatures(Geometry) is not supported by the OleDbPointProvider.");
            //When relation model has been implemented the following will complete the query

            /*
             * GetFeatures(geom.GetBoundingBox(), ds);
             * if (ds.Tables.Count > 0)
             * {
             *      for(int i=ds.Tables[0].Count-1;i>=0;i--)
             *      {
             *              if (!geom.Intersects(ds.Tables[0][i].Geometry))
             *                      ds.Tables.RemoveAt(i);
             *      }
             * }
             */
        }
Example #23
0
        /// <summary>
        /// Converts any <see cref="SharpMap.Geometries.Geometry"/> to the correspondant
        /// <see cref="GisSharpBlog.NetTopologySuite.Geometries.Geometry"/>.
        /// </summary>
        public static Geometries.Geometry ToSharpMapGeometry(NTSGeometry geometry)
        {
            if (geometry == null)
            {
                throw new NullReferenceException("geometry");
            }

            if (TypeOf(geometry, typeof(NTSPoint)))
            {
                return(ToSharpMapPoint(geometry as NTSPoint));
            }

            if (TypeOf(geometry, typeof(NTSLineString)))
            {
                return(ToSharpMapLineString(geometry as NTSLineString));
            }

            if (TypeOf(geometry, typeof(NTSPolygon)))
            {
                return(ToSharpMapPolygon(geometry as NTSPolygon));
            }

            if (TypeOf(geometry, typeof(NTSMultiPoint)))
            {
                return(ToSharpMapMultiPoint(geometry as NTSMultiPoint));
            }

            if (TypeOf(geometry, typeof(NTSMultiLineString)))
            {
                return(ToSharpMapMultiLineString(geometry as NTSMultiLineString));
            }

            if (TypeOf(geometry, typeof(NTSMultiPolygon)))
            {
                return(ToSharpMapMultiPolygon(geometry as NTSMultiPolygon));
            }

            if (TypeOf(geometry, typeof(NTSGeometryCollection)))
            {
                return(ToSharpMapGeometryCollection(geometry as NTSGeometryCollection));
            }

            var message = String.Format("Type {0} not supported", geometry.GetType().FullName);

            throw new NotSupportedException(message);
        }
Example #24
0
        /// <summary>
        /// Returns geometries within the specified bounding box
        /// </summary>
        /// <param name="bbox"></param>
        /// <returns></returns>
        public override Collection <Geometry> GetGeometriesInView(Envelope bbox)
        {
            var features = new Collection <Geometry>();

            using (var conn = new OracleConnection(ConnectionString))
            {
                //Get bounding box string
                string strBbox = GetBoxFilterStr(bbox);

                //string strSQL = "SELECT AsBinary(" + this.GeometryColumn + ") AS Geom ";
                string strSql = "SELECT g." + GeometryColumn + ".Get_WKB() ";
                strSql += " FROM " + Table + " g WHERE ";

                if (!String.IsNullOrEmpty(_definitionQuery))
                {
                    strSql += DefinitionQuery + " AND ";
                }

                strSql += strBbox;

                using (var command = new OracleCommand(strSql, conn))
                {
                    conn.Open();
                    using (OracleDataReader dr = command.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            if (dr[0] != DBNull.Value)
                            {
                                Geometry geom = GeometryFromWKB.Parse((byte[])dr[0], Factory);
                                if (geom != null)
                                {
                                    features.Add(geom);
                                }
                            }
                        }
                    }
                    conn.Close();
                }
            }
            return(features);
        }
Example #25
0
        /// <summary>
        /// Returns geometries within the specified bounding box
        /// </summary>
        /// <param name="bbox"></param>
        /// <returns></returns>
        public ICollection <IGeometry> GetGeometriesInView(IEnvelope bbox, double minGeometrySize)
        {
            Collection <IGeometry> features = new Collection <IGeometry>();

            using (SqlConnection conn = new SqlConnection(_ConnectionString))
            {
                string strSQL = "SELECT ST.AsBinary(" + this.BuildGeometryExpression() + ") ";
                strSQL += "FROM ST.FilterQuery" + this.BuildSpatialQuerySuffix() + "(" + this.BuildEnvelope(bbox) + ")";

                if (!String.IsNullOrEmpty(this.DefinitionQuery))
                {
                    strSQL += " WHERE " + this.DefinitionQuery;
                }

                if (!String.IsNullOrEmpty(this.OrderQuery))
                {
                    strSQL += " ORDER BY " + this.OrderQuery;
                }

                using (SqlCommand command = new SqlCommand(strSQL, conn))
                {
                    conn.Open();
                    using (SqlDataReader dr = command.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            if (dr[0] != DBNull.Value)
                            {
                                GeoAPI.Geometries.IGeometry geom = SharpMap.Converters.WellKnownBinary.GeometryFromWKB.Parse((byte[])dr[0]);
                                if (geom != null)
                                {
                                    features.Add(geom);
                                }
                            }
                        }
                    }
                    conn.Close();
                }
            }
            return(features);
        }
Example #26
0
        /// <summary>
        /// Returns the geometry corresponding to the Object ID
        /// </summary>
        /// <param name="oid">Object ID</param>
        /// <returns>geometry</returns>
        public override Geometry GetGeometryByID(uint oid)
        {
            DataRow[] rows;
            Geometry  geom = null;

            if (Table.Rows.Count == 0)
            {
                return(null);
            }

            string selectStatement = ObjectIdColumn + " = " + oid;

            rows = Table.Select(selectStatement);

            foreach (DataRow dr in rows)
            {
                geom = Factory.CreatePoint(new Coordinate((double)dr[XColumn], (double)dr[YColumn]));
            }

            return(geom);
        }
Example #27
0
        private static bool TypeOf(NTSGeometry geometry, Type type)
        {
            if (geometry == null)
            {
                throw new ArgumentNullException("geometry");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            var interfaces = geometry.GetType().GetInterfaces();

            foreach (Type item in interfaces)
            {
                if (item == type)
                {
                    return(true);
                }
            }
            return(false);
        }
Example #28
0
        /// <summary>
        /// Returns geometries within the specified bounding box
        /// </summary>
        /// <param name="bbox"></param>
        /// <returns></returns>
        public ICollection <IGeometry> GetGeometriesInView(IEnvelope bbox, double minGeometrySize)
        {
            Collection <GeoAPI.Geometries.IGeometry> features = new Collection <GeoAPI.Geometries.IGeometry>();

            using (SqlConnection conn = new SqlConnection(_ConnectionString))
            {
                string BoxIntersect = GetBoxClause(bbox);

                string strSQL = "SELECT " + this.GeometryColumn + " AS Geom ";
                strSQL += "FROM " + this.Table + " WHERE ";
                strSQL += BoxIntersect;
                if (!String.IsNullOrEmpty(_defintionQuery))
                {
                    strSQL += " AND " + this.DefinitionQuery;
                }

                using (SqlCommand command = new SqlCommand(strSQL, conn))
                {
                    conn.Open();
                    using (SqlDataReader dr = command.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            if (dr[0] != DBNull.Value)
                            {
                                GeoAPI.Geometries.IGeometry geom = SharpMap.Converters.WellKnownBinary.GeometryFromWKB.Parse((byte[])dr[0]);
                                if (geom != null)
                                {
                                    features.Add(geom);
                                }
                            }
                        }
                    }
                    conn.Close();
                }
            }
            return(features);
        }
Example #29
0
        public static SqlGeometry ToSqlGeometry(SMGeometry smGeometry)
        {
            SqlGeometryBuilder builder = new SqlGeometryBuilder();

//#if !DotSpatialProjections
//            if (smGeometry.SpatialReference != null)
//                builder.SetSrid((int) smGeometry.SpatialReference.AuthorityCode);
//#else
//            if (smGeometry.SpatialReference != null)
//                builder.SetSrid((int) smGeometry.SpatialReference.EpsgCode);
//#endif
//            else
            builder.SetSrid(smGeometry.SRID);

            SharpMapGeometryToSqlGeometry(builder, smGeometry);

            SqlGeometry g = builder.ConstructedGeometry;

            if (!g.STIsValid())
            {
                try
                {
                    g.Reduce(ReduceTolerance);
                    g.MakeValid();
                }
                catch (Exception ex)
                {
                    throw new SqlGeometryConverterException(ex, smGeometry);
                }
            }

            if (!g.STIsValid())
            {
                throw new SqlGeometryConverterException(smGeometry);
            }

            return(g);
        }
Example #30
0
        private List <LineString> GetAllLine(GeoAPI.Geometries.IGeometry polyline)
        {
            List <LineString> resultLineList = new List <LineString>();

            if (polyline.GeometryType != "LineString")
            {
                return(resultLineList);
            }
            int count = polyline.Coordinates.Count();

            for (int i = 0; i < count; i++)
            {
                if (i + 1 < count)
                {
                    List <Coordinate> temp = new List <Coordinate>()
                    {
                        polyline.Coordinates[i], polyline.Coordinates[i + 1]
                    };
                    LineString line = new LineString(temp.ToArray());
                    resultLineList.Add(line);
                }
            }
            return(resultLineList);
        }
Example #31
0
 /// <summary>
 /// Returns the geometry corresponding to the Object ID
 /// </summary>
 /// <param name="oid">Object ID</param>
 /// <returns>geometry</returns>
 public IGeometry GetGeometryByID(int oid)
 {
     GeoAPI.Geometries.IGeometry geom = null;
     using (SqlConnection conn = new SqlConnection(this.ConnectionString))
     {
         string strSQL = "SELECT ST.AsBinary(" + this.BuildGeometryExpression() + ") AS Geom FROM " + this.Table + " WHERE " + this.ObjectIdColumn + "='" + oid.ToString() + "'";
         conn.Open();
         using (SqlCommand command = new SqlCommand(strSQL, conn))
         {
             using (SqlDataReader dr = command.ExecuteReader())
             {
                 while (dr.Read())
                 {
                     if (dr[0] != DBNull.Value)
                     {
                         geom = SharpMap.Converters.WellKnownBinary.GeometryFromWKB.Parse((byte[])dr[0]);
                     }
                 }
             }
         }
         conn.Close();
     }
     return(geom);
 }
Example #32
0
        /// <summary>
        /// Returns the data associated with all the geometries that are intersected by 'geom'
        /// </summary>
        /// <param name="geom">Geometry to intersect with</param>
        /// <param name="ds">FeatureDataSet to fill data into</param>
        protected override void OnExecuteIntersectionQuery(Geometry geom, FeatureDataSet ds)
        {
            using (var ogrGeometry = OgrGeometry.CreateFromWkb(GeometryToWKB.Write(geom)))
            {
                _ogrLayer.SetSpatialFilter(ogrGeometry);
                ExecuteIntersectionQuery(ds);
            }

        }
 public SqlGeometryConverterException(Exception inner, SMGeometry geometry)
     : this("Failed to convert SharpMapGeometry", inner, geometry)
 {
 }
 private static void SharpMapGeometryToSqlGeometry(SqlGeometryBuilder geomBuilder, SMGeometry smGeometry)
 {
     switch (smGeometry.OgcGeometryType)
     {
         case SMGeometryType.Point:
             SharpMapPointToSqlGeometry(geomBuilder, smGeometry as SMPoint);
             break;
         case SMGeometryType.LineString:
             SharpMapLineStringToSqlGeometry(geomBuilder, smGeometry as SMLineString);
             break;
         case SMGeometryType.Polygon:
             SharpMapPolygonToSqlGeometry(geomBuilder, smGeometry as SMPolygon);
             break;
         case SMGeometryType.MultiPoint:
             SharpMapMultiPointToSqlGeometry(geomBuilder, smGeometry as SMMultiPoint);
             break;
         case SMGeometryType.MultiLineString:
             SharpMapMultiLineStringToSqlGeometry(geomBuilder, smGeometry as SMMultiLineString);
             break;
         case SMGeometryType.MultiPolygon:
             SharpMapMultiPolygonToSqlGeometry(geomBuilder, smGeometry as SMMultiPolygon);
             break;
         case SMGeometryType.GeometryCollection:
             SharpMapGeometryCollectionToSqlGeometry(geomBuilder, smGeometry as SMGeometryCollection);
             break;
         default:
             throw new ArgumentException(
                 String.Format("Cannot convert '{0}' geometry type", smGeometry.GeometryType), "smGeometry");
     }
 }
Example #35
0
 /// <summary>
 /// Returns the data associated with all the geometries that are intersected by 'geom'
 /// </summary>
 /// <param name="geometry">Geometry to intersect with</param>
 /// <param name="ds">FeatureDataSet to fill data into</param>
 public void ExecuteIntersectionQuery(Geometry geometry, FeatureDataSet ds)
 {
     ExecuteIntersectionQuery(geometry.EnvelopeInternal, ds);
 }
Example #36
0
 /// <summary>
 /// Converts any <see cref="GisSharpBlog.NetTopologySuite.Geometries.Geometry"/> array to the correspondant 
 /// <see cref="SharpMap.Geometries.Geometry"/> array.
 /// </summary>
 public static Geometries.Geometry[] ToSharpMapGeometry(NTSGeometry[] geometries)
 {
     Geometries.Geometry[] converted = new Geometries.Geometry[geometries.Length];
     int index = 0;
     foreach (NTSGeometry geometry in geometries)
         converted[index++] = ToSharpMapGeometry(geometry);
     if ((geometries.Length != converted.Length))
         throw new ApplicationException("Conversion error");
     return converted;
 }
        public static SqlGeometry ToSqlGeometry(SMGeometry smGeometry)
        {
            SqlGeometryBuilder builder = new SqlGeometryBuilder();
            //#if !DotSpatialProjections
            //            if (smGeometry.SpatialReference != null)
            //                builder.SetSrid((int) smGeometry.SpatialReference.AuthorityCode);
            //#else
            //            if (smGeometry.SpatialReference != null)
            //                builder.SetSrid((int) smGeometry.SpatialReference.EpsgCode);
            //#endif
            //            else
                builder.SetSrid(smGeometry.SRID);

            SharpMapGeometryToSqlGeometry(builder, smGeometry);

            SqlGeometry g = builder.ConstructedGeometry;
            if (!g.STIsValid())
            {
                try
                {
                    g.Reduce(ReduceTolerance);
                    g.MakeValid();
                }
                catch (Exception ex)
                {
                    throw new SqlGeometryConverterException(ex, smGeometry);
                }
            }

            if (!g.STIsValid())
                throw new SqlGeometryConverterException(smGeometry);

            return g;
        }
Example #38
0
        private static bool TypeOf(NTSGeometry geometry, Type type)
        {
            if (geometry == null)
                throw new ArgumentNullException("geometry");
            if (type == null)
                throw new ArgumentNullException("type");

            var interfaces = geometry.GetType().GetInterfaces();
            foreach (Type item in interfaces)
                if (item == type)
                    return true;
            return false;
        }
Example #39
0
        /// <summary>
        /// Converts any <see cref="SharpMap.Geometries.Geometry"/> to the correspondant 
        /// <see cref="GisSharpBlog.NetTopologySuite.Geometries.Geometry"/>.
        /// </summary>
        public static Geometries.Geometry ToSharpMapGeometry(NTSGeometry geometry)
        {
            if (geometry == null)
                throw new NullReferenceException("geometry");

            if (TypeOf(geometry, typeof(NTSPoint)))
                return ToSharpMapPoint(geometry as NTSPoint);

            if (TypeOf(geometry, typeof (NTSLineString)))
                return ToSharpMapLineString(geometry as NTSLineString);

            if (TypeOf(geometry, typeof (NTSPolygon)))
                return ToSharpMapPolygon(geometry as NTSPolygon);

            if (TypeOf(geometry, typeof (NTSMultiPoint)))
                return ToSharpMapMultiPoint(geometry as NTSMultiPoint);

            if (TypeOf(geometry, typeof (NTSMultiLineString)))
                return ToSharpMapMultiLineString(geometry as NTSMultiLineString);

            if (TypeOf(geometry, typeof (NTSMultiPolygon)))
                return ToSharpMapMultiPolygon(geometry as NTSMultiPolygon);

            if (TypeOf(geometry, typeof (NTSGeometryCollection)))
                return ToSharpMapGeometryCollection(geometry as NTSGeometryCollection);

            var message = String.Format("Type {0} not supported", geometry.GetType().FullName);
            throw new NotSupportedException(message);
        }
Example #40
0
 internal static NTSGeometryCollection ToNTSGeometryCollection(Geometries.GeometryCollection geom,
     IGeometryFactory factory)
 {
     NTSGeometry[] geometries = new NTSGeometry[geom.Collection.Count];
     int index = 0;
     foreach (Geometries.Geometry geometry in geom.Collection)
         geometries[index++] = ToNTSGeometry(geometry, factory);
     return factory.CreateGeometryCollection(geometries) as NTSGeometryCollection;
 }
Example #41
0
        /// <summary>
        /// Returns the features that intersects with 'geom'
        /// </summary>
        /// <param name="geom"></param>
        /// <param name="ds">FeatureDataSet to fill data into</param>
        protected override void OnExecuteIntersectionQuery(Geometry geom, FeatureDataSet ds)
        {
            using (var conn = new OracleConnection(ConnectionString))
            {
                string strGeom = "MDSYS.SDO_GEOMETRY('" + geom.AsText() + "', #SRID#)";

                strGeom = strGeom.Replace("#SRID#", SRID > 0 ? SRID.ToString(Map.NumberFormatEnUs) : "NULL");

                strGeom = "SDO_RELATE(g." + GeometryColumn + ", " + strGeom +
                          ", 'mask=ANYINTERACT querytype=WINDOW') = 'TRUE'";

                string strSql = "SELECT g.* , g." + GeometryColumn + ").Get_WKB() As sharpmap_tempgeometry FROM " +
                                Table + " g WHERE ";

                if (!String.IsNullOrEmpty(_definitionQuery))
                    strSql += DefinitionQuery + " AND ";

                strSql += strGeom;

                using (var adapter = new OracleDataAdapter(strSql, conn))
                {
                    conn.Open();
                    adapter.Fill(ds);
                    conn.Close();
                    if (ds.Tables.Count > 0)
                    {
                        var fdt = new FeatureDataTable(ds.Tables[0]);
                        foreach (DataColumn col in ds.Tables[0].Columns)
                            if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry")
                                fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression);
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            var fdr = fdt.NewRow();
                            foreach (DataColumn col in ds.Tables[0].Columns)
                                if (col.ColumnName != GeometryColumn && col.ColumnName != "sharpmap_tempgeometry")
                                    fdr[col.ColumnName] = dr[col];
                            fdr.Geometry = GeometryFromWKB.Parse((byte[]) dr["sharpmap_tempgeometry"], Factory);
                            fdt.AddRow(fdr);
                        }
                        ds.Tables.Add(fdt);
                    }
                }
            }
        }
 public SqlGeometryConverterException(SMGeometry geometry)
     : this("Failed to convert SharpMapGeometry", geometry)
 {
     Geometry = geometry;
 }
Example #43
0
        /// <summary>
        /// Returns the features that intersects with 'geom'
        /// </summary>
        /// <param name="geom"></param>
        /// <param name="ds">FeatureDataSet to fill data into</param>
        protected override void OnExecuteIntersectionQuery(Geometry geom, FeatureDataSet ds)
        {
            using (var conn = new OracleConnection(ConnectionString))
            {
                string strGeom = "MDSYS.SDO_GEOMETRY('" + geom.AsText() + "', #SRID#)";

                strGeom = strGeom.Replace("#SRID#", SRID > 0 ? SRID.ToString(Map.NumberFormatEnUs) : "NULL");

                strGeom = "SDO_RELATE(g." + GeometryColumn + ", " + strGeom +
                          ", 'mask=ANYINTERACT querytype=WINDOW') = 'TRUE'";

                string strSql = "SELECT * FROM " +
                                Table + " g WHERE ";

                if (!String.IsNullOrEmpty(_definitionQuery))
                    strSql += DefinitionQuery + " AND ";

                strSql += strGeom;

                using (var adapter = new OracleDataAdapter(strSql, conn))
                {
                    using (var sourceDataSet = new DataSet())
                    {
                        conn.Open();
                        adapter.Fill(sourceDataSet);
                        conn.Close();
                        if (sourceDataSet.Tables.Count > 0)
                        {
                            var fdt = new FeatureDataTable(sourceDataSet.Tables[0]);
                            foreach (DataColumn col in sourceDataSet.Tables[0].Columns)
                                if (string.Compare(col.ColumnName, GeometryColumn, CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase) != 0)
                                    fdt.Columns.Add(col.ColumnName, col.DataType, col.Expression);
                            foreach (DataRow dr in sourceDataSet.Tables[0].Rows)
                            {
                                var fdr = fdt.NewRow();
                                foreach (DataColumn col in sourceDataSet.Tables[0].Columns)
                                    if (string.Compare(col.ColumnName, GeometryColumn, CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase) != 0)
                                        fdr[col.ColumnName] = dr[col];
                                var sdoGeometry = dr[GeometryColumn] as SdoGeometry;

                                if (sdoGeometry != null)
                                {
                                    fdr.Geometry = sdoGeometry.AsGeometry();
                                }

                                fdt.AddRow(fdr);
                            }
                            ds.Tables.Add(fdt);
                        }
                    }
                }
            }
        }
Example #44
0
 public FeatureDataTable QueryFeatures(Geometry geom, double distance)
 {
     throw new NotSupportedException();
 }
Example #45
0
 /// <summary>
 /// Returns the features that intersects with 'geom'
 /// </summary>
 /// <param name="geom">Geometry</param>
 /// <returns>FeatureDataTable</returns>
 public FeatureDataTable ExecuteIntersectionQuery(Geometry geom)
 {
     var fds = new FeatureDataSet();
     ExecuteIntersectionQuery(geom, fds);
     return fds.Tables[0];
 }
Example #46
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="geom"></param>
        /// <param name="ds"></param>
        protected override void OnExecuteIntersectionQuery(Geometry geom, FeatureDataSet ds)
        {
            FeatureDataTable dataTable = CreateFeatureDataTable();
            dataTable.BeginLoadData();
            foreach (Feature feature in _features)
                if (PreparedGeometry.Intersects(feature.Geometry))
                    CreateNewRow(dataTable, feature);
            dataTable.EndLoadData();

            ds.Tables.Add(dataTable);
        }
 public SqlGeometryConverterException(string message, Exception inner, SMGeometry geometry)
     : base(message, inner)
 {
     Geometry = geometry;
 }
 /*
 private static OpenGisGeometryType ParseGeometryType(string stGeometryType)
 {
     switch (stGeometryType.ToUpper())
     {
         case "POINT":
             return OpenGisGeometryType.Point;
         case "LINESTRING":
             return OpenGisGeometryType.LineString;
         case "POLYGON":
             return OpenGisGeometryType.Polygon;
         case "MULTIPOINT":
             return OpenGisGeometryType.MultiPoint;
         case "MULTILINESTRING":
             return OpenGisGeometryType.MultiLineString;
         case "MULTIPOLYGON":
             return OpenGisGeometryType.MultiPolygon;
         case "GEOMETRYCOLLECTION":
             return OpenGisGeometryType.GeometryCollection;
     }
     throw new ArgumentException(String.Format("Invalid geometrytype '{0}'!", stGeometryType), "stGeometryType");
 }
 */
 private static SMGeometryCollection SqlGeometryToSharpMapGeometryCollection(SqlGeometry geometry, Factory factory)
 {
     var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
     var geoms = new SMGeometry[(int)geometry.STNumGeometries()];
     for (var i = 1; i <= geometry.STNumGeometries(); i++)
         geoms[i-1] = ToSharpMapGeometry(geometry.STGeometryN(i), fact);
     return fact.CreateGeometryCollection(geoms);
 }