public void displayPredictionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (SelectedPredictions.Count == 1)
            {
                if (SelectedPredictions[0] != threatMap.DisplayedPrediction)
                {
                    Prediction p = SelectedPredictions[0];

                    Thread displayThread = new Thread(new ThreadStart(delegate()
                        {
                            List<Thread> threads = new List<Thread>();

                            Thread evalThread = new Thread(new ThreadStart(delegate()
                                {
                                    DiscreteChoiceModel.Evaluate(p, PlotHeight, PlotHeight);
                                    SetPredictionsTooltip(p);
                                }));
                            evalThread.Start();
                            threads.Add(evalThread);

                            double pointDistanceThreshold = 100;

                            List<Overlay> overlays = new List<Overlay>();
                            Thread areaT = new Thread(new ParameterizedThreadStart(o =>
                                {
                                    Area area = o as Area;
                                    NpgsqlCommand command = DB.Connection.NewCommand(null);
                                    lock (overlays) { overlays.Add(new Overlay(area.Name, Geometry.GetPoints(command, area.Shapefile.GeometryTable, ShapefileGeometry.Columns.Geometry, ShapefileGeometry.Columns.Id, pointDistanceThreshold), Color.Black, true, 0)); }
                                    DB.Connection.Return(command.Connection);
                                }));

                            areaT.Start(p.PredictionArea);
                            threads.Add(areaT);

                            if (p.Model is IFeatureBasedDCM)
                            {
                                ICollection<Feature> features = (p.Model as IFeatureBasedDCM).Features;
                                if (features.Count > 0)
                                {
                                    Dictionary<string, int> featureIdViewPriority = new Dictionary<string, int>();
                                    foreach (Feature f in features.OrderBy(f => f.Id))
                                        featureIdViewPriority.Add(f.Id, featureIdViewPriority.Count + 1);

                                    string minId = features.Min(f => f.Id);
                                    foreach (Feature f in features)
                                    {
                                        Thread t = new Thread(new ParameterizedThreadStart(o =>
                                            {
                                                Feature feature = o as Feature;
                                                if (feature.EnumType == typeof(FeatureBasedDCM.FeatureType) && (feature.EnumValue.Equals(FeatureBasedDCM.FeatureType.MinimumDistanceToGeometry) ||
                                                                                                                feature.EnumValue.Equals(FeatureBasedDCM.FeatureType.GeometryDensity)))
                                                {
                                                    Shapefile shapefile = new Shapefile(int.Parse(feature.PredictionResourceId));
                                                    NpgsqlCommand command = DB.Connection.NewCommand(null);
                                                    List<List<PointF>> points = Geometry.GetPoints(command, shapefile.GeometryTable, ShapefileGeometry.Columns.Geometry, ShapefileGeometry.Columns.Id, pointDistanceThreshold);
                                                    DB.Connection.Return(command.Connection);
                                                    lock (overlays) { overlays.Add(new Overlay(shapefile.Name, points, ColorPalette.GetColor(), false, featureIdViewPriority[f.Id])); }
                                                }
                                            }));

                                        t.Start(f);
                                        threads.Add(t);
                                    }
                                }
                            }

                            foreach (Thread t in threads)
                                t.Join();

                            overlays.Sort();
                            overlays.Reverse();

                            threatMap.Display(p, overlays);

                            if (threatMap.DisplayedPrediction != null)
                                Invoke(new Action(RefreshAssessments));
                        }));

                    displayThread.Start();
                }
            }
            else
                MessageBox.Show("Select a single prediction to display.");
        }
        public void Unitialize()
        {
            cb.Items.Clear();
            if (!m_HavePanel)
            {
                this.Hide();
            }

            // Jiri Kadlec 2/14/2008 selected shapes shouldn't be cleared when closing the form ( bug #766 )
            //if(m_SelShape != -1)
            //{
            //    m_parent.m_MapWin.View.ClearSelectedShapes();
            //    m_parent.m_MapWin.View.SelectedShapes.AddByIndex(m_SelShape,YELLOW);
            //}

            m_SelShape  = -1;
            m_ShapeFile = null;
            m_shpIndex  = null;
        }
        // see: https://github.com/NetTopologySuite/NetTopologySuite/issues/111
        public void issue_111_multiPointhandler_with_invalid_values()
        {
            var factory = GeometryFactory.Default;

            var p  = factory.CreatePoint(new CoordinateZ(0, 0));
            var mp = factory.CreateMultiPoint(new[] { p });

            Geometry[] arr        = new[] { mp, GeometryCollection.Empty };
            var        geometries = factory.CreateGeometryCollection(arr);

            var shapeType = Shapefile.GetShapeType(geometries);

            Assert.AreEqual(ShapeGeometryType.MultiPointZM, shapeType);

            string tempPath = Path.GetTempFileName();
            var    sfw      = new ShapefileWriter(Path.GetFileNameWithoutExtension(tempPath), shapeType);

            Assert.Throws <ArgumentException>(() => sfw.Write(geometries));
        }
예제 #4
0
        public void CopyAttributesToClipped()
        {
            var target = new ClipPolygonWithPolygon();

            // load input 1 Shapefile as IFeatureSet
            IFeatureSet europeShape = Shapefile.OpenFile(@"Data\ClipPolygonWithPolygonTests\EUR_countries.shp");

            // load input 2 Shapefile as IFeatureSet
            IFeatureSet belgiumShape = Shapefile.OpenFile(@"Data\ClipPolygonWithPolygonTests\Belgium.shp");

            // set output file as IFeatureSet shapefile
            IFeatureSet outputShape = new Shapefile()
            {
                Filename = FileTools.GetTempFileName(".shp")
            };

            target.Execute(europeShape, belgiumShape, outputShape, new MockProgressHandler());

            // the output file needs to be closed and re-opened, because the DataTable in memory does not match the DataTable on disk
            outputShape.Close();
            var outputFile = FeatureSet.Open(outputShape.Filename);

            try
            {
                // output shapefile attribute columns should match the input 1 attribute columns
                Assert.That(outputFile.DataTable.Columns[0].Caption.Equals("ID"));
                Assert.That(outputFile.DataTable.Columns[1].Caption.Equals("Name"));

                string[,] dataValues = { { "BE", "Belgium" }, { "DE", "Germany" }, { "LU", "Luxembourg" } };

                var mpCount = 0;
                foreach (var feature in outputFile.Features)
                {
                    Assert.That(feature.DataRow.ItemArray.Length == 2 && feature.DataRow.ItemArray[0].Equals(dataValues[mpCount, 0]) && feature.DataRow.ItemArray[1].Equals(dataValues[mpCount, 1]));
                    mpCount++;
                }
                Assert.That(mpCount == 3);
            }
            finally
            {
                FileTools.DeleteShapeFile(outputShape.Filename);
            }
        }
예제 #5
0
    public override BasicInfo GetBasicInfo(string filename)
    {
        BasicInfo info = new BasicInfo();

        try
        {
            var shapefile = new Shapefile(filename);
            info.bounds   = shapefile.BoundingBox;
            info.isRaster = false;

            //TestPrintShapefile(shapefile);
        }
        catch (Exception e)
        {
            Debug.LogException(e);
            return(null);
        }
        return(info);
    }
예제 #6
0
        public static int MapTripWaypoints(List <TripWaypoint> waypoints, out int shpIndex, out List <int> handles, GPS gps, string filename, bool showInMap = true)
        {
            shpIndex = -1;
            handles  = new List <int>();
            var utils       = new MapWinGIS.Utils();
            var shpfileName = "";

            if (showInMap)
            {
                if (waypoints.Count > 0)
                {
                    Shapefile sf = null;
                    sf          = ShapefileFactory.PointsFromWayPointList(waypoints, out handles, gps.DeviceName, filename);
                    shpfileName = "Trip waypoints";
                    int h = MapLayersHandler.AddLayer(sf, shpfileName, uniqueLayer: true, layerKey: sf.Key, rejectIfExisting: true);
                }
            }
            return(MapLayersHandler.CurrentMapLayer.Handle);
        }
예제 #7
0
        private Polygon GetLuster()
        {
            Shapefile indexMapFile = Shapefile.OpenFile("kommune2018.shp");

            indexMapFile.Reproject(ProjectionInfo.FromEpsgCode(4326));
            Polygon polygon = null;

            // Get the map index from the Feature data
            for (int i = 0; i < indexMapFile.DataTable.Rows.Count; i++)
            {
                // Get the feature
                IFeature feature = indexMapFile.Features.ElementAt(i);
                if ((string)feature.DataRow[1] == "Luster")
                {
                    polygon = feature.BasicGeometry as Polygon;
                }
            }
            return(polygon);
        }
예제 #8
0
        // <summary>
        // Handles ShapeHighlighted event and shows attributes of the selected shape in the label
        // </summary>
        void AxMap1ShapeHighlighted(object sender, _DMapEvents_ShapeHighlightedEvent e)
        {
            Shapefile sf = axMap1.get_Shapefile(e.layerHandle);

            if (sf != null)
            {
                string s = "";
                for (int i = 0; i < sf.NumFields; i++)
                {
                    string val = sf.get_CellValue(i, e.shapeIndex).ToString();
                    if (val == "")
                    {
                        val = "null";
                    }
                    s += sf.Table.Field[i].Name + ":" + val + "; ";
                }
                m_label.Text = s;
            }
        }
예제 #9
0
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            string    filename = ReadFile();
            var       shp      = Shapefile.OpenFile(filename);
            IMapLayer layer    = null;

            layer = map.Layers.Add(shp) as IMapLayer;
            if (layer is MapPointLayer)
            {
                file_layer.Add(filename, layer as MapPointLayer);
                comboBox1.Items.Add(filename);
                flag = true;
            }
            else
            {
                map.Layers.Remove(layer);
                MessageBox.Show("要素集格式错误,需要点类型图层!", "TIN提示信息", MessageBoxButtons.OKCancel);
            }
        }
