private static void setupShapefile(HttpContext context, Map m)
        {
            GeometryServices geometryServices = new GeometryServices();

            string[] layernames = new[]
                                      {
                                          "Countries",
                                          "Rivers"/*,
                                          "Cities"*/
                                      };

            foreach (string s in layernames)
            {
                ShapeFileProvider shapeFile =
               new ShapeFileProvider(context.Server.MapPath(string.Format("~/App_Data/Shapefiles/{0}.shp", s)),
                                     geometryServices.DefaultGeometryFactory,
                                     geometryServices.CoordinateSystemFactory, false);
                shapeFile.IsSpatiallyIndexed = false;

                AppStateMonitoringFeatureProvider provider = new AppStateMonitoringFeatureProvider(shapeFile);

                GeoJsonGeometryStyle style = RandomStyle.RandomGeometryStyle();
                /* include GeoJson styles */
                style.IncludeAttributes = false;
                style.IncludeBBox = true;
                style.PreProcessGeometries = false;
                style.CoordinateNumberFormatString = "{0:F}";

                GeometryLayer geometryLayer = new GeometryLayer(s, style, provider);
                geometryLayer.Features.IsSpatiallyIndexed = false;
                m.AddLayer(geometryLayer);
                provider.Open();
            }

        }
 internal static GeometryLayer CreateFeatureFeatureLayer(IGeometryFactory geoFactory,
                                                         Boolean includeGeometryCollections)
 {
     GeometryLayer layer = new GeometryLayer("TestFeatures",
                                             CreateFeatureDatasource(geoFactory, includeGeometryCollections));
     return layer;
 }
        public void T02_CreateGeometryLayerFromSpatiaLite2()
        {
            SpatiaLite2Provider prov = new SpatiaLite2Provider(
                _geometryFactory, @"Data Source=test.sqlite", "TestFeatureDataTable");

            SharpMap.Layers.GeometryLayer gl = new SharpMap.Layers.GeometryLayer("test", prov);
            Assert.IsNotNull(gl);
            Assert.AreEqual("test", gl.LayerName);
            Assert.AreEqual(prov.Srid, gl.Srid);
        }
        public void T04_InsertInDataSource()
        {
            SpatiaLite2Provider prov = new SpatiaLite2Provider(
                _geometryFactory, @"Data Source=test.sqlite", "TestFeatureDataTable");

            GeometryLayer gl = new GeometryLayer("test", prov);
            gl.Select(new AllAttributesExpression());
            gl.Features.AcceptChanges();

            FeatureDataRow fdr = gl.Features.NewRow();

            fdr[0] = 999;
            fdr.Geometry = gl.GeometryFactory.WktReader.Read("POINT (-2 13)");
            fdr[1] = fdrLabel(fdr);
            gl.Features.AddRow(fdr);

            prov.Insert(gl.Features.GetChanges());

            //gl.SelectedFilter = new FeatureQueryExpression(
            //    new AttributeBinaryExpression(
            //        new PropertyNameExpression("OID"),
            //        BinaryOperator.Equals,
            //        (Int64)999));
            //Assert.AreEqual(5, gl.SelectedFeatures.Count);
            
            fdr = gl.Features.Find(999);
            Assert.IsNotNull(fdr);

        }
        public void T05_DeleteFromDataSource()
        {
            SpatiaLite2Provider prov = new SpatiaLite2Provider(
                _geometryFactory, @"Data Source=test.sqlite", "TestFeatureDataTable");
            
            GeometryLayer gl = new GeometryLayer(prov);
            gl.Select(new SpatialBinaryExpression(
                new ExtentsExpression(prov.GetExtents()), SpatialOperation.Contains, new ThisExpression()));
            gl.Features.AcceptChanges();

            //gl.Features.RemoveRow(gl.Features[4]);
            FeatureDataRow fdr = gl.Features.Find(999);
            fdr.Delete();
            //prov.Delete(gl.Features.GetChanges(DataRowState.Deleted));
            gl.Features.AcceptChanges();

            gl = new GeometryLayer(prov);
            gl.Select(new SpatialBinaryExpression(
                new ExtentsExpression(prov.GetExtents()), SpatialOperation.Contains, new ThisExpression()));
            gl.Features.AcceptChanges();
            //Assert.AreEqual(4, gl.Features.Rows.Count);

        }
        public void T03_MultipleSelects()
        {
            SpatiaLite2Provider prov = new SpatiaLite2Provider(
                _geometryFactory, @"Data Source=test.sqlite", "TestFeatureDataTable");
            GeometryLayer gl = new GeometryLayer("test", prov);

            gl.Select(new AllAttributesExpression());
            gl.Features.AcceptChanges();
            Assert.AreEqual(4, gl.Features.FeatureCount);
            Assert.AreEqual(_geometryFactory.CreateExtents2D(0,-3,15,20), gl.Features.Extents);

            gl = new GeometryLayer(prov);
            gl.Select(
                new SpatialBinaryExpression(
                    new ExtentsExpression(prov.GeometryFactory.CreateExtents2D(-1,-1,1,1)), 
                    SpatialOperation.Contains, 
                    new ThisExpression())
                    ); 
            Assert.AreEqual(1, gl.Features.Rows.Count);

            gl = new GeometryLayer(prov);
            gl.Select(
                new SpatialBinaryExpression(
                    new GeometryExpression(prov.GeometryFactory.WktReader.Read(("POLYGON((-1 -1, 0 1, 1 1, -1 -1))"))), 
                    SpatialOperation.Contains, 
                    new ThisExpression())
                    );
            Assert.AreEqual(1, gl.Features.Rows.Count);

            gl = new GeometryLayer(prov);
            gl.Select(
                new SpatialBinaryExpression(
                    new GeometryExpression(prov.GeometryFactory.WktReader.Read(("POLYGON((-1 -1, -1 1, 0 1, 0 -1, -1 -1))"))), 
                    SpatialOperation.Touches, 
                    new ThisExpression())
                    ); 
            Assert.AreEqual(1, gl.Features.Rows.Count, 1);

            gl = new GeometryLayer(prov);
            gl.Select(
                new SpatialBinaryExpression(
                    new ThisExpression(),
                    SpatialOperation.Within,
                    new GeometryExpression(prov.GeometryFactory.WktReader.Read(("POLYGON((-1 -1, 0 1, 1 1, -1 -1))"))))
                    );
            Assert.AreEqual(0, gl.Features.Rows.Count);

        }
        public void T02_CreateGeometryLayerFromSpatiaLite2()
        {
            SpatiaLite2Provider prov = new SpatiaLite2Provider(
                _geometryFactory, @"Data Source=test.sqlite", "TestFeatureDataTable");

            SharpMap.Layers.GeometryLayer gl = new SharpMap.Layers.GeometryLayer("test", prov);
            Assert.IsNotNull( gl );
            Assert.AreEqual("test", gl.LayerName);
            Assert.AreEqual(prov.Srid, gl.Srid);
        }
        public void T99_GeometryFromBinary()
        {
            PostGisProvider<Int64> prov = new PostGisProvider<Int64>(
                _geometryFactory, connectionString, "TestFeatureDataTable");
            GeometryLayer gl = new GeometryLayer("test", prov);

            gl.Select(new AllAttributesExpression());
            using (NpgsqlConnection cn = new NpgsqlConnection(connectionString))
            {
                cn.Open();
                NpgsqlCommand cm = new NpgsqlCommand(
                    "SELECT ST_SRID( ST_GeomFromWKB(:p0::bytea, -1) )", cn);
                NpgsqlParameter par = new NpgsqlParameter(":p0", DbType.Binary);
                par.Value = prov.GeometryFactory.WktReader.Read("POLYGON((-1 -1, 0 1, 1 -1, -1 -1))").AsBinary();
                cm.Parameters.Add(par);
                Debug.Write(cm.ExecuteScalar());
            }
        }
        public void T05_DeleteFromDataSource()
        {
            PostGisProvider<Int64> prov = new PostGisProvider<Int64>(
                _geometryFactory, connectionString, "TestFeatureDataTable");

            GeometryLayer gl = new GeometryLayer(prov);
            gl.Select(new SpatialBinaryExpression(
                          new ExtentsExpression(prov.GetExtents()), SpatialOperation.Contains, new ThisExpression()));
            gl.Features.AcceptChanges();

            //gl.Features.RemoveRow(gl.Features[4]);
            FeatureDataRow fdr = gl.Features.Find(999);
            fdr.Delete();
            //prov.Delete(gl.Features.GetChanges(DataRowState.Deleted));
            gl.Features.AcceptChanges();

            gl = new GeometryLayer(prov);
            gl.Select(new SpatialBinaryExpression(
                          new ExtentsExpression(prov.GetExtents()), SpatialOperation.Contains, new ThisExpression()));
            gl.Features.AcceptChanges();
            //Assert.AreEqual(4, gl.Features.Rows.TotalItemCount);
        }
        public void T03_SomeSelects()
        {
            PostGisProvider<Int64> prov = new PostGisProvider<Int64>(
                _geometryFactory, connectionString, "TestFeatureDataTable");
            GeometryLayer gl = new GeometryLayer("test", prov);

            gl.Select(new AllAttributesExpression());
            gl.Features.AcceptChanges();
            Assert.AreEqual(4, gl.Features.FeatureCount);
            Assert.AreEqual(_geometryFactory.CreateExtents2D(0, -3, 15, 20), gl.Features.Extents);

            gl = new GeometryLayer(prov);
            gl.Select(
                new SpatialBinaryExpression(
                    new ExtentsExpression(prov.GeometryFactory.CreateExtents2D(-1, -1, 1, 1)),
                    SpatialOperation.Contains,
                    new ThisExpression())
                );
            Assert.AreEqual(1, gl.Features.Rows.Count);

            gl = new GeometryLayer(prov);
            gl.Select(
                new SpatialBinaryExpression(
                    new GeometryExpression(prov.GeometryFactory.WktReader.Read(("POLYGON((-1 -1, 0 1, 1 -1, -1 -1))"))),
                    SpatialOperation.Contains,
                    new ThisExpression())
                );
            //SELECT testfeaturedatatable.poid,testfeaturedatatable.label,ST_AsBinary(xgeometryx)::bytea AS xgeometryx FROM public.testfeaturedatatable   WHERE  ( ST_Contains( ST_GeomFromWKB(:iparam0, -1), xgeometryx ))
            //SELECT testfeaturedatatable.poid,testfeaturedatatable.label,ST_AsBinary(xgeometryx)::bytea AS xgeometryx FROM public.testfeaturedatatable   WHERE  ( ST_Contains( ST_GeomFromText('POLYGON((-1 -1, 0 1, 1 -1, -1 -1))', -1), xgeometryx ))
            Assert.AreEqual(1, gl.Features.Rows.Count);

            gl = new GeometryLayer(prov);
            gl.Select(
                new SpatialBinaryExpression(
                    new GeometryExpression(
                        prov.GeometryFactory.WktReader.Read(("POLYGON((-1 -1, -1 1, 0 1, 0 -1, -1 -1))"))),
                    SpatialOperation.Touches,
                    new ThisExpression())
                );
            Assert.AreEqual(1, gl.Features.Rows.Count);

            //gl = new GeometryLayer(prov);
            //gl.Select(
            //    new SpatialBinaryExpression(
            //        new ThisExpression(),
            //        SpatialOperation.Within,
            //        new GeometryExpression(prov.GeometryFactory.WktReader.Read("POLYGON((-1 -1, 0 1, 1 -1, -1 -1))")))
            //        );
            //Assert.AreEqual(0, gl.Features.Rows.TotalItemCount);
        }
        public void T02_CreateGeometryLayerFromPostGis()
        {
            PostGisProvider<Int64> prov = new PostGisProvider<Int64>(
                _geometryFactory, connectionString, "TestFeatureDataTable");

            GeometryLayer gl = new GeometryLayer("test", prov);
            Assert.IsNotNull(gl);
            Assert.AreEqual("test", gl.LayerName);
            Assert.AreEqual(prov.Srid, gl.Srid);
        }
 public ILayer Create(String layerName, String connectionInfo)
 {
     ShapeFileProvider shapeFileData = new ShapeFileProvider(connectionInfo, _geometryFactory);
     GeometryLayer shapeFileLayer = new GeometryLayer(layerName, shapeFileData);
     return shapeFileLayer;
 }
		internal static GeometryLayer CreateFeatureFeatureLayer()
		{
			GeometryLayer layer = new GeometryLayer("TestFeatures", CreateFeatureDatasource());
			return layer;
		}
        private ILayer generateFeatureLayer()
        {
            VectorStyle style = new VectorStyle();
            String layerName;

            switch (_rnd.Next(3))
            {
                case 0:
                    {
                        KeyValuePair<String, Symbol2D> symbolEntry = getSymbolEntry(_rnd.Next(_symbolTable.Count));
                        style.Symbol = symbolEntry.Value;
                        layerName = symbolEntry.Key;
                    }
                    break;
                case 1:
                    {
                        KeyValuePair<String, StyleColor> colorEntry = getColorEntry(_rnd.Next(_colorTable.Count));
                        style.Line = new StylePen(colorEntry.Value, _rnd.NextDouble() * 3);
                        layerName = String.Format("{0} lines", colorEntry.Key);
                    }
                    break;
                case 2:
                    {
                        KeyValuePair<String, StyleColor> colorEntry = getColorEntry(_rnd.Next(_colorTable.Count));
                        style.Fill = new SolidStyleBrush(colorEntry.Value);
                        layerName = String.Format("{0} squares", colorEntry.Key);
                    }
                    break;
                default:
                    throw new InvalidOperationException();
            }

            GeometryLayer layer = new GeometryLayer(layerName, generateFeatureDataSource());
            return layer;
        }
        private void AddLayer()
        {
            using (ChooseDataSource choose = new ChooseDataSource())
            {
                if (choose.ShowDialog() == DialogResult.OK)
                {
                    IFeatureProvider prov = choose.Provider;
                    string name = choose.ProviderName;

                    workQueue.AddWorkItem(
                        string.Format("Loading Datasource {0}", name),
                        delegate
                            {
                                GeometryLayer lyr =
                                    new GeometryLayer(
                                        name,
                                        prov);
                                lyr.Features.IsSpatiallyIndexed = false;
                                prov.Open();


                                InvokeIfRequired(new Action(delegate
                                                                {
                                                                    if (Map.Layers.Count == 0)
                                                                    {
                                                                        if (lyr.SpatialReference != null)
                                                                            Map.SpatialReference = lyr.SpatialReference;
                                                                    }

                                                                    Map.Layers.Insert(0, lyr);

                                                                    lyr.Style = RandomStyle.RandomGeometryStyle();
                                                                    //lyr.Style = setGeometryStyle(lyr);

                                                                    if (Map.Layers.Count == 1)
                                                                    {
                                                                        mapViewControl1.Map = Map;

                                                                        layersView1.Map = Map;
                                                                        MapView.ZoomToExtents();
                                                                    }
                                                                }));
                            }, EnableDisableCommandsRequiringLayers
                        ,
                        delegate(Exception ex) { MessageBox.Show(string.Format("An error occured\n{0}\n{1}", ex.Message, ex.StackTrace)); });
                }
            }
        }
        private static void setupMsSqlSpatial(Map m)
        {
            string[] layernames = new[]
                                      {"Rivers",
                                          "Countries"
                                          /*,
                                          "Cities"*/
                                      };

            string sridstr = SridMap.DefaultInstance.Process(4326, "");

            foreach (string lyrname in layernames)
            {
                string tbl = lyrname;


                AppStateMonitoringFeatureProvider provider = new AppStateMonitoringFeatureProvider(
                    new MsSqlSpatialProvider(
                        new GeometryServices()[sridstr],
                        ConfigurationManager.ConnectionStrings["db"].ConnectionString,
                        "st",
                        "dbo",
                        tbl,
                        "oid",
                        "Geometry")
                        {
                            DefaultProviderProperties
                                = new ProviderPropertiesExpression(
                                new ProviderPropertyExpression[]
                                    {
                                        new WithNoLockExpression(true)
                                    })
                        });


                GeoJsonGeometryStyle style = new GeoJsonGeometryStyle();

                switch (tbl)
                {
                    case "Rivers":
                        {
                            StyleBrush brush = new SolidStyleBrush(StyleColor.Blue);
                            StylePen pen = new StylePen(brush, 1);
                            style.Enabled = true;
                            style.EnableOutline = true;
                            style.Line = pen;
                            style.Fill = brush;
                            break;
                        }

                    case "Countries":
                        {
                            StyleBrush brush = new SolidStyleBrush(new StyleColor(0, 0, 0, 255));
                            StylePen pen = new StylePen(brush, 2);
                            style.Enabled = true;
                            style.EnableOutline = true;
                            style.Line = pen;
                            style.Fill = new SolidStyleBrush(StyleColor.Green);

                            break;
                        }

                    default:
                        {
                            style = RandomStyle.RandomGeometryStyle();
                            style.MaxVisible = 100000;
                            break;
                        }
                }


                /* include GeoJson styles */
                style.IncludeAttributes = false;
                style.IncludeBBox = true;
                style.PreProcessGeometries = false;
                style.CoordinateNumberFormatString = "{0:F}";
                GeometryLayer layer = new GeometryLayer(tbl, style, provider);
                layer.Features.IsSpatiallyIndexed = false;
                layer.AddProperty(AppStateMonitoringFeatureLayerProperties.AppStateMonitor, provider.Monitor);
                m.AddLayer(layer);
            }
        }