Exemplo n.º 1
0
        public static EsriPrjFile AsEsriPrj(this SrsBase mapProjection)
        {
            switch (mapProjection.Type)
            {
            case MapProjectionType.None:
                return(new EsriPrjFile(new EsriPrjTreeNode(mapProjection.Ellipsoid, mapProjection.Title, mapProjection.Srid)));

            case MapProjectionType.AlbersEqualAreaConic:
            case MapProjectionType.AzimuthalEquidistant:
                throw new NotImplementedException();

            case MapProjectionType.CylindricalEqualArea:
                return(AsEsriPrjFile((CylindricalEqualArea)mapProjection));

            case MapProjectionType.LambertConformalConic:
                return(AsEsriPrjFile((LambertConformalConic2P)mapProjection));

            case MapProjectionType.Mercator:
                return(AsEsriPrjFile((Mercator)mapProjection));

            case MapProjectionType.TransverseMercator:
                return(AsEsriPrjFile((TransverseMercator)mapProjection));

            case MapProjectionType.UTM:
                return(AsEsriPrjFile((UTM)mapProjection));

            case MapProjectionType.WebMercator:
                return(AsEsriPrjFile((WebMercator)mapProjection));

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 2
0
        public void SaveAsShapefile(string shpFileName, Encoding encoding, SrsBase srs, bool overwrite = false)
        {
            //SaveAsShapefile(shpFileName, values.Select(v => geometryMap(v)), false, srs, overwrite);

            //DbfFile.Write(GetDbfFileName(shpFileName), values, attributeMappings, encoding, overwrite);

            ShapefileFormat.Shapefile.SaveAsShapefile(shpFileName, Features, f => f.TheSqlGeometry.AsEsriShape(), false, srs, overwrite);

            ShapefileFormat.Dbf.DbfFile.Write(ShapefileFormat.Shapefile.GetDbfFileName(shpFileName), Features.Select(f => f.Attributes).ToList(), encoding, overwrite);
        }
Exemplo n.º 3
0
        public static ShapefileDataSource <SqlFeature> Create(string shapefileName, SrsBase targetCrs, Encoding encoding = null)
        {
            Func <SqlFeature, List <object> > inverseAttributeMap = feature => feature.Attributes.Select(kvp => kvp.Value).ToList();

            var result = ShapefileDataSource <SqlFeature> .Create(shapefileName, mapShapeToSqlFeature, inverseAttributeMap, targetCrs, encoding);

            result.ToDataTableMappingFunc = ToDataTableDefaultMappings.SqlFeatureTypeMapping;

            return(result);
        }
Exemplo n.º 4
0
        public static void SaveAsPrj(string shpFileName, SrsBase srs, bool overwrite)
        {
            var prjFileName = GetPrjFileName(shpFileName);

            if (overwrite && System.IO.File.Exists(prjFileName))
            {
                System.IO.File.Delete(prjFileName);
            }

            srs.AsEsriPrj().Save(prjFileName);
        }
Exemplo n.º 5
0
        //public static void Save<T>(string shpFileName, IEnumerable<T> objects, Func<T, IEsriShape> map, bool createDbf = false, bool overwrite = false)
        //{
        //    if (objects.IsNullOrEmpty())
        //    {
        //        return;
        //    }

        //    IEsriShapeCollection collection = new EsriShapeCollection<IEsriShape>(objects.Select(o => map(o)));

        //    Save(shpFileName, collection, createDbf, overwrite);
        //}

        //public static void Save(string shpFileName, IEnumerable<IEsriShape> shapes, bool createDbf = false, bool overwrite = false)
        //{
        //    if (shapes.IsNullOrEmpty())
        //    {
        //        return;
        //    }

        //    IEsriShapeCollection collection = new EsriShapeCollection<IEsriShape>(shapes);

        //    Save(shpFileName, collection, createDbf, overwrite);
        //}

        public static void Save <T>(string shpFileName,
                                    IEnumerable <T> values,
                                    Func <T, IEsriShape> geometryMap,
                                    List <ObjectToDbfTypeMap <T> > attributeMappings,
                                    Encoding encoding,
                                    SrsBase srs,
                                    bool overwrite = false)
        {
            SaveAsShapefile(shpFileName, values.Select(v => geometryMap(v)), false, srs, overwrite);

            DbfFile.Write(GetDbfFileName(shpFileName), values, attributeMappings, encoding, overwrite);
        }
        public static T Project <T>(this T point, SrsBase sourceSrs, SrsBase targetSrs) where T : IPoint, new()
        {
            if (sourceSrs.Ellipsoid.AreTheSame(targetSrs.Ellipsoid))
            {
                var c1 = sourceSrs.ToGeodetic(point);

                return(targetSrs.FromGeodetic(c1));
            }
            else
            {
                var c1 = sourceSrs.ToGeodetic(point);

                return(targetSrs.FromGeodetic(c1, sourceSrs.Ellipsoid));
            }
        }
Exemplo n.º 7
0
        public static List <IEsriShape> Project(string shpFileName, SrsBase targetSrs)
        {
            var sourcePrj = GetPrjFileName(shpFileName);

            if (!System.IO.File.Exists(sourcePrj))
            {
                throw new System.IO.FileNotFoundException($"prj file not found. {sourcePrj}");
            }

            var sourceSrs = new Prj.EsriPrjFile(sourcePrj).AsMapProjection();

            var data = ReadShapes(shpFileName).ToList();

            return(Project(data, sourceSrs, targetSrs));

            ////Old Method
            //var sourcePrj = GetPrjFileName(shpFileName);
            //if (!System.IO.File.Exists(sourcePrj))
            //{
            //    throw new System.IO.FileNotFoundException($"prj file not found. {sourcePrj}");
            //}
            //var sourceCrs = new Prj.PrjFile(sourcePrj).AsMapProjection();
            //var data = Read(shpFileName).ToList();
            //List<IShape> result = new List<IShape>(data.Count);
            //if (sourceCrs.Ellipsoid.AreTheSame(targetCrs.Ellipsoid))
            //{
            //    for (int i = 0; i < data.Count; i++)
            //    {
            //        var c1 = data[i].Transform(p => sourceCrs.ToGeodetic(p));
            //        result.Add(c1.Transform(p => targetCrs.FromGeodetic(p)));
            //    }
            //}
            //else
            //{
            //    for (int i = 0; i < data.Count; i++)
            //    {
            //        var c1 = data[i].Transform(p => sourceCrs.ToGeodetic(p));
            //        result.Add(c1.Transform(p => targetCrs.FromGeodetic(p, sourceCrs.Ellipsoid)));
            //    }
            //}
            //return result;
        }
Exemplo n.º 8
0
        public static List <IEsriShape> Project(List <IEsriShape> values, SrsBase sourceSrs, SrsBase targetSrs) /*where TEsriPoint : IPoint, new()*/
        {
            List <IEsriShape> result = new List <IEsriShape>(values.Count);

            if (sourceSrs.Ellipsoid.AreTheSame(targetSrs.Ellipsoid))
            {
                for (int i = 0; i < values.Count; i++)
                {
                    var c1 = values[i].Transform(p => sourceSrs.ToGeodetic <EsriPoint>(new EsriPoint()
                    {
                        X = p.X, Y = p.Y
                    }), targetSrs.Srid);

                    result.Add(c1.Transform(p => targetSrs.FromGeodetic <EsriPoint>(new EsriPoint()
                    {
                        X = p.X, Y = p.Y
                    }), targetSrs.Srid));
                }
            }
            else
            {
                for (int i = 0; i < values.Count; i++)
                {
                    var c1 = values[i].Transform(p => sourceSrs.ToGeodetic <EsriPoint>(new EsriPoint()
                    {
                        X = p.X, Y = p.Y
                    }), targetSrs.Srid);

                    result.Add(c1.Transform(p => targetSrs.FromGeodetic <EsriPoint>(new EsriPoint()
                    {
                        X = p.X, Y = p.Y
                    }, sourceSrs.Ellipsoid), targetSrs.Srid));
                }
            }

            return(result);
        }
        public static void SaveAsShapefile <T>(this Feature <T> feature, string shpFileName, SrsBase srs = null) where T : IPoint, new()
        {
            if (feature == null)
            {
                return;
            }

            IRI.Ket.ShapefileFormat.Shapefile.SaveAsShapefile(shpFileName, new List <Feature <T> >()
            {
                feature
            }, srs);
        }
Exemplo n.º 10
0
        //public static Func<Point, Point> GetProjectFunc(string shapefileName, SrsBase targetCrs)
        //{
        //    //var sourcePrj = IRI.Ket.ShapefileFormat.Shapefile.GetPrjFileName(shapefileName);

        //    //if (!System.IO.File.Exists(sourcePrj))
        //    //{
        //    //    throw new System.IO.FileNotFoundException($"prj file not found. {sourcePrj}");
        //    //}

        //    //var sourceCrs = new ShapefileFormat.Prj.EsriPrjFile(sourcePrj).AsMapProjection();
        //    var sourceSrs = TryGetSrs(shapefileName);

        //    Func<Point, Point> result = null;

        //    if (sourceSrs.Ellipsoid.AreTheSame(targetCrs.Ellipsoid))
        //    {
        //        result = new Func<Point, Point>(p => (Point)targetCrs.FromGeodetic(sourceSrs.ToGeodetic(p)));
        //    }
        //    else
        //    {
        //        result = new Func<Point, Point>(p => (Point)targetCrs.FromGeodetic(sourceSrs.ToGeodetic(p), sourceSrs.Ellipsoid));
        //    }

        //    return result;
        //}

        public static Task <List <IEsriShape> > ProjectAsync(string shpFileName, SrsBase targetSrs)
        {
            return(Task.Run(() => Project(shpFileName, targetSrs)));
        }
        public static void SaveAsShapefile <T>(this IEnumerable <Feature <T> > features, string shpFileName, SrsBase srs = null) where T : IPoint, new()
        {
            if (features.IsNullOrEmpty())
            {
                return;
            }

            IRI.Ket.ShapefileFormat.Shapefile.SaveAsShapefile(shpFileName, features, srs);
        }
Exemplo n.º 12
0
        public static void SaveAsShapefile <T>(string shpFileName, IEnumerable <Feature <T> > features, SrsBase srs = null) where T : IPoint, new()
        {
            var shapes = features.Select(f => f.TheGeometry.AsEsriShape());

            Save(shpFileName, shapes, false, true, srs);

            var dbfFile = GetDbfFileName(shpFileName);

            var attributes = features.Select(f => f.Attributes).ToList();

            DbfFile.Write(dbfFile, attributes, Encoding.GetEncoding(1256), true);
        }
Exemplo n.º 13
0
        public static async Task <ShapefileDataSource <T> > CreateAsync(string shapefileName, Func <SqlGeometry, Dictionary <string, object>, T> map, Func <T, List <Object> > inverseAttributeMap, SrsBase targetSrs = null, Encoding encoding = null)
        {
            var attributes = DbfFile.Read(ShapefileFormat.Shapefile.GetDbfFileName(shapefileName), true, encoding);

            var geometries = await ShapefileFormat.Shapefile.ReadShapesAsync(shapefileName);

            return(new ShapefileDataSource <T>(shapefileName, geometries, attributes, map, inverseAttributeMap, targetSrs));
        }
Exemplo n.º 14
0
        public static async Task <List <SqlFeature> > ReadAsSqlFeatureAsync(string shpFileName, Encoding dataEncoding, SrsBase targetSrs = null, Encoding headerEncoding = null, bool correctFarsiCharacters = true, string label = null)
        {
            if (targetSrs != null)
            {
                var sourceSrs = IRI.Ket.ShapefileFormat.Shapefile.TryGetSrs(shpFileName);

                Func <Point, Point> map = p => p.Project(sourceSrs, targetSrs);

                return(await IRI.Ket.ShapefileFormat.Shapefile.ReadAsync <SqlFeature>(
                           shpFileName,
                           (d, ies) => new SqlFeature()
                {
                    Attributes = d, LabelAttribute = label, TheSqlGeometry = ies.Transform(map as Func <IPoint, IPoint>, targetSrs.Srid).AsSqlGeometry()
                },
                           //d => new SqlFeature() { Attributes = d, LabelAttribute = label },
                           //(d, srid, feature) => feature.TheSqlGeometry = d.Transform(map, targetSrs.Srid).AsSqlGeometry(),
                           true,
                           dataEncoding,
                           null,
                           headerEncoding));
            }
            else
            {
                return(await IRI.Ket.ShapefileFormat.Shapefile.ReadAsync <SqlFeature>(
                           shpFileName,
                           (d, ies) => new SqlFeature()
                {
                    Attributes = d, LabelAttribute = label, TheSqlGeometry = ies.AsSqlGeometry()
                },
                           //d => new SqlFeature() { Attributes = d, LabelAttribute = label },
                           //(d, srid, feature) => feature.TheSqlGeometry = d.AsSqlGeometry(),
                           true,
                           dataEncoding,
                           null,
                           headerEncoding));
            }
        }
Exemplo n.º 15
0
        public static void ProjectAndSaveAsShapefile(string sourceShapefileName, string targetShapefileName, SrsBase targetSrs, bool overwrite = false)
        {
            if (!CheckAllNeededFilesExists(sourceShapefileName, true))
            {
                throw new NotImplementedException();
            }

            var result = Project(sourceShapefileName, targetSrs);

            //result.ToList().Select(i=>i.MinimumBoundingBox).ToList()
            SaveAsShapefile(targetShapefileName, result, false, targetSrs, overwrite);

            var destinationDbfFileName = GetDbfFileName(targetShapefileName);

            System.IO.File.Copy(GetDbfFileName(sourceShapefileName), destinationDbfFileName, overwrite);
        }
Exemplo n.º 16
0
        public static void Save(string shpFileName, IEnumerable <IEsriShape> shapes, bool createDbf = false, bool overwrite = false, SrsBase srs = null)
        {
            if (shapes.IsNullOrEmpty())
            {
                return;
            }

            var directory = System.IO.Path.GetDirectoryName(shpFileName);

            if (!System.IO.Directory.Exists(directory) && !string.IsNullOrEmpty(directory))
            {
                System.IO.Directory.CreateDirectory(directory);
            }

            IEsriShapeCollection collection = new EsriShapeCollection <IEsriShape>(shapes);

            EsriShapeType shapeType = shapes.First().Type;

            using (System.IO.MemoryStream featureWriter = new System.IO.MemoryStream())
            {
                int recordNumber = 0;

                foreach (IEsriShape item in shapes)
                {
                    featureWriter.Write(ShpWriter.WriteHeaderToByte(++recordNumber, item), 0, 2 * ShapeConstants.IntegerSize);

                    featureWriter.Write(item.WriteContentsToByte(), 0, 2 * item.ContentLength);
                }

                using (System.IO.MemoryStream shpWriter = new System.IO.MemoryStream())
                {
                    int fileLength = (int)featureWriter.Length / 2 + 50;

                    shpWriter.Write(ShpWriter.WriteMainHeader(collection, fileLength, shapeType), 0, 100);

                    shpWriter.Write(featureWriter.ToArray(), 0, (int)featureWriter.Length);

                    //var mode = overwrite ? System.IO.FileMode.Create : System.IO.FileMode.CreateNew;
                    var mode = Shapefile.GetMode(shpFileName, overwrite);

                    System.IO.FileStream stream = new System.IO.FileStream(shpFileName, mode);

                    shpWriter.WriteTo(stream);

                    stream.Close();

                    shpWriter.Close();

                    featureWriter.Close();
                }
            }

            ShxWriter.Write(Shapefile.GetShxFileName(shpFileName), collection, shapeType, overwrite);

            if (createDbf)
            {
                Dbf.DbfFile.WriteDefault(Shapefile.GetDbfFileName(shpFileName), collection.Count, overwrite);
            }

            //try to automatically find srs
            if (srs == null)
            {
                var srid = shapes.First()?.Srid ?? 0;

                srs = SridHelper.AsSrsBase(srid);
            }

            if (srs != null)
            {
                SaveAsPrj(shpFileName, srs, overwrite);
            }
        }
Exemplo n.º 17
0
        private ShapefileDataSource(string shapefileName,
                                    IEsriShapeCollection geometries,
                                    EsriAttributeDictionary attributes,
                                    Func <SqlGeometry, Dictionary <string, object>, T> map,
                                    Func <T, List <object> > inverseAttributeMap,
                                    SrsBase targetSrs)
        {
            if (attributes == null)
            {
                throw new NotImplementedException();
            }

            this._shapefileName = shapefileName;

            _sourceSrs = IRI.Ket.ShapefileFormat.Shapefile.TryGetSrs(shapefileName);

            _targetSrs = targetSrs;

            Func <Point, Point> transformFunc = null;

            if (targetSrs != null)
            {
                transformFunc = p => p.Project(_sourceSrs, targetSrs);
            }

            //string title = System.IO.Path.GetFileNameWithoutExtension(shapefileName);

            this.Extent = geometries.MainHeader.MinimumBoundingBox;

            if (transformFunc != null)
            {
                this.Extent = this.Extent.Transform(p => transformFunc(p));
            }

            // 1400.02.03-comment
            //this._fields = new ObjectToDfbFields<T>() { ExtractAttributesFunc = inverseAttributeMap, Fields = attributes.Fields };

            // 1400.02.19
            this._fields = new List <ObjectToDbfTypeMap <T> >();

            for (int i = 0; i < attributes.Fields.Count; i++)
            {
                this._fields.Add(new ObjectToDbfTypeMap <T>(attributes.Fields[i], t => inverseAttributeMap(t)[i]));
            }


            if (geometries?.Count != attributes.Attributes?.Count)
            {
                throw new NotImplementedException();
            }

            this._features = new List <T>();

            for (int i = 0; i < geometries.Count; i++)
            {
                SqlGeometry geometry = null;

                if (transformFunc == null)
                {
                    geometry = geometries[i].AsSqlGeometry().MakeValid();
                }
                else
                {
                    geometry = geometries[i].AsSqlGeometry().MakeValid().Transform(p => transformFunc(p), targetSrs.Srid);//targetSrs should not be null in this case
                }

                var feature = map(geometry, attributes.Attributes[i]);

                feature.Id = GetNewId();

                this._idFunc = id => this._features.Single(f => f.Id == id);

                this._features.Add(feature);
            }
        }
        public static void SaveAsShapefile(this IEnumerable <GeoJsonFeature> features, string shpFileName, bool isLongitudeFirst = true, SrsBase srs = null)
        {
            if (features.IsNullOrEmpty())
            {
                return;
            }

            IRI.Ket.ShapefileFormat.Shapefile.SaveAsShapefile(shpFileName, features, isLongitudeFirst, srs);
        }
Exemplo n.º 19
0
        // 1400.03.02- remove method
        //public static void Save<T>(string shpFileName,
        //                              IEnumerable<T> values,
        //                              Func<T, IEsriShape> geometryMap,
        //                              ObjectToDfbFields<T> attributeMappings,
        //                              Encoding encoding,
        //                              SrsBase srs,
        //                              bool overwrite = false)
        //{
        //    SaveAsShapefile(shpFileName, values.Select(v => geometryMap(v)), false, srs, overwrite);

        //    DbfFile.Write(GetDbfFileName(shpFileName), values, attributeMappings, encoding, overwrite);
        //}


        // old
        //public static void Save<T>(string shpFileName,
        //                             IEnumerable<T> values,
        //                             Func<T, IEsriShape> geometryMap,
        //                             List<Func<T, object>> attributeMapping,
        //                             List<DbfFieldDescriptor> fields,
        //                             Encoding encoding,
        //                             SrsBase srs,
        //                             bool overwrite = false)
        //{
        //    SaveAsShapefile(shpFileName, values.Select(v => geometryMap(v)), false, srs, overwrite);

        //    DbfFile.Write(GetDbfFileName(shpFileName), values, attributeMapping, fields, encoding, overwrite);

        //    //DbfFile.Write(GetDbfFileName(shpFileName), values, attributeMappings.Select(m => m.MapFunction).ToList(), attributeMappings.Select(m => m.FieldType).ToList(), encoding, overwrite);
        //}

        public static void SaveAsShapefile(string shpFileName, IEnumerable <IEsriShape> data, bool createEmptyDbf, SrsBase srs, bool overwrite = false)
        {
            Save(shpFileName, data, createEmptyDbf, overwrite, srs);

            //SaveAsPrj(shpFileName, crs, overwrite);
        }
Exemplo n.º 20
0
        public static void SaveAsShapefile <T>(string shpFileName, IEnumerable <T> data, Func <T, IEsriShape> map, bool createEmptyDbf, SrsBase srs, bool overwrite = false)
        {
            Save(shpFileName, data.Select(t => map(t)), createEmptyDbf, overwrite, srs);

            //SaveAsPrj(shpFileName, crs, overwrite);
        }
Exemplo n.º 21
0
        public static void SaveAsShapefile(string shpFileName, IEnumerable <Msh.Common.Model.GeoJson.GeoJsonFeature> features, bool isLongitudeFirst = true, SrsBase srs = null)
        {
            var shapes = features.Select(f => f.Geometry.AsEsriShape(isLongitudeFirst, srs.Srid));

            Save(shpFileName, shapes, false, true, srs);

            var dbfFile = GetDbfFileName(shpFileName);

            var attributes = features.Select(f => f.Properties).ToList();

            DbfFile.Write(dbfFile, attributes, Encoding.GetEncoding(1256), true);
        }
        public static List <Geometry <T> > Project <T>(this List <Geometry <T> > values, SrsBase sourceSrs, SrsBase targetSrs) where T : IPoint, new()
        {
            List <Geometry <T> > result = new List <Geometry <T> >(values.Count);

            if (sourceSrs.Ellipsoid.AreTheSame(targetSrs.Ellipsoid))
            {
                for (int i = 0; i < values.Count; i++)
                {
                    var c1 = values[i].Transform(p => sourceSrs.ToGeodetic(p), SridHelper.GeodeticWGS84);

                    result.Add(c1.Transform(p => targetSrs.FromGeodetic(p), targetSrs.Srid));
                }
            }
            else
            {
                for (int i = 0; i < values.Count; i++)
                {
                    var c1 = values[i].Transform(p => sourceSrs.ToGeodetic(p), SridHelper.GeodeticWGS84);

                    result.Add(c1.Transform(p => targetSrs.FromGeodetic(p, sourceSrs.Ellipsoid), targetSrs.Srid));
                }
            }

            return(result);
        }
        public static async Task <SqlFeatureDataSource> CreateFromShapefileAsync(string shpFileName, string label, Encoding dataEncoding = null, SrsBase targetSrs = null, Encoding headerEncoding = null, bool correctFarsiCharacters = true)
        {
            var features = await ShapefileHelper.ReadAsSqlFeatureAsync(shpFileName, dataEncoding, targetSrs, headerEncoding, correctFarsiCharacters, label);

            var result = new SqlFeatureDataSource(features, i => i.Label);

            //result.ToDataTableMappingFunc = sqlFeatureToDataTableMapping;

            return(result);
        }