예제 #10
0
        /// <summary>
        /// 增加Shapefile图层
        /// </summary>
        /// <param name="shp"></param>
        internal void AddLayer(Shapefile shp)
        {
            myMap.AddLayer(new MyLayer(shp));
            TreeNode layerNode = new TreeNode(shp.Name, 2, 2);

            layerNode.Checked = true;
            centerLngLat      = shp.CenterPos;
            centerXY          = ETCProjection.LngLat2XY(centerLngLat); //同步更新屏幕中心点投影坐标系坐标
            treeViewLayers.Nodes[0].Nodes.Insert(0, layerNode);
            treeViewLayers.ExpandAll();
            double x      = ETCProjection.Longitude2X(shp.Xmax) - ETCProjection.Longitude2X(shp.Xmin);
            double y      = ETCProjection.Latitude2Y(shp.Ymax) - ETCProjection.Latitude2Y(shp.Ymin);
            double scale1 = x / Width;
            double scale2 = y / Height;

            Ratio      = Math.Max(scale1, scale2);
            scaleIndex = maxZoomLevel;
            UpdateMapImg();
        }
예제 #11
0
        private void CheckNewMethods()
        {
            axMap1.DeserializeLayer(1, "Test");
            var projection = new GeoProjection();

            projection.ImportFromProj4("g");
            var t = tkSavingMode.modeXMLOverwrite;

            axMap1.SaveMapState("r", true, true);

            axMap1.set_LayerSkipOnSaving(1, true);

            var sf = new Shapefile();
            var reprojectedCount = 0;

            sf.Reproject(projection, ref reprojectedCount);
            var geoProjection = sf.GeoProjection;
            var sfSimple      = sf.SimplifyLines(10, false);

            var gridHeader = new GridHeader();
            var gridHeaderGeoProjection = gridHeader.GeoProjection;

            var ext = axMap1.MaxExtents;

            var utils = new Utils();

            utils.ClipGridWithPolygon("test", sfSimple.Shape[0], "new", false);

            utils.Polygonize(string.Empty, string.Empty, 1, false, string.Empty, "ESRI Shapefile", string.Empty);

            utils.GDALInfo(string.Empty, string.Empty);
            utils.TranslateRaster(string.Empty, string.Empty, string.Empty);

            utils.OGRInfo(string.Empty, string.Empty);
            utils.OGR2OGR(string.Empty, string.Empty, string.Empty);
            utils.GDALAddOverviews(string.Empty, string.Empty, string.Empty);
            utils.GDALBuildVrt(string.Empty, string.Empty);
            utils.GDALRasterize(string.Empty, string.Empty, string.Empty);
            utils.GDALWarp(string.Empty, string.Empty, string.Empty);

            var ds = new GdalDataset();
            var subDatasetCount = ds.SubDatasetCount;
        }
예제 #12
0
        // see: https://github.com/NetTopologySuite/NetTopologySuite/issues/111
        public void issue_111_multipointhandler_with_invalid_values()
        {
            IGeometryFactory factory = GeometryFactory.Default;

            IPoint      p  = factory.CreatePoint(new Coordinate(0, 0));
            IMultiPoint mp = factory.CreateMultiPoint(new[] { p });

            IGeometry[]         arr        = new[] { mp, GeometryCollection.Empty };
            IGeometryCollection geometries = factory.CreateGeometryCollection(arr);

            ShapeGeometryType shapeType = Shapefile.GetShapeType(geometries);

            Assert.AreEqual(ShapeGeometryType.MultiPointZM, shapeType);

            string          tempPath = Path.GetTempFileName();
            ShapefileWriter sfw      = new ShapefileWriter(Path.GetFileNameWithoutExtension(tempPath), shapeType);

            sfw.Write(geometries);
        }
        public static void CategorizeNumericPointLayer1(Shapefile sf, List <double> breaks, int classificationField = 1)
        {
            for (int b = 0; b < breaks.Count; b++)
            {
                var cat = sf.Categories.Add(b.ToString());
                cat.DrawingOptions.LineColor = new Utils().ColorByName(tkMapColor.White);
                cat.DrawingOptions.PointSize = PointSizeOfMaxCategory * ((float)(b + 1) / breaks.Count);
            }
            var cat0 = sf.Categories.Add("zero");

            cat0.DrawingOptions.LineColor   = new Utils().ColorByName(tkMapColor.White);
            cat0.DrawingOptions.PointSize   = 0;
            cat0.DrawingOptions.FillVisible = false;
            cat0.DrawingOptions.LineVisible = false;

            for (int n = 0; n < sf.NumShapes; n++)
            {
                double v = (int)sf.CellValue[classificationField, n];
                if (v > 0)
                {
                    for (int c = 0; c < breaks.Count; c++)
                    {
                        if (c + 1 < breaks.Count)
                        {
                            if (v >= breaks[c] && v < breaks[c + 1])
                            {
                                sf.ShapeCategory[n] = c;
                                break;
                            }
                        }
                        else
                        {
                            sf.ShapeCategory[n] = breaks.Count - 1;
                        }
                    }
                }
                else
                {
                    sf.ShapeCategory3[n] = cat0;
                }
            }
        }
예제 #14
0
        private void ClipGridWithPolygon()
        {
            var workingFolder = @"C:\dev\TestingScripts\ScriptData\ClipGridWithPolygon";
            var tempFolder    = @"C:\dev\TestingScripts\Temp\ClipGridWithPolygon";

            var gridFilename = Path.Combine(workingFolder, "AHN2.asc");
            var sf           = new Shapefile();

            sf.Open(Path.Combine(workingFolder, "Areas.shp"));
            var numShapes    = sf.NumShapes;
            var clippedWrong = 0;
            var clippedGood  = 0;

            // Needed for the new method:
            var utils = new Utils();
            var grd   = new Grid();

            grd.Open(gridFilename);

            for (var i = 0; i < numShapes; i++)
            {
                var polygon    = sf.Shape[i];
                var resultGrid = Path.Combine(tempFolder, "AHN2_clipped" + i + ".asc");

                // Using thr mwGeoProc version takes almost 4 hours with this data:
                // if (MapWinGeoProc.SpatialOperations.ClipGridWithPolygon(ref gridFilename, ref polygon, ref resultGrid))
                if (utils.ClipGridWithPolygon2(grd, polygon, resultGrid, false))
                {
                    clippedGood++;
                }
                else
                {
                    clippedWrong++;
                }
            }

            var msg = clippedWrong == 0
                ? "All tests successful!"
                : $"{clippedGood} clippings went OK, {clippedWrong} went wrong";

            MessageBox.Show(msg);
        }
예제 #15
0
        private void OnWindowLoaded(object sender, RoutedEventArgs e)
        {
            summaryGrid.Columns.Add(new DataGridTextColumn {
                Header = "Property", Binding = new Binding("Name")
            });
            summaryGrid.Columns.Add(new DataGridTextColumn {
                Header = "Number of items", Binding = new Binding("Count")
            });
            summaryGrid.Columns.Add(new DataGridTextColumn {
                Header = "Sum", Binding = new Binding("SumAsString")
            });
            summaryGrid.Columns.Add(new DataGridTextColumn {
                Header = "Minimum", Binding = new Binding("MinAsString")
            });
            summaryGrid.Columns.Add(new DataGridTextColumn {
                Header = "Maximum", Binding = new Binding("MaxAsString")
            });
            summaryGrid.Columns.Add(new DataGridTextColumn {
                Header = "Average", Binding = new Binding("AverageAsString")
            });
            _sf = (Shapefile)MapWindowManager.MapLayersHandler.CurrentMapLayer.LayerObject;
            for (int x = 0; x < _sf.NumFields; x++)
            {
                switch (_sf.Field[x].Type)
                {
                case FieldType.DOUBLE_FIELD:
                    _dictNameFieldIndexType.Add(x, "Double");
                    _dictSFColumns.Add(x, new List <double>());
                    break;

                case FieldType.INTEGER_FIELD:
                    _dictNameFieldIndexType.Add(x, "Int");
                    _dictSFColumns.Add(x, new List <int>());
                    break;

                case FieldType.DATE_FIELD:
                    _dictNameFieldIndexType.Add(x, "DateTime");
                    _dictSFColumns.Add(x, new List <DateTime>());
                    break;
                }
            }
        }
예제 #16
0
        private static bool ImportShapefilesFromFolder()
        {
            var ds = new OgrDatasource();

            if (!ds.Open(CONNECTION_STRING))
            {
                Debug.Print("Failed to establish connection: " + ds.GdalLastErrorMsg);
            }
            else
            {
                string path  = @"d:\data\sf\london";
                var    files = Directory.GetFiles(path, "*.shp");
                foreach (var file in files)
                {
                    var sf = new Shapefile();
                    if (!sf.Open(file))
                    {
                        Debug.Print("Failed to open shapefile: {0}\n{1}", file, sf.ErrorMsg[sf.LastErrorCode]);
                    }
                    else
                    {
                        string name = Path.GetFileNameWithoutExtension(file);
                        if (!ds.ImportShapefile(sf, name, "OVERWRITE=YES", tkShapeValidationMode.NoValidation))
                        {
                            Debug.Print("Failed to import shapefile: " + name);
                        }
                        else
                        {
                            Debug.Print("Layer was imported: " + name);
                            var layer = ds.GetLayerByName(name);
                            if (layer != null)
                            {
                                Debug.Print("Imported features count: " + layer.FeatureCount);
                                layer.Close();
                            }
                        }
                    }
                }
                ds.Close();
            }
            return(true);
        }
