Beispiel #1
0
 /// <summary>
 /// Gets the feature type.
 /// </summary>
 /// <param name="fileName">File whose feature type should be returned.</param>
 /// <returns>The feature type.</returns>
 public FeatureType GetFeatureType(string fileName)
 {
     using (var reader = new OgrDataReader(fileName))
     {
         return(reader.GetFeatureType());
     }
 }
        /// <summary>
        /// Opens the specified file
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public IFeatureSet Open(string fileName)
        {
            IFeatureSet fs = new FeatureSet();

            fs.Name     = Path.GetFileNameWithoutExtension(fileName);
            fs.Filename = fileName;
            using (var reader = new OgrDataReader(fileName))
            {
                // skip the geometry column which is always column 0
                for (int i = 1; i < reader.FieldCount; i++)
                {
                    string sFieldName = reader.GetName(i);
                    Type   type       = reader.GetFieldType(i);

                    int    uniqueNumber = 1;
                    string uniqueName   = sFieldName;
                    while (fs.DataTable.Columns.Contains(uniqueName))
                    {
                        uniqueName = sFieldName + uniqueNumber;
                        uniqueNumber++;
                    }
                    fs.DataTable.Columns.Add(new DataColumn(uniqueName, type));
                }

                var wkbReader = new WKBReader();
                while (reader.Read())
                {
                    var wkbGeometry = (byte[])reader["Geometry"];

                    var geometry = wkbReader.Read(wkbGeometry);

                    IFeature feature = new Feature(geometry);
                    feature.DataRow = fs.DataTable.NewRow();
                    for (int i = 1; i < reader.FieldCount; i++)
                    {
                        object value = reader[i];
                        if (value == null)
                        {
                            value = DBNull.Value;
                        }
                        feature.DataRow[i - 1] = value;
                    }
                    fs.Features.Add(feature);
                }

                try
                {
                    fs.Projection = reader.GetProj4ProjectionInfo();
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                }
            }

            return(fs);
        }
        /// <summary>
        /// Opens the given file.
        /// </summary>
        /// <param name="fileName">Name of the file that should be opened.</param>
        public void Open(string fileName)
        {
            Dispose();

            _fileName  = fileName;
            _groupName = Path.GetFileNameWithoutExtension(fileName);

            using var reader = new OgrDataReader(fileName);
            _dataSets        = reader.GetLayers();
        }
        /// <summary>
        /// Gets the IMapSelfLoadLayer that contains all the DataSets as Layers and can be added to the map.
        /// </summary>
        /// <param name="layerNames">The names of the layers that should be loaded. If this is null all layers get loaded.</param>
        /// <returns>Null, if there are no DataSets, a MapSelfLoadLayer if there is only 1 DataSet otherwise a MapSelfLoadGroup with layers for the existing DataSets.</returns>
        public IMapSelfLoadLayer GetLayer(string[] layerNames)
        {
            if (_dataSets.Count < 1)
            {
                return(null);
            }

            if (_dataSets.Count > 1)
            {
                // return a group with all the layers
                var gr = new MapSelfLoadGroup
                {
                    LegendText = _groupName,
                    DataSet    = this,
                    FilePath   = _fileName
                };

                foreach (var ds in _dataSets)
                {
                    if (layerNames == null || layerNames.Contains(ds.Key.Name))
                    {
                        IMapFeatureLayer l = gr.Layers.Add(ds.Key);
                        OgrDataReader.TranslateStyles(l, ds.Value);
                        l.IsExpanded = false;
                    }
                }

                return(gr);
            }

            // return the single existing layer
            var fs = _dataSets.Keys.FirstOrDefault();

            if (fs == null || (layerNames != null && !layerNames.Contains(fs.Name)))
            {
                return(null);
            }

            IMapSelfLoadLayer layer;

            switch (fs.FeatureType)
            {
            case FeatureType.MultiPoint:
            case FeatureType.Point:
                layer = new MapSelfLoadPointLayer(fs);
                break;

            case FeatureType.Line:
                layer = new MapSelfLoadLineLayer(fs);
                break;

            case FeatureType.Polygon:
                layer = new MapSelfLoadPolygonLayer(fs);
                break;

            default:
                return(null);
            }

            OgrDataReader.TranslateStyles((IMapFeatureLayer)layer, _dataSets[fs]);
            layer.IsExpanded = false;
            return(layer);
        }