Beispiel #1
0
        public static Schema CreateSchema(string name, OSGeo.OGR.wkbGeometryType schematype)
        {
            OSGeo.OGR.wkbGeometryType type = wkbGeometryType.wkbPoint;
            switch (schematype)
            {
            case wkbGeometryType.wkbMultiPoint:
                type = wkbGeometryType.wkbPoint;
                break;

            case wkbGeometryType.wkbPoint:
                type = wkbGeometryType.wkbPoint;
                break;

            case wkbGeometryType.wkbPolygon:
                type = wkbGeometryType.wkbPolygon;
                break;

            case wkbGeometryType.wkbMultiPolygon:
                type = wkbGeometryType.wkbPolygon;
                break;

            case wkbGeometryType.wkbLineString:
                type = wkbGeometryType.wkbLineString;
                break;

            case wkbGeometryType.wkbMultiLineString:
                type = wkbGeometryType.wkbLineString;
                break;
            }
            return(new Schema(name, type, new GRS(new SpatialReference(Osr.SRS_WKT_WGS84)), new Dictionary <string, FieldDefn>()));
        }
Beispiel #2
0
 public ReferenceSystem rs;                    //空间坐标系
 public Schema(String name, OSGeo.OGR.wkbGeometryType type, ReferenceSystem spatialreference, Dictionary <string, FieldDefn> _fields)
 {
     this.name         = name;
     this.geometryType = type;
     this.rs           = spatialreference;
     this.fields       = _fields;
 }
Beispiel #3
0
        public bool CreateLayer(string name, OSGeo.OGR.wkbGeometryType type, string save_featuresourcepath, string save_stylepath)
        {
            FeatureSource featuresource = this.gdb.CreateFeatureSource(Schema.CreateSchema(name, type), save_featuresourcepath);
            Layer         layer         = Layer.LoadLayer(featuresource, name, save_stylepath);

            layerlist.Insert(0, layer);
            return(true);
        }