예제 #17
0
        public void AddField()
        {
            bool result;
            var  sf = new Shapefile {
                GlobalCallback = this
            };

            try
            {
                // Create in-memory shapefile
                result = sf.CreateNewWithShapeID(string.Empty, ShpfileType.SHP_POINT);
                Assert.IsTrue(result, "Could not create shapefile");

                Assert.IsTrue(sf.EditingShapes, "Shapefile is not in edit shapes mode");
                Assert.IsTrue(sf.EditingTable, "Shapefile is not in edit table mode");

                // Add fields:
                Assert.IsTrue(sf.Table.EditingTable, "Table is not in edit table mode");
                // This should work:
                var fieldIndex = sf.Table.EditAddField("date", FieldType.STRING_FIELD, 0, 50);
                Assert.AreEqual(sf.NumFields - 1, fieldIndex, "Could not add string field");
                // This should work:
                fieldIndex = sf.Table.EditAddField("double", FieldType.DOUBLE_FIELD, 10, 20);
                Assert.AreEqual(sf.NumFields - 1, fieldIndex, "Could not add double field");

                // This should fail, because width cannot be 0:
                fieldIndex = sf.Table.EditAddField("date", FieldType.STRING_FIELD, 50, 0);
                Assert.AreEqual(-1, fieldIndex, "Field is not added but -1 is not returned. ");
                Console.WriteLine("Expected error: " + sf.Table.ErrorMsg[sf.Table.LastErrorCode]);

                // This should fail, because precsion cannot be 0 when type is double:
                fieldIndex = sf.Table.EditAddField("date", FieldType.DOUBLE_FIELD, 0, 20);
                Assert.AreEqual(-1, fieldIndex, "Field is not added but -1 is not returned. ");
                Console.WriteLine("Expected error: " + sf.Table.ErrorMsg[sf.Table.LastErrorCode]);
            }
            finally
            {
                // Close the shapefile:
                result = sf.Close();
                Assert.IsTrue(result, "Could not close shapefile");
            }
        }
예제 #18
0
        private void UnselectAllLayer()
        {
            foreach (ILayer layer in MapControlTools.Layers)
            {
                Shapefile sf = AxMap.get_Shapefile(layer.Handle);
                if (sf != null)
                {
                    sf.Identifiable   = false;
                    sf.Selectable     = false;
                    sf.SelectionColor = utils.ColorByName(tkMapColor.Yellow);

                    if (layer.GetType() == typeof(ResTBDamagePotentialLayer))
                    {
                        ResTBDamagePotentialLayer resTBDamagePotentialLayer = (ResTBDamagePotentialLayer)layer;

                        sf = AxMap.get_Shapefile(resTBDamagePotentialLayer.PointHandle);
                        sf.Identifiable = false;
                        sf.Selectable   = false;
                        sf = AxMap.get_Shapefile(resTBDamagePotentialLayer.LineHandle);
                        sf.Identifiable = false;
                        sf.Selectable   = false;
                        sf = AxMap.get_Shapefile(resTBDamagePotentialLayer.PolygonHandle);
                        sf.Identifiable = false;
                        sf.Selectable   = false;
                    }
                    else if (layer.GetType() == typeof(ResTBRiskMapLayer))
                    {
                        ResTBRiskMapLayer resTBRiskMapLayer = (ResTBRiskMapLayer)layer;

                        sf = AxMap.get_Shapefile(resTBRiskMapLayer.PointHandle);
                        sf.Identifiable = false;
                        sf.Selectable   = false;
                        sf = AxMap.get_Shapefile(resTBRiskMapLayer.LineHandle);
                        sf.Identifiable = false;
                        sf.Selectable   = false;
                        sf = AxMap.get_Shapefile(resTBRiskMapLayer.PolygonHandle);
                        sf.Identifiable = false;
                        sf.Selectable   = false;
                    }
                }
            }
        }
예제 #19
0
        /// <summary>
        /// 图层排序
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        private string[] sortLayer(string[] files)
        {
            //图层顺序控制处理,使传进来的图层按照点,线,面由上至下叠置
            List <Shapefile> shapefiles = new List <Shapefile>();

            foreach (string file in files)
            {
                Shapefile sf = new Shapefile();
                sf.Open(file);
                shapefiles.Add(sf);
            }
            //新图层排序
            shapefiles.Sort(layerComparator);
            string[] result = new string[shapefiles.Count];
            for (int i = 0; i < shapefiles.Count; i++)
            {
                result[i] = shapefiles[i].Filename;
            }
            return(result);
        }
예제 #20
0
        static void ImportCountryLayer(string path)
        {
            using (var shapefile = new Shapefile(path))
            {
                foreach (var shape in shapefile)
                {
                    ShapePolygon polygon = shape as ShapePolygon;

                    var country = new Country();
                    country.Name  = shape.GetMetadata("COUNTRY");
                    country.Shape = Poly2Str(polygon.Parts);


                    //country.ChineseName = shape.GetMetadata("cname");
                    country.ID = db.Id(1);

                    db.Insert <Country>("Country", country);
                }
            }
        }
예제 #21
0
        /// <summary>
        /// Calculates the maximium width of the icon for the layer going through all categories
        /// </summary>
        /// <returns></returns>
        protected internal int get_MaxIconWidth(Shapefile sf)
        {
            if (sf == null)
            {
                return(0);
            }

            var maxWidth = GetCategoryWidth(sf.DefaultDrawingOptions);

            for (var i = 0; i < sf.Categories.Count; i++)
            {
                var width = GetCategoryWidth(sf.Categories.Item[i].DrawingOptions);
                if (width > maxWidth)
                {
                    maxWidth = width;
                }
            }

            return(maxWidth);
        }
예제 #22
0
        /// <summary>
        /// 计算图标(icon)的最大宽度
        /// </summary>
        public int Get_MaxIconWidth(Shapefile sf)
        {
            if (sf == null)
            {
                return(0);
            }

            var maxWidth = this.Get_CategoryWidth(sf.DefaultDrawingOptions);

            for (var i = 0; i < sf.Categories.Count; i++)
            {
                var width = this.Get_CategoryWidth(sf.Categories.get_Item(i).DrawingOptions);
                if (width > maxWidth)
                {
                    maxWidth = width;
                }
            }

            return(maxWidth);
        }
예제 #23
0
        /// <summary>
        /// Creates a new in-memory FeatureSet not bound to any datasource.
        /// </summary>
        public FeatureSet(GeometryType geomType, ZValueType zValue = ZValueType.None, bool addShapeIdField = true)
        {
            if (geomType == GeometryType.None)
            {
                throw new ArgumentException("Invalid geometry type.");
            }

            var shpType = GeometryHelper.GeometryType2ShpType(geomType, zValue);

            _shapefile = new Shapefile();

            if (!addShapeIdField)
            {
                _shapefile.CreateNew("", shpType);
            }
            else
            {
                _shapefile.CreateNewWithShapeID("", shpType);
            }
        }
예제 #24
0
        public void ClipRasterWithPolygonTest()
        {
            var folder = Common.AbsolutePath("Data");

            var shapeFilePath  = Path.Combine(folder, "elbe_watershed1.shp");
            var rasterFilePath = Path.Combine(folder, "kriging.bgd");
            var resultFilePath = Path.Combine(folder, "clipResult.bgd");

            var lClipPolygon = Shapefile.OpenFile(shapeFilePath);
            var lGridToClip  = Raster.OpenFile(rasterFilePath, false);

            var lGridAfterClip = new Raster {
                Filename = resultFilePath
            };

            ClipRaster.ClipRasterWithPolygon(lClipPolygon.Features[0], lGridToClip, lGridAfterClip.Filename);
            var ras2 = Raster.Open(lGridAfterClip.Filename);

            Assert.AreEqual(lGridToClip.NoDataValue, ras2.NoDataValue);
        }
예제 #25
0
 protected virtual void Dispose(bool disposing)
 {
     if (!_disposed)
     {
         if (disposing)
         {
             _frameWidthDict.Clear();
             _frameWidthDict = null;
         }
         if (_shapeFileMask != null)
         {
             _shapeFileMask.EditClear();
             _shapeFileMask.Close();
             _shapeFileMask = null;
         }
         MapLayersHandler = null;
         _axMap           = null;
         _disposed        = true;
     }
 }
예제 #26
0
        public static Byte[] GetMap(MapRequest mapReq)
        {
            string filepath = @"D:\MyDocuments\pkumap\";
            Bitmap tempbmp  = new Bitmap(mapReq.Width, mapReq.Height);

            for (int i = 0; i < mapReq.layers.Length; i++)
            {
                string       filename = filepath + mapReq.layers[i] + ".shp";
                Shapefile    shp      = new Shapefile(filename);
                FeatureClass fc       = shp.GetFeature();
                tempbmp = fc.DrawBMP(mapReq.bbox, tempbmp, mapReq.styles[0]);
            }

            MemoryStream ms = new MemoryStream();

            tempbmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            byte[] bitmapData = new Byte[ms.Length];
            bitmapData = ms.ToArray();
            return(bitmapData);
        }
예제 #27
0
        public static Dictionary <int, int> BuildFieldMap(this Shapefile source, Shapefile target)
        {
            var dict = new Dictionary <int, int>();

            for (int i = 0; i < source.NumFields; i++)
            {
                var fld       = source.Field[i];
                var fldTarget = target.FieldByName[fld.Name];
                if (fldTarget == null || fld.Type != fldTarget.Type)
                {
                    continue;
                }
                int targetIndex = target.Table.FieldIndexByName[fldTarget.Name];
                if (targetIndex != -1)
                {
                    dict.Add(i, targetIndex);
                }
            }
            return(dict);
        }
예제 #28
0
 private void LoadShapefileHotspots(string shapefilePath)
 {
     log.Info(string.Format("Parsing shapefile: {0}", shapefilePath));
     using (Shapefile shp = new Shapefile(shapefilePath))
     {
         foreach (Shape shape in shp)
         {
             if (shape.Type == ShapeType.Point)
             {
                 int confidence;
                 if (int.TryParse(shape.GetMetadata("confidence"), out confidence))
                 {
                     ShapePoint shapePoint = shape as ShapePoint;
                     hotspots.Add(new Hotspot(shapePoint.Point.Y, shapePoint.Point.X, confidence));
                 }
             }
         }
     }
     log.Info(string.Format("Cumulative hotspots parsed: {0}", hotspots.Count));
 }
