public void test(ref Layer oLayer) { // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); // 获取图层中的属性表表头并输出 string strInfo = "属性表结构信息:\n"; FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); strInfo += string.Format("{0}:{1} ({2}.{3})\n", oField.GetNameRef(), oField.GetFieldTypeName(oField.GetFieldType()), oField.GetWidth(), oField.GetPrecision()); } // 输出图层中的要素个数 strInfo += string.Format("要素个数 = {0}\n", oLayer.GetFeatureCount(0)); Feature oFeature = null; // 下面开始遍历图层中的要素 while ((oFeature = oLayer.GetNextFeature()) != null) { strInfo += string.Format("\n当前处理第{0}个: \n属性值:", oFeature.GetFID()); // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); switch (type) { case FieldType.OFTString: IntPtr pchar = OGR_F_GetFieldAsString(Feature.getCPtr(oFeature), iField); string val = Marshal.PtrToStringAnsi(pchar); strInfo += string.Format("{0}\t", val); break; case FieldType.OFTReal: strInfo += string.Format("{0}\t", oFeature.GetFieldAsDouble(iField)); break; case FieldType.OFTInteger: strInfo += string.Format("{0}\t", oFeature.GetFieldAsInteger(iField)); break; default: strInfo += string.Format("{0}\t", oFeature.GetFieldAsString(iField)); break; } } // 获取要素中的几何体 Geometry oGeometry = oFeature.GetGeometryRef(); // 为了演示,只输出一个要素信息 break; } strInfo += "\n数据集关闭!"; }
private DataTable BuildSchemaTable() { _schemaTable = new DataTable("SchemaTable"); _schemaTable.Locale = CultureInfo.InvariantCulture; AddDataColumn(_schemaTable, SchemaTableColumn.ColumnName, typeof(string)); AddDataColumn(_schemaTable, SchemaTableColumn.ColumnOrdinal, typeof(int)); AddDataColumn(_schemaTable, SchemaTableColumn.ColumnSize, typeof(int), -1); AddDataColumn(_schemaTable, SchemaTableColumn.NumericPrecision, typeof(short)); AddDataColumn(_schemaTable, SchemaTableColumn.NumericScale, typeof(short)); AddDataColumn(_schemaTable, SchemaTableColumn.DataType, typeof(Type)); AddDataColumn(_schemaTable, SchemaTableColumn.ProviderType, typeof(int)); AddDataColumn(_schemaTable, SchemaTableColumn.IsLong, typeof(bool), false); AddDataColumn(_schemaTable, SchemaTableColumn.AllowDBNull, typeof(bool), true); AddDataColumn(_schemaTable, SchemaTableColumn.IsUnique, typeof(bool), false); AddDataColumn(_schemaTable, SchemaTableColumn.IsKey, typeof(bool), false); AddDataColumn(_schemaTable, SchemaTableColumn.BaseSchemaName, typeof(string)); AddDataColumn(_schemaTable, SchemaTableColumn.BaseTableName, typeof(string), String.Empty); AddDataColumn(_schemaTable, SchemaTableColumn.BaseColumnName, typeof(string)); AddDataRow(_schemaTable, GeometryFieldName, GeometryFieldOrdinal, 1, typeof(byte[]), true, false, false); AddDataRow(_schemaTable, FidFieldName, FidFieldOrdinal, 1, typeof(long), false, true, true); for (int i = 0; i < _iFieldCount; i++) { FieldDefn f = _ogrFeatureDefinition.GetFieldDefn(i); AddDataRow(_schemaTable, f.GetName(), i + 2, f.GetWidth(), TranslateOgrType(f.GetFieldType()), true, false, false); } _schemaTable.AcceptChanges(); return(_schemaTable); }
public static string ReportLayer(Layer layer) { string strInfomation = ""; FeatureDefn def = layer.GetLayerDefn(); strInfomation += ("Layer name: " + def.GetName()); strInfomation += ("Feature Count: " + layer.GetFeatureCount(1).ToString()); Envelope ext = new Envelope(); layer.GetExtent(ext, 1); strInfomation += ("Extent: " + ext.MinX.ToString() + "," + ext.MaxX.ToString() + "," + ext.MinY.ToString() + "," + ext.MaxY.ToString()); /* -------------------------------------------------------------------- */ /* Reading the spatial reference */ /* -------------------------------------------------------------------- */ OSGeo.OSR.SpatialReference sr = layer.GetSpatialRef(); string srs_wkt; if (sr != null) { sr.ExportToPrettyWkt(out srs_wkt, 1); } else { srs_wkt = "(unknown)"; } strInfomation += ("Layer SRS WKT: " + srs_wkt); /* -------------------------------------------------------------------- */ /* Reading the fields */ /* -------------------------------------------------------------------- */ strInfomation += ("Field definition:"); for (int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++) { FieldDefn fdef = def.GetFieldDefn(iAttr); strInfomation += (fdef.GetNameRef() + ": " + fdef.GetFieldTypeName(fdef.GetFieldType()) + " (" + fdef.GetWidth().ToString() + "." + fdef.GetPrecision().ToString() + ")"); } /* -------------------------------------------------------------------- */ /* Reading the shapes */ /* -------------------------------------------------------------------- */ strInfomation += (""); Feature feat; while ((feat = layer.GetNextFeature()) != null) { strInfomation += ReportFeature(feat, def); feat.Dispose(); } return(strInfomation); }
public static MG_Field AsField(FieldDefn f) { MG_Field mgField = new MG_Field(); mgField.Name = f.GetNameRef(); //collect data mgField.Type = AsFieldDBType(f.GetFieldType()); //collect data mgField.Width = f.GetWidth(); //collect data mgField.Precision = f.GetPrecision(); //collect data return(mgField); }
public override void ReinitializeMetaData() { base.ReinitializeMetaData(); //get default output IDTSOutput100 defaultOutput = null; this.GetErrorOutputInfo(ref errorOutputID, ref errorOutputIndex); foreach (IDTSOutput100 output in ComponentMetaData.OutputCollection) { if (output.ID != errorOutputID) { defaultOutput = output; } } if (this.isConnected) { defaultOutput.OutputColumnCollection.RemoveAll(); defaultOutput.ExternalMetadataColumnCollection.RemoveAll(); //get ogrlayer and layer definition Layer OGRLayer; bool isSQLLayer = (!(ComponentMetaData.CustomPropertyCollection["SQL Statement"].Value == null || ComponentMetaData.CustomPropertyCollection["SQL Statement"].Value.ToString() == string.Empty)); if (isSQLLayer) { OGRLayer = getSQLLayer(); } else { OGRLayer = getLayer(); } FeatureDefn OGRFeatureDef = OGRLayer.GetLayerDefn(); //for each field in ogrlayer add output column and external metadata int i = 0; while (i < OGRFeatureDef.GetFieldCount()) { //map OGR field type to SSIS data type FieldDefn OGRFieldDef = OGRFeatureDef.GetFieldDefn(i); DataType BufferDataType = this.OGRTypeToBufferType(OGRFieldDef.GetFieldType()); int length = 0; int precision = OGRFieldDef.GetWidth(); int scale = OGRFieldDef.GetPrecision(); int codepage = 1252; switch (BufferDataType) { case DataType.DT_WSTR: length = precision; codepage = 0; precision = 0; scale = 0; //check for length == 0 if (length == 0) { //BufferDataType = DataType.DT_NTEXT; length = 4000; } break; default: length = 0; precision = 0; codepage = 0; scale = 0; break; } //create column metadata IDTSOutputColumn100 col = defaultOutput.OutputColumnCollection.New(); col.Name = OGRFieldDef.GetName(); col.ErrorRowDisposition = DTSRowDisposition.RD_FailComponent; col.TruncationRowDisposition = DTSRowDisposition.RD_FailComponent; col.SetDataTypeProperties(BufferDataType, length, precision, scale, codepage); //create external metadata IDTSExternalMetadataColumn100 ecol = defaultOutput.ExternalMetadataColumnCollection.New(); ecol.Name = col.Name; ecol.DataType = col.DataType; ecol.Precision = col.Precision; ecol.Length = col.Length; ecol.Scale = col.Scale; ecol.CodePage = col.CodePage; col.ExternalMetadataColumnID = ecol.ID; i++; } //get geometry column string geomtryColumn = (OGRLayer.GetGeometryColumn() != "") ? OGRLayer.GetGeometryColumn() : "GEOMETRY"; //add geom output column IDTSOutputColumn100 geomCol = defaultOutput.OutputColumnCollection.New(); geomCol.Name = geomtryColumn; geomCol.ErrorRowDisposition = DTSRowDisposition.RD_FailComponent; geomCol.TruncationRowDisposition = DTSRowDisposition.RD_FailComponent; geomCol.SetDataTypeProperties(DataType.DT_IMAGE, 0, 0, 0, 0); //add geom external metadata IDTSExternalMetadataColumn100 egeomCol = defaultOutput.ExternalMetadataColumnCollection.New(); egeomCol.Name = geomCol.Name; egeomCol.DataType = geomCol.DataType; egeomCol.Precision = geomCol.Precision; egeomCol.Length = geomCol.Length; egeomCol.Scale = geomCol.Scale; egeomCol.CodePage = geomCol.CodePage; //map column metadata to external column metadata geomCol.ExternalMetadataColumnID = egeomCol.ID; //set geometry column custom property ComponentMetaData.CustomPropertyCollection["Geometry Column"].Value = geomtryColumn; if (isSQLLayer) { this.OGRDataSource.ReleaseResultSet(OGRLayer); } } }
private void button2_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "打开ShapeFile数据"; dlg.Filter = "ShapeFile数据(*.shp)|*.shp"; if (dlg.ShowDialog() == DialogResult.OK) { Ogr.RegisterAll(); string strVectorFile = dlg.FileName; textBox1.Text = strVectorFile; //打开数据 DataSource ds = Ogr.Open(strVectorFile, 0); if (ds == null) { listBox1.Items.Add(string.Format("打开文件【{0}】失败!", strVectorFile)); return; } listBox1.Items.Add(string.Format("打开文件【{0}】成功!", strVectorFile)); // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个 int iLayerCount = ds.GetLayerCount(); // 获取第一个图层 Layer oLayer = ds.GetLayerByIndex(0); if (oLayer == null) { listBox1.Items.Add(string.Format("获取第{0}个图层失败!\n", 0)); return; } // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); // 通过属性表的SQL语句对图层中的要素进行筛选,这部分详细参考SQL查询章节内容 //oLayer.SetAttributeFilter("\"NAME99\"LIKE \"北京市市辖区\""); // 通过指定的几何对象对图层中的要素进行筛选 //oLayer.SetSpatialFilter(); // 通过指定的四至范围对图层中的要素进行筛选 //oLayer.SetSpatialFilterRect(); // 获取图层中的属性表表头并输出 listBox1.Items.Add("属性表结构信息:"); FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); listBox1.Items.Add(string.Format("{0}:{1} ({2}.{3})", oField.GetNameRef(), oField.GetFieldTypeName(oField.GetFieldType()), oField.GetWidth(), oField.GetPrecision())); } // 输出图层中的要素个数 listBox1.Items.Add(string.Format("要素个数 = {0}", oLayer.GetFeatureCount(0))); Feature oFeature = null; // 下面开始遍历图层中的要素 while ((oFeature = oLayer.GetNextFeature()) != null) { Geometry geo = oFeature.GetGeometryRef(); wkbGeometryType wkb = geo.GetGeometryType(); listBox1.Items.Add(string.Format("当前处理第要素值:{0}", wkb.ToString())); string strGml = geo.ExportToGML(); listBox1.Items.Add(strGml); listBox1.Items.Add(string.Format("当前处理第{0}个: \n属性值:", oFeature.GetFID())); // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); switch (type) { case FieldType.OFTString: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsString(iField))); break; case FieldType.OFTReal: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsDouble(iField))); break; case FieldType.OFTInteger: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsInteger(iField))); break; default: listBox1.Items.Add(string.Format("{0}\t", oFeature.GetFieldAsString(iField))); break; } } // 获取要素中的几何体 Geometry oGeometry = oFeature.GetGeometryRef(); // 为了演示,只输出一个要素信息 break; } listBox1.Items.Add("数据集关闭!"); } }
private static void ReportLayer(Layer layer) { //layer info string layerName = layer.GetName(); //Links string layerName2 = layer.GetLayerDefn().GetName(); //Links int fc = layer.GetFeatureCount(1); //16 Envelope ext = new Envelope(); layer.GetExtent(ext, 1); /* -------------------------------------------------------------------- */ /* Reading the spatial reference */ /* -------------------------------------------------------------------- */ OSGeo.OSR.SpatialReference sr = layer.GetSpatialRef(); string srs_wkt; if (sr != null) { sr.ExportToPrettyWkt(out srs_wkt, 1); } else { srs_wkt = "(unknown)"; } // feature definition FeatureDefn def = layer.GetLayerDefn(); //string layerName2 = def.GetName();//Links /* -------------------------------------------------------------------- */ /* Reading the fields */ /* -------------------------------------------------------------------- */ int nFieldCount = def.GetFieldCount();//3 for (int iField = 0; iField < nFieldCount; iField++) { // field definition FieldDefn fdef = def.GetFieldDefn(iField); // field info string fieldName = fdef.GetName(); //Id Name URL string fieldNameRef = fdef.GetNameRef(); // Id Name URL FieldType fieldType = fdef.GetFieldType(); //OFTInteger OFTString OFTString string fieldTypeName = fdef.GetFieldTypeName(fdef.GetFieldType()); //Integer String String int width = fdef.GetWidth(); //6 50 254 int precision = fdef.GetPrecision(); //0 0 0 } /* -------------------------------------------------------------------- */ /* Reading the shapes */ /* -------------------------------------------------------------------- */ for (int fid = 0; fid < layer.GetFeatureCount(1); fid++) { Feature f = layer.GetFeature(fid); ReportFeature(f, def); f.Dispose(); } //Feature f; //while ((f = layer.GetNextFeature()) != null) //{ // ReportFeature(f, def); // f.Dispose(); //} }
public static void Test2() { try { Ogr.RegisterAll(); string shapefile = @"C:\data\Links\Links.shp"; DataSource ds = Ogr.Open(shapefile, 0); Driver driver = ds.GetDriver(); int nLayerCount = ds.GetLayerCount();//1 for (int iLayer = 0; iLayer < nLayerCount; iLayer++) { Layer layer = ds.GetLayerByIndex(iLayer); string layerName = layer.GetName(); int fc = layer.GetFeatureCount(1); Envelope env = new Envelope(); layer.GetExtent(env, 1); //MessageBox.Show("test sr"); OSGeo.OSR.SpatialReference sr = layer.GetSpatialRef(); string sr_wkt; sr.ExportToPrettyWkt(out sr_wkt, 1); layer.GetName(); FeatureDefn def = layer.GetLayerDefn(); def.GetName(); for (int iField = 0; iField < def.GetFieldCount(); iField++) { FieldDefn fdef = def.GetFieldDefn(iField); string fieldName = fdef.GetName(); //Id Name URL FieldType fieldType = fdef.GetFieldType(); //OFTInteger OFTString OFTString string fieldTypeName = fdef.GetFieldTypeName(fdef.GetFieldType()); //Integer String String int width = fdef.GetWidth(); //6 50 254 int precision = fdef.GetPrecision(); //0 0 0 } for (int fid = 0; fid < layer.GetFeatureCount(1); fid++) { Feature f = layer.GetFeature(fid); int id = f.GetFID(); int nFiledCount = f.GetFieldCount(); Geometry geom = f.GetGeometryRef(); // retrive geometry data //this.Geometrys.Add(geom); string geomName = geom.GetGeometryName(); //POINT string geomType = geom.GetGeometryType().ToString(); //wkbPoint Envelope geom_env = new Envelope(); geom.GetEnvelope(geom_env); // wkt string geom_wkt; geom.ExportToWkt(out geom_wkt);//"POINT (-63.490966216299803 46.66247022944782)" int wkbSize = geom.WkbSize(); if (wkbSize > 0) { // wkb byte[] geom_wkb = new byte[wkbSize]; geom.ExportToWkb(geom_wkb); string str_wkb = BitConverter.ToString(geom_wkb); // wkb--->wkt Geometry geom2 = Geometry.CreateFromWkb(geom_wkb); string geom2_wkt; geom2.ExportToWkt(out geom2_wkt); } f.Dispose(); } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } }
public void opiszWarstwe() { FeatureDefn def = this.layer.GetLayerDefn(); Console.WriteLine("Nazwa warstwy: " + def.GetName()); Console.WriteLine("Liczba obiektów: " + this.layer.GetFeatureCount(1)); Envelope ext = new Envelope(); layer.GetExtent(ext, 1); Console.WriteLine("Zasiêg: " + ext.MinX + "," + ext.MaxX + "," + ext.MinY + "," + ext.MaxY); SpatialReference sr = this.layer.GetSpatialRef(); string srs_wkt = "(unknown)"; if (sr != null) { sr.ExportToPrettyWkt(out srs_wkt, 1); } Console.WriteLine("Uk³ad przestrzenny warstwy: " + srs_wkt); Console.WriteLine("Definicje atrybutów:"); for (int iAttr = 0; iAttr < def.GetFieldCount(); iAttr++) { FieldDefn fdef = def.GetFieldDefn(iAttr); Console.WriteLine(fdef.GetNameRef() + ": " + fdef.GetFieldTypeName(fdef.GetFieldType()) + " (" + fdef.GetWidth() + "." + fdef.GetPrecision() + ")"); } }
public void readLayerTest(string strVectorFile) { Gdal.AllRegister(); //为了支持中文路径,请添加下面这句代码 Gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); //为了使属性表字段支持中文,请添加下面这句 Gdal.SetConfigOption("SHAPE_ENCODING", ""); // 注册所有的驱动 Ogr.RegisterAll(); Gdal.SetConfigOption("GDAL_DATA", "E://lib//gdal//gdal1.9//data"); //打开数据 DataSource ds = Ogr.Open(strVectorFile, 0); if (ds == null) { Console.WriteLine("打开文件【{0}】失败!", strVectorFile); return; } Console.WriteLine("打开文件【{0}】成功!", strVectorFile); // 获取该数据源中的图层个数,一般shp数据图层只有一个,如果是mdb、dxf等图层就会有多个 int iLayerCount = ds.GetLayerCount(); // 获取第一个图层 Layer oLayer = ds.GetLayerByIndex(0); String c = ""; oLayer.GetSpatialRef().AutoIdentifyEPSG(); Console.WriteLine(oLayer.GetSpatialRef().AutoIdentifyEPSG()); OSGeo.OSR.SpatialReference spa = oLayer.GetSpatialRef(); // if (spa==null) // { spa = new OSGeo.OSR.SpatialReference(null); spa.ImportFromEPSG(3395); spa.ExportToWkt(out c); Console.WriteLine(c); return; // } // String a = ""; // spa.EPSGTreatsAsLatLong(); // Console.WriteLine(spa.ExportToWkt(out a)); // oLayer.GetSpatialRef().ExportToWkt(out a); // Console.WriteLine(spa.GetProjParm(out a)); // Console.WriteLine(oLayer.GetSpatialRef().GetLinearUnitsName()); if (oLayer == null) { Console.WriteLine("获取第{0}个图层失败!\n", 0); return; } // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); // 通过属性表的SQL语句对图层中的要素进行筛选,这部分详细参考SQL查询章节内容 // oLayer.SetAttributeFilter("\"NAME99\"LIKE \"北京市市辖区\""); // 通过指定的几何对象对图层中的要素进行筛选 //oLayer.SetSpatialFilter(); // 通过指定的四至范围对图层中的要素进行筛选 //oLayer.SetSpatialFilterRect(); // 获取图层中的属性表表头并输出 // Console.WriteLine("属性表结构信息:"); FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); Console.WriteLine("{0}:{1} ({2}.{3})", oField.GetNameRef(), oField.GetFieldTypeName(oField.GetFieldType()), oField.GetWidth(), oField.GetPrecision()); } // 输出图层中的要素个数 Console.WriteLine("要素个数 = {0}", oLayer.GetFeatureCount(0)); Feature oFeature = null; // 下面开始遍历图层中的要素 while ((oFeature = oLayer.GetNextFeature()) != null) { Console.WriteLine("当前处理第{0}个: \n属性值:", oFeature.GetFID()); // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); switch (type) { case FieldType.OFTString: Console.WriteLine("{0}\t", oFeature.GetFieldAsString(iField)); break; case FieldType.OFTReal: Console.WriteLine("{0}\t", oFeature.GetFieldAsDouble(iField)); break; case FieldType.OFTInteger: Console.WriteLine("{0}\t", oFeature.GetFieldAsInteger(iField)); break; default: Console.WriteLine("{0}\t", oFeature.GetFieldAsString(iField)); break; } } // 获取要素中的几何体 Geometry oGeometry = oFeature.GetGeometryRef(); // String a=oGeometry.GetGeometryName(); // String b = ""; // oGeometry.ExportToWkt(out b); Console.WriteLine(oGeometry.GetGeometryName()); // 为了演示,只输出一个要素信息 break; } Console.WriteLine("数据集关闭!"); Console.ReadLine(); }
public void readLine(ref Layer oLayer, string pointTableName) { // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); //打开数据 DataSource newDs = Ogr.Open(mSrcFile, 0); if (newDs == null) { Console.WriteLine(string.Format("打开文件【{0}】失败!\n", mSrcFile)); return; } //根据图层名称获取相应的图层 Layer matchLayer = newDs.GetLayerByName(pointTableName); Driver oDriver = null; DataSource oDS = null; Layer newLayer = null; initShp(ref oLayer, ref oDriver, ref oDS, ref newLayer, LAYER_TYPE.LINE); // 获取图层中的属性表表头并输出 string strInfo = "属性表结构信息:\n"; FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); FieldType type = oField.GetFieldType(); //为新图层创建属性 FieldDefn newField = new FieldDefn(oField.GetNameRef(), type); if (type == FieldType.OFTString) { newField.SetWidth(oField.GetWidth()); } newLayer.CreateField(newField, 1); strInfo += string.Format("{0}:{1} ({2}.{3})\n", oField.GetNameRef(), oField.GetFieldTypeName(oField.GetFieldType()), oField.GetWidth(), oField.GetPrecision()); } FeatureDefn newDefn = newLayer.GetLayerDefn(); // 输出图层中的要素个数 strInfo += string.Format("要素个数 = {0}\n", oLayer.GetFeatureCount(0)); Feature oFeature = null; // 下面开始遍历图层中的要素 double S_X = 0.0; double S_Y = 0.0; double E_X = 0.0; double E_Y = 0.0; while ((oFeature = oLayer.GetNextFeature()) != null) { strInfo += string.Format("\n当前处理第{0}个: \n属性值:", oFeature.GetFID()); //为新图层创建要素 Feature oFeatureLineString = new Feature(newDefn); string sql = string.Empty; // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); string name = oFieldDefn.GetNameRef(); FieldType type = oFieldDefn.GetFieldType(); switch (type) { case FieldType.OFTString: IntPtr pchar = OGR_F_GetFieldAsString(Feature.getCPtr(oFeature), iField); string val = Marshal.PtrToStringAnsi(pchar); oFeatureLineString.SetField(iField, val); switch (name) { case "S_Point": sql = oFeature.GetFieldAsString(iField); getCoordinate(ref matchLayer, ref sql, ref S_X, ref S_Y); break; case "E_Point": sql = oFeature.GetFieldAsString(iField); getCoordinate(ref matchLayer, ref sql, ref E_X, ref E_Y); break; default: break; } break; case FieldType.OFTReal: oFeatureLineString.SetField(name, oFeature.GetFieldAsDouble(iField)); break; case FieldType.OFTInteger: oFeatureLineString.SetField(iField, oFeature.GetFieldAsInteger(iField)); break; default: oFeatureLineString.SetField(iField, oFeature.GetFieldAsString(iField)); break; } } Geometry oGeometry = Geometry.CreateFromWkt(string.Format("LINESTRING({0} {1},{2} {3})", S_X, S_Y, E_X, E_Y)); oFeatureLineString.SetGeometryDirectly(oGeometry); newLayer.CreateFeature(oFeatureLineString); oGeometry.Dispose(); } strInfo += "\n数据集关闭!"; oDS.Dispose(); }
//----------------------------------------------------------------------------------- public void readPoint(ref Layer oLayer) { // 对图层进行初始化,如果对图层进行了过滤操作,执行这句后,之前的过滤全部清空 oLayer.ResetReading(); Driver oDriver = null; DataSource oDS = null; Layer newLayer = null; initShp(ref oLayer, ref oDriver, ref oDS, ref newLayer, LAYER_TYPE.POINT); // 获取图层中的属性表表头并输出 string strInfo = "属性表结构信息:\n"; FeatureDefn oDefn = oLayer.GetLayerDefn(); int iFieldCount = oDefn.GetFieldCount(); for (int iAttr = 0; iAttr < iFieldCount; iAttr++) { FieldDefn oField = oDefn.GetFieldDefn(iAttr); FieldType type = oField.GetFieldType(); string fieldName = oField.GetName(); //为新图层创建属性 if (!fieldName.Equals("X坐标") && !fieldName.Equals("Y坐标")) { FieldDefn newField = new FieldDefn(fieldName, type); if (type == FieldType.OFTString) { newField.SetWidth(oField.GetWidth()); } newLayer.CreateField(newField, 1); } //获取图层属性信息 strInfo += string.Format("{0}:{1} ({2}.{3})\n", oField.GetNameRef(), oField.GetFieldTypeName(oField.GetFieldType()), oField.GetWidth(), oField.GetPrecision()); } FeatureDefn newDefn = newLayer.GetLayerDefn(); // 输出图层中的要素个数 strInfo += string.Format("要素个数 = {0}\n", oLayer.GetFeatureCount(0)); Feature oFeature = null; // 下面开始遍历图层中的要素 while ((oFeature = oLayer.GetNextFeature()) != null) { strInfo += string.Format("\n当前处理第{0}个: \n属性值:", oFeature.GetFID()); //为新图层创建要素 Feature oFeaturePoint = new Feature(newDefn); double pointX = 0.0; double pointY = 0.0; // 获取要素中的属性表内容 for (int iField = 0; iField < iFieldCount; iField++) { FieldDefn oFieldDefn = oDefn.GetFieldDefn(iField); FieldType type = oFieldDefn.GetFieldType(); string name = oFieldDefn.GetNameRef(); switch (type) { case FieldType.OFTString: IntPtr pchar = OGR_F_GetFieldAsString(Feature.getCPtr(oFeature), iField); string val = Marshal.PtrToStringAnsi(pchar); oFeaturePoint.SetField(name, val); break; case FieldType.OFTReal: if (!name.Equals("X坐标") && !name.Equals("Y坐标")) { oFeaturePoint.SetField(name, oFeature.GetFieldAsDouble(iField)); } switch (name) { case "X": pointX = oFeature.GetFieldAsDouble(iField); break; case "Y": pointY = oFeature.GetFieldAsDouble(iField); break; default: break; } break; case FieldType.OFTInteger: oFeaturePoint.SetField(iField, oFeature.GetFieldAsInteger(iField)); break; default: oFeaturePoint.SetField(iField, oFeature.GetFieldAsString(iField)); break; } } //填充要素几何信息 Geometry oGeometry = Geometry.CreateFromWkt(string.Format("POINT({0} {1})", pointX, pointY)); oFeaturePoint.SetGeometry(oGeometry); newLayer.CreateFeature(oFeaturePoint); } strInfo += "\n数据集关闭!"; oDS.Dispose(); }
public override void ReinitializeMetaData() { base.ReinitializeMetaData(); IDTSInput100 input = ComponentMetaData.InputCollection[0]; if (!this.validExternalMetadata && this.isConnected) { input.ExternalMetadataColumnCollection.RemoveAll(); input.InputColumnCollection.RemoveAll(); this.validExternalMetadata = true; Layer OGRLayer = this.getLayer(); FeatureDefn OGRFeatureDef = OGRLayer.GetLayerDefn(); int i = 0; while (i < OGRFeatureDef.GetFieldCount()) { //map OGR field type to SSIS data type FieldDefn OGRFieldDef = OGRFeatureDef.GetFieldDefn(i); DataType BufferDataType = this.OGRTypeToBufferType(OGRFieldDef.GetFieldType()); int length = 0; int precision = OGRFieldDef.GetWidth(); int scale = OGRFieldDef.GetPrecision(); int codepage = 1252; switch (BufferDataType) { case DataType.DT_WSTR: length = precision; codepage = 0; precision = 0; scale = 0; //check for length == 0 if (length == 0) { BufferDataType = DataType.DT_NTEXT; } break; default: length = 0; precision = 0; codepage = 0; scale = 0; break; } IDTSExternalMetadataColumn100 ecol = input.ExternalMetadataColumnCollection.New(); ecol.Name = OGRFieldDef.GetName(); ecol.DataType = BufferDataType; ecol.Length = length; ecol.Precision = precision; ecol.Scale = scale; ecol.CodePage = codepage; i++; } //get geometry column string geomtryColumn = (OGRLayer.GetGeometryColumn() != "") ? OGRLayer.GetGeometryColumn() : "GEOMETRY"; //Set OGRGeometry external metadata column IDTSExternalMetadataColumn100 egeomCol = input.ExternalMetadataColumnCollection.New(); egeomCol.Name = geomtryColumn; egeomCol.DataType = DataType.DT_IMAGE; egeomCol.Precision = 0; egeomCol.Length = 0; egeomCol.Scale = 0; egeomCol.CodePage = 0; //set geometry column custom property ComponentMetaData.CustomPropertyCollection["Geometry Column"].Value = geomtryColumn; } if (!ComponentMetaData.AreInputColumnsValid) { ComponentMetaData.RemoveInvalidInputColumns(); } }