Beispiel #4
0
        //将内存中一个图层的数据另存为在文件中
        private Boolean SaveAsToFile(String name, String path)
        {
            Ogr.RegisterAll();
            //to support chinese path
            OSGeo.GDAL.Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");
            //to support chinese field name
            OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "");
            string strDriverName = "ESRI Shapefile";
            Driver oDriver       = Ogr.GetDriverByName(strDriverName);

            if (oDriver == null)
            {
                //说明driver不可用
                return(false);
            }
            String pathPrefix  = System.IO.Path.GetDirectoryName(path);
            String pathPostfix = System.Guid.NewGuid().ToString();

            if (File.Exists(pathPrefix + "\\" + pathPostfix))
            {
                File.Delete(pathPrefix + "\\" + pathPostfix);
            }

            //create a datasource, if successfully, a shp file would be created
            DataSource oDs       = oDriver.CreateDataSource(pathPrefix + "\\" + pathPostfix, null);
            Schema     tmpSchema = this.featureSources[name].schema;
            Layer      layer     = oDs.CreateLayer(name, tmpSchema.rs.spetialReference, tmpSchema.geometryType, null);

            //insert all the fields to layer
            //use index instead of field name to avoid the error caused by chinese words
            int fieldcount = tmpSchema.fields.Count;

            for (int i = 0; i < fieldcount; ++i)
            {
                layer.CreateField(tmpSchema.fields.ElementAt(i).Value, 1);
            }
            FeatureDefn fdef = layer.GetLayerDefn();

            FeatureCollection fc = this.featureSources[name].features;
            int fcount           = fc.count;

            for (int i = 0; i < fcount; ++i)
            {
                GisSmartTools.Data.Feature tmpFeature = fc.featureList[i];
                if (!tmpFeature.visible)
                {
                    continue;
                }
                //create a new feature
                OSGeo.OGR.Feature newFeature = new OSGeo.OGR.Feature(fdef);

                //set attribute
                newFeature.SetFID(tmpFeature.featureID);
                for (int j = 0; j < fieldcount; ++j)
                {
                    String    fieldname = tmpSchema.fields.ElementAt(j).Key;
                    FieldType ft        = tmpSchema.fields[fieldname].GetFieldType();
                    try{
                        switch (ft)
                        {
                        case FieldType.OFTString:
                            newFeature.SetField(j, (String)tmpFeature.attributes[fieldname]);
                            break;

                        case FieldType.OFTInteger:
                            newFeature.SetField(j, (int)tmpFeature.attributes[fieldname]);
                            break;

                        case FieldType.OFTReal:
                            newFeature.SetField(j, (double)tmpFeature.attributes[fieldname]);
                            break;

                        case FieldType.OFTWideString:
                            newFeature.SetField(j, (String)tmpFeature.attributes[fieldname]);
                            break;

                        default:
                            newFeature.SetField(j, (String)tmpFeature.attributes[fieldname]);
                            break;
                        }
                    }catch (Exception e)
                    {
                    }
                }

                //get geometry
                OSGeo.OGR.wkbGeometryType featType = tmpFeature.geometry.geometryType;
                OSGeo.OGR.Geometry        geo      = new OSGeo.OGR.Geometry(featType);
                if (geo != null)
                {
                    switch (featType)
                    {
                    case OSGeo.OGR.wkbGeometryType.wkbPoint:
                        GisSmartTools.Geometry.PointD tmpPoint = (GisSmartTools.Geometry.PointD)tmpFeature.geometry;
                        geo.AddPoint(tmpPoint.X, tmpPoint.Y, 0.00);
                        break;

                    case OSGeo.OGR.wkbGeometryType.wkbLineString:
                        GisSmartTools.Geometry.SimplePolyline tmpLine = (GisSmartTools.Geometry.SimplePolyline)tmpFeature.geometry;
                        foreach (GisSmartTools.Geometry.PointD po in tmpLine.points)
                        {
                            geo.AddPoint(po.X, po.Y, 0.00);
                        }
                        break;

                    case wkbGeometryType.wkbMultiLineString:
                        GisSmartTools.Geometry.Polyline lines = (GisSmartTools.Geometry.Polyline)tmpFeature.geometry;
                        foreach (SimplePolyline line in lines.childPolylines)
                        {
                            OSGeo.OGR.Geometry tmpgeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLineString);
                            foreach (GisSmartTools.Geometry.PointD point in line.points)
                            {
                                tmpgeo.AddPoint(point.X, point.Y, 0.00);
                            }
                            geo.AddGeometryDirectly(tmpgeo);
                        }
                        break;

                    case OSGeo.OGR.wkbGeometryType.wkbPolygon:
                        GisSmartTools.Geometry.SimplePolygon gon = (GisSmartTools.Geometry.SimplePolygon)tmpFeature.geometry;
                        foreach (SimplePolyline ring7 in gon.rings)
                        {
                            OSGeo.OGR.Geometry tmpgeo = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
                            foreach (GisSmartTools.Geometry.PointD point in ring7.points)
                            {
                                tmpgeo.AddPoint(point.X, point.Y, 0.00);
                            }
                            geo.AddGeometryDirectly(tmpgeo);
                        }
                        break;

                    case wkbGeometryType.wkbMultiPolygon:
                        GisSmartTools.Geometry.Polygon gons = (GisSmartTools.Geometry.Polygon)tmpFeature.geometry;
                        foreach (GisSmartTools.Geometry.SimplePolygon cgon in gons.childPolygons)
                        {
                            OSGeo.OGR.Geometry geogon = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPolygon);
                            foreach (GisSmartTools.Geometry.SimplePolyline ring6 in cgon.rings)
                            {
                                OSGeo.OGR.Geometry geoline = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbLinearRing);
                                foreach (GisSmartTools.Geometry.PointD point6 in ring6.points)
                                {
                                    geoline.AddPoint(point6.X, point6.Y, 0.00);
                                }
                                geogon.AddGeometryDirectly(geoline);
                            }
                            geo.AddGeometryDirectly(geogon);
                        }
                        break;

                    default:
                        break;
                    }
                }

                //set feature
                newFeature.SetGeometry(geo);
                //add to layer
                layer.CreateFeature(newFeature);
            }

            //call Dispose method to save to file
            oDs.Dispose();
            DataSource tds;

            if (this.iDses.TryGetValue(path, out tds))
            {
                tds.Dispose();
                this.iDses.Remove(path);
            }

            //enumerate all the files in the temp file directory and move them to the output directory
            string[] tmpName = System.IO.Directory.GetFiles(pathPrefix + "\\" + pathPostfix);
            foreach (string file in tmpName)
            {
                string decFile = pathPrefix + "\\" + Path.GetFileName(file);
                if (File.Exists(decFile))
                {
                    File.Delete(decFile);
                }
                File.Move(file, decFile);
            }
            Directory.Delete(pathPrefix + "\\" + pathPostfix, true);
            return(true);
        }