예제 #29
0
 private void CreateCars(Extents ext, Shapefile paths, int numCars)
 {
     Cars.Clear();
     for (int i = 0; i < numCars; i++)
     {
         var    path = paths.Shape[0]; //paths.Shape[_random.Next(paths.NumShapes - 1)];
         double x    = ext.xMin + (ext.xMax - ext.xMin) * _random.NextDouble();
         double y    = ext.yMin + (ext.yMax - ext.yMin) * _random.NextDouble();
         var    car  = new Car(path, _random)
         {
             Id      = i,
             X       = x,
             Y       = y,
             Speed   = 30 + 50 * _random.NextDouble(),
             CarType = (CarType)_random.Next(3),
             State   = (CarState)_random.Next(3),
         };
         Cars.Add(car);
     }
 }
예제 #30
0
    public void Load(string filename, RectangleD boundingBox)
    {
        var fileInfo = new System.IO.FileInfo(filename);

        Console.WriteLine("Loading " + fileInfo.Name + "...");

        Shapefile shapefile = new Shapefile(filename);

        foreach (var shape in shapefile.OfType <ShapePolygon>())
        {
            foreach (var polygon in Polygon.ReadShapePolygon(shape, this.offset, this.namePrefix))
            {
                if (boundingBox.Left < polygon.Center.x && boundingBox.Right > polygon.Center.x && boundingBox.Top > polygon.Center.z && boundingBox.Bottom < polygon.Center.z)
                {
                    this.add(polygon);
                }
            }
        }
        Console.WriteLine("Loaded " + this.polygons.Count() + " polygons in " + this.data.Keys.Count + " buckets.");
    }
        public static void AddCoordinatesToLineSf(Shapefile sfLines, Helper.Coordinate coordinate1,
                                                  Helper.Coordinate coordinate2)
        {
            var shp = new Shape();

            if (!shp.Create(ShpfileType.SHP_POLYLINE))
            {
                throw new Exception("Error in creating shape. Error: " + shp.ErrorMsg[shp.LastErrorCode]);
            }
            if (shp.AddPoint(coordinate1.X, coordinate1.Y) < 0)
            {
                throw new Exception("Error in adding point. Error: " + shp.ErrorMsg[shp.LastErrorCode]);
            }
            if (shp.AddPoint(coordinate2.X, coordinate2.Y) < 0)
            {
                throw new Exception("Error in adding point. Error: " + shp.ErrorMsg[shp.LastErrorCode]);
            }

            // Check if this line intersects other lines, if so shorten to intersection point:
            var numShapes = sfLines.NumShapes;

            for (var i = 0; i < numShapes; i++)
            {
                var shpTesting = sfLines.Shape[i];
                if (!shpTesting.Crosses(shp))
                {
                    continue;
                }

                var newCoordinate = GetIntersection(Helper.PointToCoordinate(shp.Point[0]),
                                                    Helper.PointToCoordinate(shp.Point[1]), Helper.PointToCoordinate(shpTesting.Point[0]),
                                                    Helper.PointToCoordinate(shpTesting.Point[1]));
                // Replace point:
                shp.Point[1] = Helper.CoordinateToPoint(newCoordinate);
            }

            if (sfLines.EditAddShape(shp) < 0)
            {
                throw new Exception("Error in adding shape. Error: " + sfLines.ErrorMsg[sfLines.LastErrorCode]);
            }
        }
