public override bool isExternalMetadataValid() { IDTSExternalMetadataColumnCollection100 exColumns = ComponentMetaData.InputCollection[0].ExternalMetadataColumnCollection; Layer OGRLayer = this.getLayer(); FeatureDefn OGRFeatureDef = OGRLayer.GetLayerDefn(); // Get Geometry column name string geomtryColumn = (OGRLayer.GetGeometryColumn() != "") ? OGRLayer.GetGeometryColumn() : "GEOMETRY"; //check for correct number of external metadata columns if (OGRFeatureDef.GetFieldCount() + 1 != exColumns.Count) { return(false); } //validate each external metadata column for (int i = 0; i < exColumns.Count; i++) { IDTSExternalMetadataColumn100 col = exColumns[i]; if (col.Name == geomtryColumn) { if (col.DataType != DataType.DT_IMAGE) { return(false); } // Check geometry column custom property against source if ((string)ComponentMetaData.CustomPropertyCollection["Geometry Column"].Value != geomtryColumn) { return(false); } } else { //check if ogr field exists by name int OGRFieldIndex = OGRFeatureDef.GetFieldIndex(col.Name); if (OGRFieldIndex == -1) { return(false); } //check if ogr column matches output column type FieldDefn OGRFieldDef = OGRFeatureDef.GetFieldDefn(OGRFieldIndex); FieldType OGRFieldType = OGRFieldDef.GetFieldType(); if (this.OGRTypeToBufferType(OGRFieldType) != col.DataType) { //check for case where OFTString -> DT_NTEXT if (!(OGRFieldType == FieldType.OFTString && col.DataType == DataType.DT_NTEXT)) { return(false); } } } } return(true); }
/// <summary> /// 将项目坐标信息写入指定图层中 /// (要求图层依次包含以下字段:界址点数,地块面积,地块编号,地块名称,几何类型,图幅号,地块用途,地类编码) /// </summary> /// <param name="tProjInfo"></param> /// <param name="layer"></param> public static void ToLayer(TProjInfo tProjInfo, Layer layer) { FeatureDefn featureDefn = layer.GetLayerDefn(); //创建XMID字段,写入guid以区分项目 var guid = Guid.NewGuid().ToString(); const string strXmid = "XMID"; if (featureDefn.GetFieldIndex(strXmid) < 0) { layer.CreateField(new FieldDefn(strXmid, FieldType.OFTString), 1); } //创建Feature foreach (TPolygon polygon in tProjInfo.Polygons) { Feature feature = new Feature(featureDefn); for (int i = 0; i < polygon.AttributeValues.Count; i++) { feature.SetField(i, polygon.AttributeValues[i]); } feature.SetField(strXmid, guid); bool first = true; StringBuilder polygonWkt = new StringBuilder("POLYGON("); foreach (TRing ring in polygon.Rings) { StringBuilder sbRingWkt = new StringBuilder((first ? "" : ",") + "("); first = false; bool ringFirst = true; foreach (double[] doubles in ring.XyPair) { sbRingWkt.Append((ringFirst ? "" : ",") + doubles[1] + " " + doubles[0]); ringFirst = false; } sbRingWkt.Append(")"); polygonWkt.Append(sbRingWkt); } polygonWkt.Append(")"); Geometry geometry = Geometry.CreateFromWkt(polygonWkt.ToString()); feature.SetGeometry(geometry); layer.CreateFeature(feature); } layer.SyncToDisk(); }
//Checks if output metadata columns match ogrlayer source columns public override bool isMetadataValid() { //get default output IDTSOutput100 output = null; for (int i = 0; i < ComponentMetaData.OutputCollection.Count; i++) { if (!ComponentMetaData.OutputCollection[i].IsErrorOut) { output = ComponentMetaData.OutputCollection[i]; } } //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(); // Get Geometry column name string geomtryColumn = (OGRLayer.GetGeometryColumn() != "") ? OGRLayer.GetGeometryColumn() : "GEOMETRY"; // Begin checks bool isValid = true; if (OGRFeatureDef.GetFieldCount() + 1 == output.OutputColumnCollection.Count) { int i = 0; do { //check if ogr field exists by name IDTSOutputColumn100 col = output.OutputColumnCollection[i]; int OGRFieldIndex = OGRFeatureDef.GetFieldIndex(col.Name); if (OGRFieldIndex == -1) { //set isValid false if not geom column if ((col.Name != geomtryColumn) || (col.DataType != DataType.DT_IMAGE) || ((string)ComponentMetaData.CustomPropertyCollection["Geometry Column"].Value != geomtryColumn)) { isValid = false; } } else { //is isVaild to false if field types don't match FieldDefn OGRFieldDef = OGRFeatureDef.GetFieldDefn(OGRFieldIndex); FieldType OGRFieldType = OGRFieldDef.GetFieldType(); if ((col.DataType != this.OGRTypeToBufferType(OGRFieldType)) && (!(OGRFieldType == FieldType.OFTString && col.DataType == DataType.DT_NTEXT))) // special case where lenth not provided by OFTString { isValid = false; } } i++; } while (isValid && i < output.OutputColumnCollection.Count); } else { isValid = false; } //Manualy call release on OGR layer sql result set if needed if (isSQLLayer) { this.OGRDataSource.ReleaseResultSet(OGRLayer); } return(isValid); }
public override void PrimeOutput(int outputs, int[] outputIDs, PipelineBuffer[] buffers) { //identify buffers PipelineBuffer errorBuffer = null; PipelineBuffer defaultBuffer = null; for (int x = 0; x < outputs; x++) { if (outputIDs[x] == errorOutputID) { errorBuffer = buffers[x]; } else { defaultBuffer = buffers[x]; } } //get ogrlayer and ogrlayer feature 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(); } Feature OGRFeature; FeatureDefn OGRFeatureDef = OGRLayer.GetLayerDefn(); //initialize columnInfo object columnInfo ci = new columnInfo(); //for each row in ogrlayer add row to output buffer while ((OGRFeature = OGRLayer.GetNextFeature()) != null) { try { defaultBuffer.AddRow(); //set buffer column values for (int i = 0; i < this.columnInformation.Count; i++) { ci = (columnInfo)this.columnInformation[i]; if (ci.geom) { Geometry geom = OGRFeature.GetGeometryRef(); if (geom != null) { byte[] geomBytes = new byte[geom.WkbSize()]; geom.ExportToWkb(geomBytes); defaultBuffer.AddBlobData(ci.bufferColumnIndex, geomBytes); } } else { int OGRFieldIndex = OGRFeatureDef.GetFieldIndex(ci.columnName); FieldDefn OGRFieldDef = OGRFeatureDef.GetFieldDefn(OGRFieldIndex); FieldType OGRFieldType = OGRFieldDef.GetFieldType(); //declare datetime variables int pnYear, pnMonth, pnDay, pnHour, pnMinute, pnTZFlag; float pnSecond; DateTime dt; TimeSpan ts; switch (OGRFieldType) { //case FieldType.OFTBinary: // break; case FieldType.OFTDate: OGRFeature.GetFieldAsDateTime(OGRFieldIndex, out pnYear, out pnMonth, out pnDay, out pnHour, out pnMinute, out pnSecond, out pnTZFlag); dt = new DateTime(pnYear, pnMonth, pnDay); defaultBuffer.SetDate(ci.bufferColumnIndex, dt); break; case FieldType.OFTDateTime: OGRFeature.GetFieldAsDateTime(OGRFieldIndex, out pnYear, out pnMonth, out pnDay, out pnHour, out pnMinute, out pnSecond, out pnTZFlag); dt = new DateTime(pnYear, pnMonth, pnDay, pnHour, pnMinute, (int)pnSecond); //set time zone? defaultBuffer.SetDateTime(ci.bufferColumnIndex, dt); break; case FieldType.OFTInteger: defaultBuffer.SetInt32(ci.bufferColumnIndex, OGRFeature.GetFieldAsInteger(OGRFieldIndex)); break; case FieldType.OFTInteger64: defaultBuffer.SetInt64(ci.bufferColumnIndex, OGRFeature.GetFieldAsInteger(OGRFieldIndex)); break; case FieldType.OFTReal: defaultBuffer.SetDouble(ci.bufferColumnIndex, OGRFeature.GetFieldAsDouble(OGRFieldIndex)); break; case FieldType.OFTTime: OGRFeature.GetFieldAsDateTime(OGRFieldIndex, out pnYear, out pnMonth, out pnDay, out pnHour, out pnMinute, out pnSecond, out pnTZFlag); ts = new TimeSpan(pnHour, pnMinute, (int)pnSecond); defaultBuffer.SetTime(ci.bufferColumnIndex, ts); break; case FieldType.OFTString: default: defaultBuffer.SetString(ci.bufferColumnIndex, OGRFeature.GetFieldAsString(OGRFieldIndex)); break; } } } } catch (Exception ex) { //redirect to error buffer if (ci.errorDisposition == DTSRowDisposition.RD_RedirectRow) { // Add a row to the error buffer. errorBuffer.AddRow(); // Set the error information. int errorCode = System.Runtime.InteropServices.Marshal.GetHRForException(ex); errorBuffer.SetErrorInfo(errorOutputID, errorCode, ci.lineageID); // Remove the row that was added to the default buffer. defaultBuffer.RemoveRow(); } //fail component else if (ci.errorDisposition == DTSRowDisposition.RD_FailComponent || ci.errorDisposition == DTSRowDisposition.RD_NotUsed) { ComponentMetaData.FireError(0, "primeoutput failure", ex.ToString(), string.Empty, 0, out cancel); throw; } } } //set end of rowset for buffers if (defaultBuffer != null) { defaultBuffer.SetEndOfRowset(); } if (errorBuffer != null) { errorBuffer.SetEndOfRowset(); } //clean up layer object if (isSQLLayer) { this.OGRDataSource.ReleaseResultSet(OGRLayer); } }
/* -------------------------------------------------------------------- */ /* Reading the Fields in this Feature */ /* -------------------------------------------------------------------- */ public static void CreateTreeFields(ref TreeNode thisFeatureTree, Feature feature, FeatureDefn featureDefn) { // field definition as we iterate through the fields FieldDefn iFieldDefn; int nameIndex; int objlIndex; string nameString; string objlString; // To get the name and the S-57 Type code (OBJL), we have to look forward into the fields. This piece of code // is mostly here to show how that is done as will need this when rendering these objects. You could easily add // shapes or color, but it is not necessary here. The name and the code is nice for the debug file though so we can // see what each feature is. // OBJL field should always exist - the exception are the meta data fields such as DSID which has no code. objlIndex = featureDefn.GetFieldIndex("OBJL"); if (objlIndex > 0) { objlString = DecodeField(feature, featureDefn.GetFieldDefn(objlIndex), objlIndex); } else { objlString = "No Code"; } // name field may not exist. we could get either the info or text description if they exist nameIndex = featureDefn.GetFieldIndex("OBJNAM"); if (nameIndex > 0) { nameString = DecodeField(feature, featureDefn.GetFieldDefn(nameIndex), nameIndex); } else { nameString = "Not Named"; } DebugUtil.WriteLine(); DebugUtil.WriteLine(string.Format(" *** Feature: {0}, Name: {1}, S-57 Code: {2}", featureDefn.GetName(), nameString, objlString)); DebugUtil.WriteLine(); // get the styles, if any if (feature.GetStyleString() != null) { DebugUtil.WriteLine(" Style = " + feature.GetStyleString()); } // this gets the sub-geometries. not all features have sub-geometries Geometry geom = feature.GetGeometryRef(); if (geom != null) { DebugUtil.WriteLine(" Geometry Name: " + geom.GetGeometryName() + " Sub-geometry Count: " + geom.GetGeometryCount()); Geometry sub_geom; for (int i = 0; i < geom.GetGeometryCount(); i++) { sub_geom = geom.GetGeometryRef(i); if (sub_geom != null) { string subGeoString; DebugUtil.Write(" sub-geometry " + i + ": "); switch (sub_geom.GetGeometryType()) { case wkbGeometryType.wkbLineString: case wkbGeometryType.wkbPoint25D: sub_geom.ExportToWkt(out subGeoString); DebugUtil.WriteLine(subGeoString); TreeUtil.AddChildNode(thisFeatureTree, subGeoString); break; default: DebugUtil.WriteLine("Unhandled subgeometry type: " + sub_geom.GetGeometryType()); TreeUtil.AddChildNode(thisFeatureTree, "Unhandled sub-geomtry type", sub_geom.GetGeometryType().ToString()); break; } } } // a bit confusing, but if it has no sub geometries, use the base geometry. mostly applies to the type POINT if (geom.GetGeometryCount() == 0) { string geom_wkt; geom.ExportToWkt(out geom_wkt); DebugUtil.WriteLine(" geom_wkt: " + geom_wkt); TreeUtil.AddChildNode(thisFeatureTree, geom_wkt); } } // if geo != null for (int iField = 0; iField < feature.GetFieldCount(); iField++) { iFieldDefn = featureDefn.GetFieldDefn(iField); // not all fields have usable information, skip those that don't. if (feature.IsFieldSet(iField)) // IsFieldSet is a handy way to see if the field has any actual information { DebugUtil.Write(" "); DebugUtil.Write("Field Name: " + iFieldDefn.GetName() + ", "); DebugUtil.Write("Type Name: " + iFieldDefn.GetFieldTypeName(iFieldDefn.GetFieldType()) + ", "); DebugUtil.Write("Field Type: " + iFieldDefn.GetFieldType() + ", "); DebugUtil.Write("Value: " + DecodeField(feature, iFieldDefn, iField)); DebugUtil.WriteLine(); } // if isField } }
public override void ProcessInput(int inputID, PipelineBuffer buffer) { Layer OGRLayer = this.getLayer(); FeatureDefn OGRFeatureDef = OGRLayer.GetLayerDefn(); int batchCount = 0; OGRLayer.StartTransaction(); //initialize columnInfo object columnInfo ci = new columnInfo(); while (buffer.NextRow()) { try { //Start transaction if (this.batchSize != 0 && batchCount % this.batchSize == 0) { OGRLayer.CommitTransaction(); OGRLayer.StartTransaction(); batchCount = 0; } Feature OGRFeature = new Feature(OGRFeatureDef); for (int i = 0; i < this.columnInformation.Count; i++) { ci = this.columnInformation[i]; if (!buffer.IsNull(ci.bufferColumnIndex)) { if (ci.geom) { byte[] geomBytes = new byte[buffer.GetBlobLength(ci.bufferColumnIndex)]; geomBytes = buffer.GetBlobData(ci.bufferColumnIndex, 0, geomBytes.Length); Geometry geom = Geometry.CreateFromWkb(geomBytes); OGRFeature.SetGeometry(geom); } else { int OGRFieldIndex = OGRFeatureDef.GetFieldIndex(ci.columnName); FieldDefn OGRFieldDef = OGRFeatureDef.GetFieldDefn(OGRFieldIndex); FieldType OGRFieldType = OGRFieldDef.GetFieldType(); //declare datetime variables DateTime dt; TimeSpan ts; switch (OGRFieldType) { //case FieldType.OFTBinary: // break; case FieldType.OFTDate: dt = buffer.GetDate(ci.bufferColumnIndex); OGRFeature.SetField(OGRFieldIndex, dt.Year, dt.Month, dt.Day, 0, 0, 0, 0); break; case FieldType.OFTDateTime: dt = buffer.GetDateTime(ci.bufferColumnIndex); //get timezone? OGRFeature.SetField(OGRFieldIndex, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0); break; case FieldType.OFTInteger: OGRFeature.SetField(OGRFieldIndex, buffer.GetInt32(ci.bufferColumnIndex)); break; case FieldType.OFTInteger64: OGRFeature.SetField(OGRFieldIndex, buffer.GetInt64(ci.bufferColumnIndex)); break; case FieldType.OFTReal: OGRFeature.SetField(OGRFieldIndex, buffer.GetDouble(ci.bufferColumnIndex)); break; case FieldType.OFTTime: ts = buffer.GetTime(ci.bufferColumnIndex); OGRFeature.SetField(OGRFieldIndex, 0, 0, 0, ts.Hours, ts.Minutes, ts.Seconds, 0); break; case FieldType.OFTString: default: OGRFeature.SetField(OGRFieldIndex, buffer.GetString(ci.bufferColumnIndex)); break; } } } } OGRLayer.CreateFeature(OGRFeature); batchCount++; //increment incrementPipelinePerfCounters to display correct # of rows written ComponentMetaData.IncrementPipelinePerfCounter(103, 1); } catch (Exception ex) { //Redirect row IDTSInputColumn100 inputColumn = ComponentMetaData.InputCollection[0].InputColumnCollection.GetInputColumnByLineageID(ci.lineageID); IDTSOutput100 output = ComponentMetaData.OutputCollection[0]; if (ci.errorDisposition == DTSRowDisposition.RD_RedirectRow) { int errorCode = System.Runtime.InteropServices.Marshal.GetHRForException(ex); buffer.DirectErrorRow(output.ID, errorCode, inputColumn.LineageID); } else if (ci.errorDisposition == DTSRowDisposition.RD_FailComponent || ci.errorDisposition == DTSRowDisposition.RD_NotUsed) { OGRLayer.RollbackTransaction(); ComponentMetaData.FireError(0, ComponentMetaData.Name, ex.Message, string.Empty, 0, out cancel); throw new Exception(ex.Message); } } } OGRLayer.CommitTransaction(); }