Beispiel #5
0
        //read data really
        private static FeatureCollection GetFeatureCollection(OSGeo.OGR.Layer layer, GisSmartTools.Data.Schema schema)
        {
            FeatureCollection fc = new FeatureCollection();

            OSGeo.OGR.Feature     feature;
            OSGeo.OGR.FeatureDefn posDefn = layer.GetLayerDefn();
            int fieldCount = posDefn.GetFieldCount();
            int iField     = 0;

            //read all the features in layer
            while ((feature = layer.GetNextFeature()) != null)
            {
                OSGeo.OGR.Geometry         geometry = feature.GetGeometryRef();
                GisSmartTools.Data.Feature feat     = new Feature(feature.GetFID(), schema, null);
                //get and save feature's attributes
                for (iField = 0; iField < fieldCount; iField++)
                {
                    OSGeo.OGR.FieldDefn oField = posDefn.GetFieldDefn(iField);
                    Object    objAttr;
                    FieldType ft = oField.GetFieldType();
                    switch (ft)
                    {
                    case FieldType.OFTString:
                        objAttr = feature.GetFieldAsString(iField);
                        break;

                    case FieldType.OFTInteger:
                        objAttr = feature.GetFieldAsInteger(iField);
                        break;

                    case FieldType.OFTReal:
                        objAttr = feature.GetFieldAsDouble(iField);
                        break;

                    case FieldType.OFTWideString:
                        objAttr = feature.GetFieldAsString(iField);
                        break;

                    case FieldType.OFTStringList:
                        objAttr = feature.GetFieldAsStringList(iField);
                        break;

                    case FieldType.OFTIntegerList:
                        int outCount;
                        objAttr = feature.GetFieldAsIntegerList(iField, out outCount);
                        break;

                    case FieldType.OFTRealList:
                        int outCount2;
                        objAttr = feature.GetFieldAsDoubleList(iField, out outCount2);
                        break;

                    case FieldType.OFTWideStringList:
                        objAttr = feature.GetFieldAsStringList(iField);
                        break;

                    default:
                        objAttr = feature.GetFieldAsString(iField);
                        break;
                    }
                    feat.AddAttribute(oField.GetName(), objAttr);
                }

                //get geometry
                if (geometry != null)
                {
                    OSGeo.OGR.wkbGeometryType goetype = geometry.GetGeometryType();
                    switch (goetype)    //according to the type, we operate differently
                    {
                    case wkbGeometryType.wkbPoint:
                        if (geometry != null && goetype == wkbGeometryType.wkbPoint)
                        {
                            feat.geometry = new PointD(geometry.GetX(0), geometry.GetY(0));
                        }
                        //add feature to featureCollection
                        fc.InsertFeature(feat);
                        break;

                    case wkbGeometryType.wkbLineString:
                        List <PointD> pointlist  = new List <PointD>();
                        int           pointcount = geometry.GetPointCount();
                        for (int k = 0; k < pointcount; k++)
                        {
                            pointlist.Add(new PointD(geometry.GetX(k), geometry.GetY(k)));
                        }
                        feat.geometry = new SimplePolyline(pointlist);
                        //add feature to featureCollection
                        fc.InsertFeature(feat);
                        break;

                    case wkbGeometryType.wkbMultiLineString:
                        feat.geometry = GetPolyline(geometry);
                        fc.InsertFeature(feat);
                        break;

                    case wkbGeometryType.wkbPolygon:
                        feat.geometry = GetSimplePolygon(geometry);
                        //add feature to featureCollection
                        fc.InsertFeature(feat);
                        break;

                    case wkbGeometryType.wkbMultiPolygon:
                        feat.geometry = GetPolygon(geometry);
                        fc.InsertFeature(feat);
                        break;

                    default:
                        //we don't support another geometry types,
                        //so we don't load those records to avoid errors
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("kkkkkkkkkkkkkkkkkkkkkkkkkkkkkk");
                }
            }

            return(fc);
        }
Beispiel #6
0
        public static bool CovertRegion2Line(string path, string savePath)
        {
            try
            {
                OSGeo.OGR.Ogr.RegisterAll();
                OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");
                if (dr == null)
                {
                    Console.WriteLine("启动驱动失败!\n");
                }

                OSGeo.OGR.DataSource ds    = dr.Open(path, 0);
                int             layerCount = ds.GetLayerCount();
                OSGeo.OGR.Layer layer      = ds.GetLayerByIndex(0);
                FeatureDefn     fdf        = layer.GetLayerDefn();
                int             fieldCount = fdf.GetFieldCount();

                // 创建数据源
                DataSource oDS = dr.CreateDataSource(savePath, null);
                if (oDS == null)
                {
                    Console.WriteLine("创建矢量文件【%s】失败!\n", savePath);
                    return(false);
                }
                OSGeo.OSR.SpatialReference srs = layer.GetSpatialRef();
                // 创建图层,创建一个多边形图层,这里没有指定空间参考,如果需要的话,需要在这里进行指定
                Layer oLayer = oDS.CreateLayer("line", srs, OSGeo.OGR.wkbGeometryType.wkbMultiLineString, null);

                if (oLayer == null)
                {
                    Console.WriteLine("图层创建失败!\n");
                    return(false);
                }
                for (int i = 0; i < fieldCount; i++)
                {
                    FieldDefn fieldDefn = fdf.GetFieldDefn(i);
                    oLayer.CreateField(fieldDefn, 1);
                }
                FeatureDefn       oDefn = oLayer.GetLayerDefn();
                OSGeo.OGR.Feature feat;
                while ((feat = layer.GetNextFeature()) != null)
                {
                    Feature            oFeatureMulty = new Feature(oDefn);
                    OSGeo.OGR.Geometry geometry      = feat.GetGeometryRef();
                    for (int i = 0; i < fieldCount; i++)
                    {
                        string value = feat.GetFieldAsString(i);
                        oFeatureMulty.SetField(i, value);
                    }
                    OSGeo.OGR.wkbGeometryType goetype = geometry.GetGeometryType();
                    if (goetype == OSGeo.OGR.wkbGeometryType.wkbPolygon)
                    {
                        Geometry tempGeo = new Geometry(wkbGeometryType.wkbMultiLineString);
                        int      count   = geometry.GetGeometryCount();
                        for (int j = 0; j < count; j++)
                        {
                            Geometry line = new Geometry(wkbGeometryType.wkbLinearRing);
                            Geometry geo  = geometry.GetGeometryRef(j);
                            for (int i = 0; i < geo.GetPointCount(); i++)
                            {
                                double x = geo.GetX(i);
                                double y = geo.GetY(i);
                                line.AddPoint(x, y, 0);
                            }
                            tempGeo.AddGeometryDirectly(line);
                        }
                        oFeatureMulty.SetGeometry(tempGeo);
                    }
                    oLayer.SetFeature(oFeatureMulty);
                }
                oLayer.Dispose();
                layer.Dispose();
                oDS.Dispose();
                ds.Dispose();
                dr.Dispose();
            }
            catch
            { }
            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// 转换并纠偏,只使用与火星坐标纠偏
        /// </summary>
        /// <param name="dataTable">数据表,如果是点表中含X,Y字段,如果是线和面,表中含有PATH字段</param>
        /// <param name="filePath">存储路径</param>
        /// <param name="geoType">数据类型,暂时支持简单的点线面</param>
        //public static void saveShpFile(DataTable dataTable,string filePath,wkbGeometryType geoType)
        //{
        //    saveShpFile(dataTable, filePath, geoType, ProjectConvert.GCJ_WGS);
        //}
        /// <summary>
        /// 导出shp数据到表格
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>DataTable</returns>
        public static DataTable GetData(string path)
        {
            try
            {
                OSGeo.OGR.Ogr.RegisterAll();
                OSGeo.OGR.Driver dr = OSGeo.OGR.Ogr.GetDriverByName("ESRI shapefile");

                if (dr == null)
                {
                    Console.WriteLine("启动驱动失败!\n");
                }

                OSGeo.OGR.DataSource ds    = dr.Open(path, 0);
                int             layerCount = ds.GetLayerCount();
                OSGeo.OGR.Layer layer      = ds.GetLayerByIndex(0);
                DataTable       dataTable  = new DataTable();
                FeatureDefn     fdf        = layer.GetLayerDefn();
                int             fieldCount = fdf.GetFieldCount();
                for (int i = 0; i < fieldCount; i++)
                {
                    dataTable.Columns.Add(fdf.GetFieldDefn(i).GetName(), Type.GetType("System.String"));
                }
                if (fdf.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbPoint)
                {
                    if (!dataTable.Columns.Contains("X"))
                    {
                        dataTable.Columns.Add("X", Type.GetType("System.String"));
                    }
                    if (!dataTable.Columns.Contains("Y"))
                    {
                        dataTable.Columns.Add("Y", Type.GetType("System.String"));
                    }
                }
                else if (fdf.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbPolygon || fdf.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbLineString)
                {
                    if (!dataTable.Columns.Contains("PATH"))
                    {
                        dataTable.Columns.Add("PATH", Type.GetType("System.String"));
                    }
                }
                OSGeo.OGR.Feature feat;
                while ((feat = layer.GetNextFeature()) != null)
                {
                    DataRow            row      = dataTable.NewRow();
                    OSGeo.OGR.Geometry geometry = feat.GetGeometryRef();
                    for (int i = 0; i < fieldCount; i++)
                    {
                        string value = feat.GetFieldAsString(i);
                        row[i] = value.Replace(",", "");
                    }
                    OSGeo.OGR.wkbGeometryType goetype = geometry.GetGeometryType();
                    if (goetype == OSGeo.OGR.wkbGeometryType.wkbPoint)
                    {
                        double x = geometry.GetX(0);
                        double y = geometry.GetY(0);
                        row["X"] = x.ToString();
                        row["Y"] = y.ToString();
                    }
                    else if (goetype == OSGeo.OGR.wkbGeometryType.wkbLineString)
                    {
                        string points = "";
                        for (int i = 0; i < geometry.GetPointCount(); i++)
                        {
                            double x = geometry.GetX(i);
                            double y = geometry.GetY(i);
                            points += x.ToString() + "|" + y.ToString() + ";";
                        }
                        if (points != "")
                        {
                            points.Substring(0, points.Length - 1);
                        }
                        row["PATH"] = points;
                    }
                    else if (goetype == OSGeo.OGR.wkbGeometryType.wkbPolygon)
                    {
                        int    count  = geometry.GetGeometryCount();
                        string points = "";
                        for (int j = 0; j < count; j++)
                        {
                            Geometry geo = geometry.GetGeometryRef(j);
                            for (int i = 0; i < geo.GetPointCount(); i++)
                            {
                                double x = geo.GetX(i);
                                double y = geo.GetY(i);
                                points += x.ToString() + "|" + y.ToString() + ";";
                            }
                            if (points != "")
                            {
                                points.Substring(0, points.Length - 1);
                            }
                            points += "|";
                        }
                        if (points != "")
                        {
                            points.Substring(0, points.Length - 1);
                        }
                        row["PATH"] = points;
                    }
                }
                layer.Dispose();
                ds.Dispose();
                dr.Dispose();
                return(dataTable);
            }
            catch
            {
                return(null);
            }
        }
Beispiel #8
0
        /// <summary>
        /// 写shp文件
        /// </summary>
        /// <param name="lstAttribute"></param>
        /// <param name="geometryType"></param>
        /// <param name="lstWkt"></param>
        /// <param name="lstAttributeObj"></param>
        /// <param name="srsWkt"></param>
        /// <returns></returns>
        public bool DoExport(List <AttributeModel> lstAttribute, OSGeo.OGR.wkbGeometryType geometryType, List <string> lstWkt, List <AttributeObj> lstAttributeObj, string srsWkt)
        {
            _lstAttribute    = lstAttribute;
            _geoType         = geometryType;
            _lstWkt          = lstWkt;
            _lstAttributeObj = lstAttributeObj;

            try
            {
                //注册
                string pszDriverName = "ESRI Shapefile";

                //调用对Shape文件读写的Driver接口
                OSGeo.OGR.Driver poDriver = OSGeo.OGR.Ogr.GetDriverByName(pszDriverName);
                if (poDriver == null)
                {
                    throw new Exception("Driver Error");
                }

                //用此Driver创建Shape文件
                OSGeo.OGR.DataSource poDS;
                poDS = poDriver.CreateDataSource(_shpFileName, null);
                if (poDS == null)
                {
                    throw new Exception("DataSource Creation Error");
                }

                //定义坐标系
                OSGeo.OSR.SpatialReference srs = new OSGeo.OSR.SpatialReference(srsWkt);
                //创建层Layer
                OSGeo.OGR.Layer poLayer   = null;
                string          layerName = Path.GetFileNameWithoutExtension(_shpFileName);
                poLayer = poDS.CreateLayer(layerName, srs, _geoType, null);

                if (poLayer == null)
                {
                    throw new Exception("Layer Creation Failed");
                }

                //创建属性列
                foreach (AttributeModel att in _lstAttribute)
                {
                    OSGeo.OGR.FieldDefn oField = new OSGeo.OGR.FieldDefn(att.AttributeName, att.AttributeType);
                    //if (att.AttributeWidth > 0)
                    //{
                    oField.SetWidth(att.AttributeWidth);
                    //}
                    oField.SetPrecision(att.AttributePrecision);


                    poLayer.CreateField(oField, att.AttributeApproxOK);
                }

                //创建一个Feature,一个Geometry
                OSGeo.OGR.Feature         poFeature  = new OSGeo.OGR.Feature(poLayer.GetLayerDefn());
                OSGeo.OGR.wkbGeometryType wkbGeotype = OSGeo.OGR.wkbGeometryType.wkbGeometryCollection;
                OSGeo.OGR.Geometry        geo        = new OSGeo.OGR.Geometry(wkbGeotype);

                for (int i = 0; i < _lstWkt.Count; i++)
                {
                    foreach (KeyValuePair <string, string> item in _lstAttributeObj[i].AttributeValue)
                    {
                        poFeature.SetField(item.Key, item.Value);
                    }
                    geo = OSGeo.OGR.Geometry.CreateFromWkt(_lstWkt[i]);
                    poFeature.SetGeometry(geo);
                    poLayer.CreateFeature(poFeature);
                }
                //关闭文件读写
                poFeature.Dispose();
                poDS.Dispose();

                return(true);
            }
            catch (System.Exception e)
            {
                return(false);
                //throw new Exception(e.Message);
            }
        }