예제 #32
0
        // <summary>
        // Shows attributes of shape in mouse move event.
        // </summary>
        public void ShowAttributes(AxMap axMap1, string dataPath, ToolStripStatusLabel label)
        {
            axMap1.Projection = tkMapProjection.PROJECTION_GOOGLE_MERCATOR;
            axMap1.ProjectionMismatchBehavior = tkMismatchBehavior.mbCheckLooseAndReproject;

            string filename = dataPath + "landuse.shp";
            Shapefile sf = new Shapefile(); 
            if (sf.Open(filename))
            {
                m_layerHandle = axMap1.AddLayer(sf, true);
                sf = axMap1.get_Shapefile(m_layerHandle);     // in case a copy of shapefile was created by AxMap.ProjectionMismatchBehavior
                
                sf.HotTracking = true;
                axMap1.SendMouseMove = true;
                axMap1.CursorMode = tkCursorMode.cmNone;
                axMap1.ShapeHighlighted += AxMap1ShapeHighlighted;
                m_label = label;
            }
            else
            {
                MessageBox.Show("Failed to open shapefile");
            }
        }
        /// <summary>
        /// Extracts feature vectors from points in a time range.
        /// </summary>
        /// <param name="prediction">Prediction to extract vectors for.</param>
        /// <param name="training">Whether or not this is the training phase.</param>
        /// <param name="start">Start time (points without a time are always included).</param>
        /// <param name="end">End time (points without a time are always included).</param>
        /// <returns></returns>
        protected virtual IEnumerable<FeatureVectorList> ExtractFeatureVectors(Prediction prediction, bool training, DateTime start, DateTime end)
        {
            // this can be called concurrently (e.g., via the time slice model with one thread per slice), so lock on prediction to get the point objects and their vectors
            FeatureVectorList featureVectors;
            Dictionary<int, FeatureVector> pointIdFeatureVector;
            int numFeatures;
            lock (prediction)
            {
                prediction.ReleasePoints(); // so that we get new point objects each time -- their times might be modified by a sub-class (e.g., TimeSliceDCM).
                featureVectors = new FeatureVectorList(prediction.Points.Count);
                pointIdFeatureVector = new Dictionary<int, FeatureVector>(prediction.Points.Count);
                numFeatures = GetNumFeaturesExtractedFor(prediction);
                foreach (Point point in prediction.Points)
                    if (point.Time == DateTime.MinValue || (point.Time >= start && point.Time <= end))
                    {
                        point.TrueClass = point.IncidentType;
                        FeatureVector vector = new FeatureVector(point, numFeatures);
                        featureVectors.Add(vector);
                        pointIdFeatureVector.Add(point.Id, vector);
                    }
            }

            Area area = training ? prediction.Model.TrainingArea : prediction.PredictionArea;
            Set<Thread> threads = new Set<Thread>();

            #region spatial distance features
            List<Feature> spatialDistanceFeatures = Features.Where(f => f.EnumValue.Equals(FeatureType.MinimumDistanceToGeometry)).ToList();
            if (spatialDistanceFeatures.Count > 0)
            {
                Console.Out.WriteLine("Extracting spatial distance feature values");
                float distanceWhenBeyondThreshold = (float)Math.Sqrt(2.0 * Math.Pow(FeatureDistanceThreshold, 2)); // with a bounding box of FeatureDistanceThreshold around each point, the maximum distance between a point and some feature shapefile geometry would be sqrt(2*FeatureDistanceThreshold^2). That is, the feature shapefile geometry would be positioned in one of the corners of the bounding box.
                threads.Clear();
                for (int i = 0; i < Configuration.ProcessorCount; ++i)
                {
                    Thread t = new Thread(new ParameterizedThreadStart(o =>
                        {
                            int core = (int)o;
                            NpgsqlConnection threadConnection = DB.Connection.OpenConnection;
                            string pointTableName = Point.GetTableName(prediction);
                            foreach (Feature spatialDistanceFeature in spatialDistanceFeatures)
                            {
                                Shapefile shapefile = new Shapefile(int.Parse(training ? spatialDistanceFeature.TrainingResourceId : spatialDistanceFeature.PredictionResourceId));

                                NpgsqlCommand cmd = DB.Connection.NewCommand("SELECT points." + Point.Columns.Id + " as points_" + Point.Columns.Id + "," +
                                                                             "CASE WHEN COUNT(" + shapefile.GeometryTable + "." + ShapefileGeometry.Columns.Geometry + ")=0 THEN " + distanceWhenBeyondThreshold + " " +
                                                                             "ELSE min(st_distance(st_closestpoint(" + shapefile.GeometryTable + "." + ShapefileGeometry.Columns.Geometry + ",points." + Point.Columns.Location + "),points." + Point.Columns.Location + ")) " +
                                                                             "END as feature_value " +

                                                                             "FROM (SELECT *,st_expand(" + pointTableName + "." + Point.Columns.Location + "," + FeatureDistanceThreshold + ") as bounding_box " +
                                                                                   "FROM " + pointTableName + " " +
                                                                                   "WHERE " + pointTableName + "." + Point.Columns.Id + " % " + Configuration.ProcessorCount + " = " + core + " AND " +
                                                                                              "(" +
                                                                                                  pointTableName + "." + Point.Columns.Time + "='-infinity'::timestamp OR " +
                                                                                                  "(" +
                                                                                                      pointTableName + "." + Point.Columns.Time + ">=@point_start AND " +
                                                                                                      pointTableName + "." + Point.Columns.Time + "<=@point_end" +
                                                                                                  ")" +
                                                                                              ")" +
                                                                                   ") points " +

                                                                             "LEFT JOIN " + shapefile.GeometryTable + " " +

                                                                             "ON points.bounding_box && " + shapefile.GeometryTable + "." + ShapefileGeometry.Columns.Geometry + " AND " +
                                                                                 "(" +
                                                                                    shapefile.GeometryTable + "." + ShapefileGeometry.Columns.Time + "='-infinity'::timestamp OR " +
                                                                                    "(" +
                                                                                        shapefile.GeometryTable + "." + ShapefileGeometry.Columns.Time + ">=@geometry_start AND " +
                                                                                        shapefile.GeometryTable + "." + ShapefileGeometry.Columns.Time + "<=@geometry_end" +
                                                                                    ")" +
                                                                                 ")" +

                                                                             "GROUP BY points." + Point.Columns.Id, null, threadConnection);

                                DateTime spatialDistanceFeatureStart = start - spatialDistanceFeature.Parameters.GetTimeSpanValue(SpatialDistanceParameter.LagOffset);
                                DateTime spatialDistanceFeatureEnd = spatialDistanceFeatureStart + spatialDistanceFeature.Parameters.GetTimeSpanValue(SpatialDistanceParameter.LagDuration);

                                if (spatialDistanceFeatureEnd >= start)
                                    Console.Out.WriteLine("WARNING:  Spatial distance sample overlaps extraction period.");

                                if (spatialDistanceFeatureEnd < spatialDistanceFeatureStart)
                                    Console.Out.WriteLine("WARNING:  Spatial distance sample end precedes sample start.");

                                ConnectionPool.AddParameters(cmd, new Parameter("point_start", NpgsqlDbType.Timestamp, start),
                                                                  new Parameter("point_end", NpgsqlDbType.Timestamp, end),
                                                                  new Parameter("geometry_start", NpgsqlDbType.Timestamp, spatialDistanceFeatureStart),
                                                                  new Parameter("geometry_end", NpgsqlDbType.Timestamp, spatialDistanceFeatureEnd));

                                NpgsqlDataReader reader = cmd.ExecuteReader();
                                NumericFeature distanceFeature = _idNumericFeature[spatialDistanceFeature.Id];
                                while (reader.Read())
                                {
                                    FeatureVector vector;
                                    if (!pointIdFeatureVector.TryGetValue(Convert.ToInt32(reader["points_" + Point.Columns.Id]), out vector))  // above, we select all points that fall between point_start and point_end. the latter can be one tick short of the next minute, and npgsql rounds up causing points to appear in the reader that we didn't add to the pointIdFeatureVector collection.
                                        continue;

                                    double value = Convert.ToDouble(reader["feature_value"]);

                                    // value > threshold shouldn't happen here, since we exluced such objects from consideration above; however, the calculations aren't perfect in postgis, so we check again and reset appropriately
                                    if (value > distanceWhenBeyondThreshold)
                                        value = distanceWhenBeyondThreshold;

                                    vector.Add(distanceFeature, value, false); // don't update range due to concurrent access to the feature
                                }
                                reader.Close();
                            }

                            DB.Connection.Return(threadConnection);
                        }));

                    t.Start(i);
                    threads.Add(t);
                }

                foreach (Thread t in threads)
                    t.Join();
            }
            #endregion

            #region spatial density features
            List<Feature> spatialDensityFeatures = Features.Where(f => f.EnumValue.Equals(FeatureType.GeometryDensity)).ToList();
            if (spatialDensityFeatures.Count > 0)
            {
                List<PostGIS.Point> densityEvalPoints = featureVectors.Select(v => (v.DerivedFrom as Point).Location).ToList();
                Dictionary<string, List<float>> featureIdDensityEstimates = new Dictionary<string, List<float>>(spatialDensityFeatures.Count);
                threads.Clear();
                for (int i = 0; i < Configuration.ProcessorCount; ++i)
                {
                    Thread t = new Thread(new ParameterizedThreadStart(core =>
                        {
                            NpgsqlCommand command = DB.Connection.NewCommand(null);
                            for (int j = (int)core; j < spatialDensityFeatures.Count; j += Configuration.ProcessorCount)
                            {
                                Feature spatialDensityFeature = spatialDensityFeatures[j];

                                DateTime spatialDensityFeatureStart = start - spatialDensityFeature.Parameters.GetTimeSpanValue(SpatialDensityParameter.LagOffset);
                                DateTime spatialDensityFeatureEnd = spatialDensityFeatureStart + spatialDensityFeature.Parameters.GetTimeSpanValue(SpatialDensityParameter.LagDuration);

                                if (spatialDensityFeatureEnd >= start)
                                    Console.Out.WriteLine("WARNING:  Spatial density sample overlaps extraction period.");

                                if (spatialDensityFeatureEnd < spatialDensityFeatureStart)
                                    Console.Out.WriteLine("WARNING:  Spatial density sample end precedes sample start.");

                                Shapefile shapefile = new Shapefile(int.Parse(training ? spatialDensityFeature.TrainingResourceId : spatialDensityFeature.PredictionResourceId));
                                string geometryRecordWhereClause = "WHERE " + ShapefileGeometry.Columns.Time + "='-infinity'::timestamp OR (" + ShapefileGeometry.Columns.Time + ">=@geometry_start AND " + ShapefileGeometry.Columns.Time + "<=@geometry_end)";
                                Parameter geometryStart = new Parameter("geometry_start", NpgsqlDbType.Timestamp, spatialDensityFeatureStart);
                                Parameter geometryEnd = new Parameter("geometry_end", NpgsqlDbType.Timestamp, spatialDensityFeatureEnd);
                                List<PostGIS.Point> kdeInputPoints = Geometry.GetPoints(command, shapefile.GeometryTable, ShapefileGeometry.Columns.Geometry, ShapefileGeometry.Columns.Id, geometryRecordWhereClause, -1, geometryStart.NpgsqlParameter, geometryEnd.NpgsqlParameter).SelectMany(pointList => pointList).Select(p => new PostGIS.Point(p.X, p.Y, area.Shapefile.SRID)).ToList();

                                Console.Out.WriteLine("Computing spatial density of \"" + shapefile.Name + "\".");
                                int sampleSize = spatialDensityFeature.Parameters.GetIntegerValue(SpatialDensityParameter.SampleSize);
                                List<float> densityEstimates = KernelDensityDCM.GetDensityEstimate(kdeInputPoints, sampleSize, false, -1, -1, densityEvalPoints, false);

                                // the density might not be computable if too few points are provided -- use default value for all evaluation points in such cases
                                if (densityEstimates.Count != densityEvalPoints.Count)
                                {
                                    float defaultValue = spatialDensityFeature.Parameters.GetFloatValue(SpatialDensityParameter.DefaultValue);
                                    Console.Out.WriteLine("WARNING:  Using default value \"" + defaultValue + "\" for feature " + spatialDensityFeature);
                                    densityEstimates = Enumerable.Repeat(defaultValue, densityEvalPoints.Count).ToList();
                                }

                                lock (featureIdDensityEstimates) { featureIdDensityEstimates.Add(spatialDensityFeature.Id, densityEstimates); }
                            }

                            DB.Connection.Return(command.Connection);
                        }));

                    t.Start(i);
                    threads.Add(t);
                }

                foreach (Thread t in threads)
                    t.Join();

                foreach (string featureId in featureIdDensityEstimates.Keys)
                {
                    List<float> densityEstimates = featureIdDensityEstimates[featureId];
                    NumericFeature densityFeature = _idNumericFeature[featureId];
                    for (int i = 0; i < densityEstimates.Count; ++i)
                        featureVectors[i].Add(densityFeature, densityEstimates[i], false);  // don't update range due to concurrent access to the feature
                }
            }
            #endregion

            #region geometry attribute features
            List<Feature> geometryAttributeFeatures = Features.Where(f => f.EnumValue.Equals(FeatureType.GeometryAttribute)).ToList();
            if (geometryAttributeFeatures.Count > 0)
            {
                Console.Out.WriteLine("Extracting geometry attribute features.");
                threads.Clear();
                for (int i = 0; i < Configuration.ProcessorCount; ++i)
                {
                    Thread t = new Thread(new ParameterizedThreadStart(o =>
                        {
                            int core = (int)o;
                            NpgsqlConnection threadConnection = DB.Connection.OpenConnection;
                            string pointTableName = Point.GetTableName(prediction);
                            foreach (Feature geometryAttributeFeature in geometryAttributeFeatures)
                            {
                                Shapefile shapefile = new Shapefile(int.Parse(training ? geometryAttributeFeature.TrainingResourceId : geometryAttributeFeature.PredictionResourceId));
                                string attributeColumn = geometryAttributeFeature.Parameters.GetStringValue(GeometryAttributeParameter.AttributeColumn);
                                NpgsqlCommand cmd = DB.Connection.NewCommand("SELECT " + pointTableName + "." + Point.Columns.Id + " as point_id," + shapefile.GeometryTable + "." + attributeColumn + " as geometry_attribute " +
                                                                             "FROM " + pointTableName + " " +
                                                                             "LEFT JOIN " + shapefile.GeometryTable + " " + // the geometry might not overlap the point, in which case we'll use the default feature value below
                                                                             "ON st_intersects(" + pointTableName + "." + Point.Columns.Location + "," + shapefile.GeometryTable + "." + ShapefileGeometry.Columns.Geometry + ") " +
                                                                             "WHERE " + pointTableName + "." + Point.Columns.Id + " % " + Configuration.ProcessorCount + " = " + core + " AND " +
                                                                                        "(" +
                                                                                          pointTableName + "." + Point.Columns.Time + "='-infinity'::timestamp OR " +
                                                                                          "(" +
                                                                                            pointTableName + "." + Point.Columns.Time + ">=@point_start AND " +
                                                                                            pointTableName + "." + Point.Columns.Time + "<=@point_end" +
                                                                                          ")" +
                                                                                        ") " +
                                                                             "ORDER BY " + pointTableName + "." + Point.Columns.Id, null, threadConnection);

                                ConnectionPool.AddParameters(cmd, new Parameter("point_start", NpgsqlDbType.Timestamp, start),
                                                                  new Parameter("point_end", NpgsqlDbType.Timestamp, end));

                                LAIR.MachineLearning.Feature attributeFeature;
                                string attributeType = geometryAttributeFeature.Parameters.GetStringValue(GeometryAttributeParameter.AttributeType);
                                if (attributeType == "Numeric")
                                    attributeFeature = _idNumericFeature[geometryAttributeFeature.Id] as LAIR.MachineLearning.Feature;
                                else if (attributeType == "Nominal")
                                    attributeFeature = _idNominalFeature[geometryAttributeFeature.Id] as LAIR.MachineLearning.Feature;
                                else
                                    throw new NotImplementedException("Unrecognized geometry attribute feature type:  " + attributeType);

                                List<object> values = new List<object>();
                                int currPointId = -1;
                                int pointId = -1;

                                Action addFeatureToVector = new Action(() =>
                                    {
                                        if (values.Count > 0)
                                        {
                                            FeatureVector vector = pointIdFeatureVector[currPointId];
                                            if (attributeFeature is NumericFeature)
                                                vector.Add(attributeFeature, values.Select(v => Convert.ToSingle(v)).Average(), false);  // don't update range due to concurrent access to the feature
                                            else if (values.Count == 1)
                                                vector.Add(attributeFeature, Convert.ToString(values[0]), false);  // don't update range due to concurrent access to the feature
                                            else
                                                throw new Exception("Nominal geometry attribute \"" + attributeColumn + "\" of shapefile \"" + shapefile.GeometryTable + "\" has multiple non-numeric values at point \"" + (vector.DerivedFrom as Point).Location + "\".");
                                        }

                                        values.Clear();
                                        currPointId = pointId;
                                    });

                                NpgsqlDataReader reader = cmd.ExecuteReader();
                                string defaultValue = geometryAttributeFeature.Parameters.GetStringValue(GeometryAttributeParameter.DefaultValue);
                                while (reader.Read())
                                {
                                    pointId = Convert.ToInt32(reader["point_id"]);
                                    if (pointId != currPointId)
                                        addFeatureToVector();

                                    object value = reader["geometry_attribute"];
                                    if (value is DBNull)  // we did a left join above, so the value might be null meaning the geometry did not overlap the point
                                        value = defaultValue;

                                    values.Add(value);
                                }
                                reader.Close();

                                addFeatureToVector();
                            }

                            DB.Connection.Return(threadConnection);
                        }));

                    t.Start(i);
                    threads.Add(t);
                }

                foreach (Thread t in threads)
                    t.Join();
            }
            #endregion

            #region incident density features
            List<Feature> kdeFeatures = Features.Where(f => f.EnumValue.Equals(FeatureType.IncidentDensity)).ToList();
            if (kdeFeatures.Count > 0)
            {
                List<PostGIS.Point> densityEvalPoints = featureVectors.Select(v => (v.DerivedFrom as Point).Location).ToList();
                Dictionary<string, List<float>> featureIdDensityEstimates = new Dictionary<string, List<float>>(kdeFeatures.Count);
                threads.Clear();
                for (int i = 0; i < Configuration.ProcessorCount; ++i)
                {
                    Thread t = new Thread(new ParameterizedThreadStart(core =>
                        {
                            for (int j = (int)core; j < kdeFeatures.Count; j += Configuration.ProcessorCount)
                            {
                                Feature kdeFeature = kdeFeatures[j];

                                List<PostGIS.Point> kdeInputPoints = new List<PostGIS.Point>();
                                string incident = training ? kdeFeature.TrainingResourceId : kdeFeature.PredictionResourceId;
                                int lagCount = kdeFeature.Parameters.GetIntegerValue(IncidentDensityParameter.LagCount);
                                TimeSpan lagOffset = kdeFeature.Parameters.GetTimeSpanValue(IncidentDensityParameter.LagOffset);
                                TimeSpan lagDuration = kdeFeature.Parameters.GetTimeSpanValue(IncidentDensityParameter.LagDuration);
                                for (int k = 1; k <= lagCount; ++k)
                                {
                                    DateTime incidentSampleStart = start - new TimeSpan(k * lagOffset.Ticks);
                                    DateTime incidentSampleEnd = incidentSampleStart + lagDuration;

                                    if (incidentSampleEnd >= start)
                                        Console.Out.WriteLine("WARNING:  Incident density sample overlaps extraction period.");

                                    if (incidentSampleEnd < incidentSampleStart)
                                        Console.Out.WriteLine("WARNING:  Incident density sample end precedes sample start.");

                                    kdeInputPoints.AddRange(Incident.Get(incidentSampleStart, incidentSampleEnd, area, incident).Select(inc => inc.Location));
                                }

                                Console.Out.WriteLine("Computing spatial density of \"" + incident + "\" with " + lagCount + " lag(s) at offset " + lagOffset + ", each with duration " + lagDuration);
                                int sampleSize = kdeFeature.Parameters.GetIntegerValue(IncidentDensityParameter.SampleSize);
                                List<float> densityEstimates = KernelDensityDCM.GetDensityEstimate(kdeInputPoints, sampleSize, false, 0, 0, densityEvalPoints, false);

                                // the density might not be computable if too few points are provided -- use default density for all evaluation points in such cases
                                if (densityEstimates.Count != densityEvalPoints.Count)
                                {
                                    float defaultValue = kdeFeature.Parameters.GetFloatValue(IncidentDensityParameter.DefaultValue);
                                    Console.Out.WriteLine("WARNING:  Using default value \"" + defaultValue + "\" for feature " + kdeFeature);
                                    densityEstimates = Enumerable.Repeat(defaultValue, densityEvalPoints.Count).ToList();
                                }

                                lock (featureIdDensityEstimates) { featureIdDensityEstimates.Add(kdeFeature.Id, densityEstimates); }
                            }
                        }));

                    t.Start(i);
                    threads.Add(t);
                }

                foreach (Thread t in threads)
                    t.Join();

                foreach (string featureId in featureIdDensityEstimates.Keys)
                {
                    List<float> densityEstimates = featureIdDensityEstimates[featureId];
                    NumericFeature densityFeature = _idNumericFeature[featureId];
                    for (int i = 0; i < densityEstimates.Count; ++i)
                        featureVectors[i].Add(densityFeature, densityEstimates[i], false);  // don't update range due to concurrent access to the feature (e.g., via time slice model calling into this method)
                }
            }
            #endregion

            // update all feature ranges. this wasn't done above due to potential concurrent access, either within this method or from calls into this method. each feature needs to be locked here due to potential concurrent calls into this method (e.g., time slice model)
            foreach (FeatureVector vector in featureVectors)
                foreach (LAIR.MachineLearning.Feature f in vector)
                    lock (f)
                        f.UpdateRange(vector[f]);

            IFeatureExtractor externalFeatureExtractor = InitializeExternalFeatureExtractor(typeof(FeatureBasedDCM));
            if (externalFeatureExtractor == null)
                yield return featureVectors;
            else
                foreach (FeatureVectorList externalFeatureVectors in externalFeatureExtractor.ExtractFeatures(prediction, featureVectors, training, start, end, true))
                    yield return externalFeatureVectors;
        }
예제 #34
0
파일: SvgWriter.cs 프로젝트: Knoema/shp2svg
        public void GetMetadata(string path)
        {
            using (Shapefile shapefile = new Shapefile(path))
            {
                var stringBuilder = new StringBuilder();

                var i = 0;
                foreach (Shape shape in shapefile)
                    if (shape.Type == ShapeType.Polygon)
                    {
                        if (i == 0)
                        {
                            stringBuilder.AppendLine(string.Join(", ", shape.GetMetadataNames()));
                            i++;
                        }

                        stringBuilder.AppendLine(string.Join(", ", shape.GetMetadataNames().Select(p => shape.GetMetadata(p))));
                    }

                File.WriteAllText(Helper.GetFilePath(path, ".csv"), stringBuilder.ToString());
            }
        }
예제 #35
0
파일: SvgWriter.cs 프로젝트: Knoema/shp2svg
        public void CreateSVG()
        {
            if (!File.Exists(_path))
            {
                LogMessage(string.Format("File does not exist. Path: {0}", _path));
                return;
            }

            var regionsPath = Path.Combine(Path.GetDirectoryName(_path), "regions.json");
            if (!File.Exists(regionsPath))
            {
                LogMessage(string.Format("Regions file does not exist. Path: {0}", regionsPath));
                return;
            }

            var regions = JsonConvert.DeserializeObject<Region>(File.ReadAllText(regionsPath));

            using (Shapefile shapefile = new Shapefile(_path))
            {
                LogMessage(string.Format("Start Create SVG for: {0}", Path.GetFileName(_path)));

                var scale = Math.Max(
                    Math.Abs(_projection.lonToX(shapefile.BoundingBox.Left) - _projection.lonToX(shapefile.BoundingBox.Right)) / _width,
                    Math.Abs(_projection.latToY(shapefile.BoundingBox.Top) - _projection.latToY(shapefile.BoundingBox.Bottom)) /_height
                );

                using (var writer = XmlWriter.Create(Helper.GetFilePath(_path, ".svg"), new XmlWriterSettings() { Indent = true }))
                {
                    writer.WriteStartElement("svg", "http://www.w3.org/2000/svg");
                    writer.WriteAttributeString("id", "svgmapid");
                    writer.WriteAttributeString("viewBox", string.Format("0 0 {0} {1}", _width, _height));
                    writer.WriteAttributeString("fill", "transparent");
                    writer.WriteAttributeString("stroke", "gray");
                    writer.WriteAttributeString("stroke-width", "0.3");

                    var failedRegions = new List<string>();

                    foreach (Shape shape in shapefile)
                        if (shape.Type == ShapeType.Polygon)
                        {
                            var region = shape.GetMetadata(_attr);
                            var id = GetId(region, regions);

                            foreach (PointD[] part in (shape as ShapePolygon).Parts)
                            {
                                writer.WriteStartElement("path");
                                writer.WriteAttributeString("id", id);

                                if (string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(region))
                                {
                                    failedRegions.Add(region);
                                    writer.WriteAttributeString("region", region);
                                }

                                var coords = new List<List<double>>();
                                for (int i = 0; i < part.Length; i++)
                                    coords.Add(new List<double>() { part[i].X, part[i].Y });

                                coords = Helper.SimplifyCoordinates(coords, _tolerance);

                                var stringBuilder = new StringBuilder();

                                for (int i = 0; i < coords.Count; i++)
                                    stringBuilder.Append(string.Format("{0} {1} {2} {3}",
                                           i == 0 ? "M" : "L",
                                           (_projection.lonToX(coords[i][0]) - _projection.lonToX(shapefile.BoundingBox.Left)) / scale,
                                           (-_projection.latToY(coords[i][1]) + _projection.latToY(shapefile.BoundingBox.Bottom)) / scale,
                                           i == coords.Count - 1 ? "Z" : string.Empty
                                       )
                                   );

                                writer.WriteAttributeString("d", stringBuilder.ToString());
                                writer.WriteEndElement();
                            }
                        }

                    writer.WriteEndElement();
                    writer.Flush();
                    writer.Close();

                    if (failedRegions.Count > 0)
                        LogMessage(string.Format("Unable to match id for region: {0}", string.Join(", ", failedRegions.Distinct())));

                    LogMessage(string.Format("End Create SVG for: {0}{1}", Path.GetFileName(_path), Environment.NewLine));
                }
            }
        }
예제 #36
0
        public ActionResult GetCoordinatesFromShapeFile()
        {
            Shapefile shapefile;
            var shapeCoordinates = new Dictionary<string, string>();
            try
            {
                shapefile = new Shapefile(Server.MapPath("..\\Content\\ShapeFiles\\tmp"), Shapefile.ConnectionStringTemplateJet);

                foreach (Catfood.Shapefile.Shape shape in shapefile)
                {
                    ShapePolygon poligono = shape as ShapePolygon;
                    foreach (PointD[] part in poligono.Parts)
                    {
                        int i = 0;
                        foreach (PointD point in part)
                        {
                            shapeCoordinates.Add(i.ToString(), point.X.ToString() + ";" + point.Y.ToString());
                            i++;
                        }
                    }
                }

                //Eliminamos los archivos ya que no los necesitamos más
                deleteFiles();

                return Json(shapeCoordinates, JsonRequestBehavior.AllowGet);
            }
            catch (FileNotFoundException e)
            {
                var negativeResponse = new Dictionary<string, string>();
                negativeResponse.Add("0", "Error");

                //Eliminamos los archivos ya que no los necesitamos más
                deleteFiles();

                return Json(negativeResponse, JsonRequestBehavior.AllowGet);
            }
        }
예제 #37
0
        private void OnOpenFile(string fileName)
        {
            // Show message.
            this.textBox.AppendText(string.Format("Opening file \'{0}\'...{1}", fileName, Environment.NewLine));

            try
            {
                // Open a stream to the ZIP file.
                using (FileStream fileInStream = new FileStream(fileName, FileMode.Open))
                {
                    // Open the ZIP archive.
                    using (ZipArchive zipArchive = new ZipArchive(fileInStream, ZipArchiveMode.Read))
                    {
                        // The shape file name.
                        string shapeFileName = null;

                        this.textBox.AppendText(string.Format("Extracting shape ZIP archive...{0}", Environment.NewLine));
                        foreach (ZipArchiveEntry entry in zipArchive.Entries)
                        {
                            // If this is the shape file, save the name.
                            if (Path.GetExtension(entry.Name) == ".shp")
                            {
                                shapeFileName = entry.Name;
                            }
                            this.textBox.AppendText(string.Format("- {0}: {1} bytes {2} bytes compressed{3}", entry.Name, entry.Length, entry.CompressedLength, Environment.NewLine));
                        }

                        // If there are no entries, throw an exception.
                        if (null == shapeFileName) throw new FileNotFoundException("The ZIP archive does not contain a shape file.");

                        // Create the name of a temporary folder.
                        string tempFolder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

                        this.textBox.AppendText(string.Format("Shape file name is: \'{0}\'{1}", shapeFileName, Environment.NewLine));

                        // Create the temporary folder.
                        System.IO.Directory.CreateDirectory(tempFolder);

                        this.textBox.AppendText(string.Format("Creating temporary folder \'{0}\'...{1}", tempFolder, Environment.NewLine));

                        try
                        {
                            // Extract the shapefile contents.
                            zipArchive.ExtractToDirectory(tempFolder);

                            // Open the shapefile.
                            using (Shapefile shapefile = new Shapefile(Path.Combine(tempFolder, shapeFileName)))
                            {
                                this.textBox.AppendText(Environment.NewLine);

                                // Write the basic information.
                                this.textBox.AppendText(string.Format("Type: {0}, Shapes: {1:n0}{2}", shapefile.Type, shapefile.Count, Environment.NewLine));

                                this.textBox.AppendText(Environment.NewLine);

                                // Create a map object.
                                Map map = new Map(new MapRectangle(
                                    shapefile.BoundingBox.Left,
                                    shapefile.BoundingBox.Top,
                                    shapefile.BoundingBox.Right,
                                    shapefile.BoundingBox.Bottom));

                                // Write the bounding box of this shape file.
                                this.textBox.AppendText(string.Format("Bounds: {0},{1} -> {2},{3}{4}",
                                    shapefile.BoundingBox.Left,
                                    shapefile.BoundingBox.Top,
                                    shapefile.BoundingBox.Right,
                                    shapefile.BoundingBox.Bottom,
                                    Environment.NewLine));

                                // Enumerate all shapes.
                                foreach (Shape shape in shapefile)
                                {
                                    // Shape basic information.
                                    //this.textBox.AppendText(string.Format("{0} {1} {2} ", shape.RecordNumber, shape.Type, shape.GetMetadata("name")));

                                    // Create a new shape.
                                    MapShape mapShape;
                                    switch (shape.Type)
                                    {
                                        case ShapeType.Point:
                                            ShapePoint shapePoint = shape as ShapePoint;
                                            mapShape = new MapShapePoint(new MapPoint(shapePoint.Point.X, shapePoint.Point.Y));
                                            break;
                                        case ShapeType.Polygon:
                                            ShapePolygon shapePolygon = shape as ShapePolygon;

                                            //this.textBox.AppendText(string.Format(": {0}", shapePolygon.Parts.Count));

                                            MapShapePolygon mapShapePolygon = new MapShapePolygon(new MapRectangle(
                                                shapePolygon.BoundingBox.Left,
                                                shapePolygon.BoundingBox.Top,
                                                shapePolygon.BoundingBox.Right,
                                                shapePolygon.BoundingBox.Bottom));
                                            foreach(PointD[] part in shapePolygon.Parts)
                                            {
                                                MapPart mapPart = new MapPart();
                                                foreach (PointD point in part)
                                                {
                                                    mapPart.Points.Add(point.X, point.Y);
                                                }
                                                mapShapePolygon.Parts.Add(mapPart);
                                            }
                                            mapShape = mapShapePolygon;
                                            break;
                                        default:
                                            throw new NotSupportedException(string.Format("Shape type {0} is not supported.", shape.Type));
                                    }
                                    // Add the shape metadata.
                                    foreach (string name in shape.GetMetadataNames())
                                    {
                                        mapShape.Metadata[name] = shape.GetMetadata(name);
                                    }
                                    // Add the shape to the map.
                                    map.Shapes.Add(mapShape);
                                    //this.textBox.AppendText(Environment.NewLine);
                                }

                                this.textBox.AppendText(Environment.NewLine);

                                // Create a memory stream.
                                using (MemoryStream memoryStream = new MemoryStream())
                                {
                                    // Serialize the map data.
                                    map.Write(memoryStream);
                                    // Display the XML.
                                    this.textBox.AppendText(Encoding.UTF8.GetString(memoryStream.ReadToEnd()));

                                    this.textBox.AppendText(Environment.NewLine);
                                    this.textBox.AppendText(Environment.NewLine);

                                    // Set the stream position to zero.
                                    memoryStream.Position = 0;
                                    // Display a dialog to save the file.
                                    if (this.saveFileDialog.ShowDialog(this) == DialogResult.OK)
                                    {
                                        // Create a file stream.
                                        using (FileStream fileOutStream = System.IO.File.Create(this.saveFileDialog.FileName))
                                        {
                                            // Compress the stream.
                                            //using (GZipStream zipStream = new GZipStream(fileOutStream, CompressionLevel.Optimal))
                                            //{
                                                this.textBox.AppendText("Uncompressed data is {0} bytes.{1}".FormatWith(memoryStream.Length, Environment.NewLine));
                                                memoryStream.CopyTo(fileOutStream);
                                                this.textBox.AppendText("Compressed data is {0} bytes.{1}".FormatWith(fileOutStream.Length, Environment.NewLine));
                                            //}
                                        }
                                    }
                                }
                                //this.textBox.AppendText(map.ToXml().ToString());
                                this.textBox.AppendText(Environment.NewLine);
                            }
                        }
                        finally
                        {
                            // Delete the temporary folder.
                            this.textBox.AppendText(Environment.NewLine);
                            System.IO.Directory.Delete(tempFolder, true);
                            this.textBox.AppendText(string.Format("Temporary folder \'{0}\' deleted.{1}", tempFolder, Environment.NewLine));
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                this.textBox.AppendText(string.Format("An exception occurred. {0}", exception.Message));
            }
            this.textBox.AppendText(Environment.NewLine);
            this.textBox.AppendText("Done.");
        }
예제 #38
0
        /// <summary>
        /// Initializes the shape file.
        /// </summary>
        /// <param name="shapeKey">Shape key used.</param>
        private void Initialize(string shapeKey)
        {
            // construct shapefile with the path to the .shp file
            using (Shapefile shapefile = new Shapefile(this.shapeFilePath))
            {
                // enumerate all shapes
                foreach (Shape shape in shapefile)
                {
                    // cast shape based on the type
                    switch (shape.Type)
                    {
                        case ShapeType.Polygon:
                            // a polygon contains one or more parts - each part is a list of points which
                            // are clockwise for boundaries and anti-clockwise for holes
                            // see http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
                            ShapePolygon shapePolygon = shape as ShapePolygon;
                            string shapeName = shape.GetMetadata(shapeKey);
                            if (!string.IsNullOrEmpty(shapeName))
                            {
                                shapeName = shapeName.Trim().ToLowerInvariant();
                                if (!this.shapesList.ContainsKey(shapeName))
                                {
                                    this.shapesList.Add(shapeName, shapePolygon.Parts);
                                }
                                else
                                {
                                    this.shapesList[shapeName].AddRange(shapePolygon.Parts);
                                }
                            }

                            break;
                        default:
                            // and so on for other types...
                            break;
                    }
                }
            }

            for (int h = 0; h < Constants.MaxHvalue; h++)
            {
                for (int v = 0; v < Constants.MaxVvalue; v++)
                {
                    this.cells.Add(string.Format(CultureInfo.CurrentCulture, "h{0:D2}v{1:D2}", h, v), new List<string>());
                }
            }

            // Initialize Region to HV Map.
            foreach (var region in System.IO.File.ReadAllLines(this.regionHVMapFilePath))
            {
                string[] regionCellMap = region.Split(':');
                string regionName = regionCellMap[0].ToLower(CultureInfo.CurrentCulture);

                foreach (var cell in regionCellMap[1].Split(','))
                {
                    this.cells[cell].Add(regionName);
                }
            }
        }
        public override void Import()
        {
            base.Import();

            Console.Out.WriteLine("Importing shapefile from \"" + Path + "\"...");

            try
            {
                Dictionary<string, string> importOptionValue = new Dictionary<string, string>();
                string importOptionsPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Path), System.IO.Path.GetFileNameWithoutExtension(Path) + ".att");
                if (File.Exists(importOptionsPath))
                    foreach (string line in File.ReadLines(importOptionsPath))
                    {
                        string[] parts = line.Split('=');
                        importOptionValue.Add(parts[0].Trim(), parts[1].Trim());
                    }

                if (_sourceSRID > 0 && _targetSRID > 0)
                    importOptionValue["reprojection"] = _sourceSRID + ":" + _targetSRID;

                if (!string.IsNullOrWhiteSpace(Name))
                    importOptionValue["name"] = Name;

                List<string> neededValues = new List<string>();
                if (!importOptionValue.ContainsKey("reprojection") || string.IsNullOrWhiteSpace(importOptionValue["reprojection"])) neededValues.Add("reprojection");
                if (!importOptionValue.ContainsKey("name") || string.IsNullOrWhiteSpace(importOptionValue["name"])) neededValues.Add("name");
                if (neededValues.Count > 0)
                {
                    if (_shapefileInfoRetriever == null)
                        throw new Exception("Missing the following shapefile options, and no information retriever was present:  " + neededValues.Concatenate(","));
                    else
                        _shapefileInfoRetriever.GetShapefileInfo(Path, neededValues, importOptionValue);
                }

                string missingValues = neededValues.Where(v => !importOptionValue.ContainsKey(v) || string.IsNullOrWhiteSpace(importOptionValue[v])).Concatenate(", ");
                if (missingValues != "")
                    throw new Exception("Failed to provide needed values for shapefile import:  " + missingValues);

                string reprojection = importOptionValue["reprojection"];
                Match reprojectionMatch = new Regex("(?<from>[0-9]+):(?<to>[0-9]+)").Match(reprojection);
                if (!reprojectionMatch.Success)
                    throw new Exception("Invalid shapefile reprojection \"" + reprojection + "\". Must be in 1234:1234 format.");

                int fromSRID = int.Parse(reprojectionMatch.Groups["from"].Value);
                int toSRID = int.Parse(reprojectionMatch.Groups["to"].Value);
                if (fromSRID == toSRID)
                    reprojection = fromSRID.ToString();

                string name = importOptionValue["name"];
                if (string.IsNullOrWhiteSpace(name))
                    throw new Exception("Empty name given for shapefile \"" + Path + "\".");

                Name = name; // to make sure names retrieved from *.att files get back to the object and ultimately back to the DB

                File.WriteAllText(importOptionsPath, "reprojection=" + fromSRID + ":" + toSRID + Environment.NewLine +
                                                     "name=" + name);

                Shapefile.ShapefileType type;
                if (this is FeatureShapefileImporter)
                    type = Shapefile.ShapefileType.Feature;
                else if (this is AreaShapefileImporter)
                    type = Shapefile.ShapefileType.Area;
                else if (this is IncidentShapefileImporter)
                    type = Shapefile.ShapefileType.Incident;
                else
                    throw new NotImplementedException("Unrecognized shapefile importer type:  " + GetType());

                _importedShapefile = Shapefile.Create(name, toSRID, type);

                string sql;
                string error;
                using (Process process = new Process())
                {
                    process.StartInfo.FileName = Configuration.Shp2PgsqlPath;
                    process.StartInfo.Arguments = "-I -g " + ShapefileGeometry.Columns.Geometry + " -s " + reprojection + " \"" + Path + "\" " + _importedShapefile.GeometryTable;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.Start();

                    Console.Out.WriteLine("Converting shapefile \"" + name + "\".");

                    sql = process.StandardOutput.ReadToEnd().Replace("BEGIN;", "").Replace("COMMIT;", "");
                    error = process.StandardError.ReadToEnd().Trim().Replace(Environment.NewLine, "; ").Replace("\n", "; ");

                    process.WaitForExit();
                }

                Console.Out.WriteLine(error);

                DB.Connection.ExecuteNonQuery(sql);

                // if there's an id column already, rename it native_id
                if (DB.Connection.GetColumnNames(_importedShapefile.GeometryTable).Select(c => c.ToLower()).Contains("id"))
                {
                    DB.Connection.ExecuteNonQuery("ALTER TABLE " + _importedShapefile.GeometryTable + " DROP COLUMN IF EXISTS native_id;" +
                                                  "ALTER TABLE " + _importedShapefile.GeometryTable + " RENAME COLUMN id TO native_id");
                }

                // rename primary key column to ShapefileGeometry.Columns.Id
                List<string> primaryKeyColumns = DB.Connection.GetPrimaryKeyColumns(_importedShapefile.GeometryTable).ToList();
                if (primaryKeyColumns.Count == 1)
                {
                    if (primaryKeyColumns[0] != ShapefileGeometry.Columns.Id)
                    {
                        DB.Connection.ExecuteNonQuery("ALTER TABLE " + _importedShapefile.GeometryTable + " DROP COLUMN IF EXISTS " + ShapefileGeometry.Columns.Id + ";" +
                                                      "ALTER TABLE " + _importedShapefile.GeometryTable + " RENAME COLUMN " + primaryKeyColumns[0] + " TO " + ShapefileGeometry.Columns.Id);
                    }
                }
                else
                    throw new Exception("Imported shapefile database does not contain a single primary key column.");

                Console.Out.WriteLine("Shapefile import succeeded.");
            }
            catch (Exception ex)
            {
                if (_importedShapefile != null)
                {
                    try { _importedShapefile.Delete(); }
                    catch (Exception ex2) { Console.Out.WriteLine("Failed to delete shapefile:  " + ex2.Message); }
                }

                _importedShapefile = null;

                throw new Exception("Shapefile import failed:  " + ex.Message);
            }
        }
예제 #40
-17
파일: Logger.cs 프로젝트: Knoema/shp2svg
        //log shapefile on console
        public static void ShapeFileLogger(string path)
        {
            if (!File.Exists(path))
            {
                Console.WriteLine("File does not exist or no input provided");
                return;
            }

            LogText(string.Format("Logging File: {0}", Path.GetFileName(path)));

            // construct shapefile with the path to the .shp file
            using (Shapefile shapefile = new Shapefile(path))
            {
                Console.WriteLine("ShapefileDemo Dumping {0}", path);
                Console.WriteLine();

                // a shapefile contains one type of shape (and possibly null shapes)
                Console.WriteLine("Type: {0}, Shapes: {1:n0}", shapefile.Type, shapefile.Count);

                // a shapefile also defines a bounding box for all shapes in the file
                Console.WriteLine("Bounds: {0},{1} -> {2},{3}",
                    shapefile.BoundingBox.Left,
                    shapefile.BoundingBox.Top,
                    shapefile.BoundingBox.Right,
                    shapefile.BoundingBox.Bottom);
                Console.WriteLine();

                // enumerate all shapes
                foreach (Shape shape in shapefile)
                {
                    Console.WriteLine("----------------------------------------");
                    Console.WriteLine("Shape {0:n0}, Type {1}", shape.RecordNumber, shape.Type);

                    // each shape may have associated metadata
                    string[] metadataNames = shape.GetMetadataNames();
                    if (metadataNames != null)
                    {
                        Console.WriteLine("Metadata:");
                        foreach (string metadataName in metadataNames)
                        {
                            Console.WriteLine("{0}={1} ({2})", metadataName, shape.GetMetadata(metadataName), shape.DataRecord.GetDataTypeName(shape.DataRecord.GetOrdinal(metadataName)));
                        }
                        Console.WriteLine();
                    }

                    // cast shape based on the type
                    switch (shape.Type)
                    {
                        case ShapeType.Point:
                            // a point is just a single x/y point
                            ShapePoint shapePoint = shape as ShapePoint;
                            Console.WriteLine("Point={0},{1}", shapePoint.Point.X, shapePoint.Point.Y);
                            break;

                        case ShapeType.Polygon:
                            // a polygon contains one or more parts - each part is a list of points which
                            // are clockwise for boundaries and anti-clockwise for holes
                            // see http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf
                            ShapePolygon shapePolygon = shape as ShapePolygon;
                            foreach (PointD[] part in shapePolygon.Parts)
                            {
                                Console.WriteLine("Polygon part:");
                                foreach (PointD point in part)
                                {
                                    Console.WriteLine("{0}, {1}", point.X, point.Y);
                                }
                                Console.WriteLine();
                            }
                            break;

                        default:
                            // and so on for other types...
                            break;
                    }

                    Console.WriteLine("----------------------------------------");
                    Console.WriteLine();
                }

            }
            Console.WriteLine("Done");
            Console.WriteLine();
        }