/// <summary> /// /// </summary> /// <param name="pFrm"></param> /// <param name="pSrcFileGDB"></param> /// <param name="pSQLFileName"></param> /// <param name="pBatchSize"></param> /// <param name="pAppend"></param> /// <returns></returns> public static ReturnValue ExportFileGDBToMyAbuDhabiDotNetSQL(frmMain pFrm, string pSrcFileGDB, string pSQLFileName, int pBatchSize = 25, bool pAppend = false) { var mRetVal = new ReturnValue(true); NumberFormatInfo mNumFormat = new CultureInfo("en-US", false).NumberFormat; // Open FileGDB var mFileGDBDrv = OSGeo.OGR.Ogr.GetDriverByName("FileGDB"); var mSrcDSrc = mFileGDBDrv.Open(pSrcFileGDB, 1); if (mSrcDSrc == null) { pFrm.Log("Could not open ESRI FGDB: " + pSrcFileGDB, true); mRetVal.Success = false; return(mRetVal); } // Create re-usable projection var mSrcProj = ExtFunctions.GetSpatialReferenceByEPSG(32640); var mTgtProj = ExtFunctions.GetSpatialReferenceByEPSG(4326); var mTransformation = new OSGeo.OSR.CoordinateTransformation(mSrcProj, mTgtProj); int mLineCount = 0; int mFileCount = 1; // Create file to write to var mStreamWriter = new StreamWriter(pSQLFileName + "." + mFileCount, pAppend, Encoding.UTF8, 1024); // Create re-usable feature object OSGeo.OGR.Feature mFeat = null; OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8"); DataSource mDistrictsDataSource = Ogr.Open(DistrictImport.GetShapefileName(), 0); Layer mDistricts = null; if (mDistrictsDataSource != null) { mDistricts = mDistrictsDataSource.GetLayerByIndex(0); } var mRecords = new SignRecord(); // Process address unit signs var mAUSLyr = mSrcDSrc.GetLayerByName("Address_unit_signs"); if (mAUSLyr == null) { mRetVal.AddMessage("Could not find address units layer", true); return(mRetVal); } if (mAUSLyr != null) { pFrm.Log("Opened Address_unit_signs layer from FileGDB for reading..."); int mNumFeatures = mAUSLyr.GetFeatureCount(1); int idxFeat = 0; while (null != (mFeat = mAUSLyr.GetNextFeature())) { var mGeom = mFeat.GetGeometryRef(); Envelope mOrigEnvelope = new Envelope(); mGeom.GetEnvelope(mOrigEnvelope); var mDistrictInfo = mDistricts.GetDistrictByPoint(mGeom, 32640); mGeom.Transform(mTransformation); Envelope mEnvelope = new Envelope(); mGeom.GetEnvelope(mEnvelope); string mWkt; mGeom.ExportToWkt(out mWkt); mRecords.AddToBuffer( mFeat.GetFieldAsString("QR_CODE"), mFeat.GetFieldAsString("ADDRESSUNITNR"), mFeat.GetFieldAsString("ROADNAME_EN").MySQLEscape(), mFeat.GetFieldAsString("ROADNAME_AR").MySQLEscape(), mDistrictInfo != null ? mDistrictInfo.GetFieldAsString("NAMELATIN").MySQLEscape() : "", mDistrictInfo != null ? mDistrictInfo.GetFieldAsString("NAMEARABIC").MySQLEscape() : "", mEnvelope.MinX.ToString(mNumFormat), mEnvelope.MinY.ToString(mNumFormat), mFeat.GetFieldAsString("DESCRIPTION_AR").MySQLEscape(), mFeat.GetFieldAsString("DESCRIPTION_EN").MySQLEscape(), "Address_unit_signs", SignType.addressUnitNumberSign ); idxFeat++; if (idxFeat % pBatchSize == 0 || idxFeat == mNumFeatures) { var mSql = String.Format(sqlStatements.insertUpdateMyAbuDhabiNetSQL, mRecords.GetBuffer()); mStreamWriter.WriteLine(mSql); pFrm.pgBar.ProgressBar.Value = (idxFeat * 100) / mNumFeatures; mLineCount++; if (mLineCount % 500 == 0) { mFileCount++; mStreamWriter.Flush(); mStreamWriter.Close(); mStreamWriter = new StreamWriter(pSQLFileName + "." + mFileCount, pAppend, Encoding.UTF8, 1024); mLineCount = 0; } } } pFrm.Log("Done, processed " + idxFeat + " features..."); mAUSLyr.Dispose(); mAUSLyr = null; } // Process address unit signs var mSNSLyr = mSrcDSrc.GetLayerByName("Street_name_signs"); if (mSNSLyr == null) { mRetVal.AddMessage("Could not find layer Street_name_signs", true); return(mRetVal); } if (mSNSLyr != null) { Debug.WriteLine(mSNSLyr); pFrm.Log("Opened Street_name_signs layer from FileGDB for reading..."); int mNumFeatures = mSNSLyr.GetFeatureCount(1); int idxFeat = 0; mFeat = null; while (null != (mFeat = mSNSLyr.GetNextFeature())) { var mGeom = mFeat.GetGeometryRef(); Envelope mOrigEnvelope = new Envelope(); mGeom.GetEnvelope(mOrigEnvelope); var mDistrictInfo = mDistricts.GetDistrictByPoint(mGeom, 32640); mGeom.Transform(mTransformation); Envelope mEnvelope = new Envelope(); mGeom.GetEnvelope(mEnvelope); string mWkt; mGeom.ExportToWkt(out mWkt); mRecords.AddToBuffer( mFeat.GetFieldAsString("QR_CODE"), "", mFeat.GetFieldAsString("ROADNAME_EN_P1").MySQLEscape() + "/" + mFeat.GetFieldAsString("ROADNAME_EN_P2").MySQLEscape(), mFeat.GetFieldAsString("ROADNAME_AR_P1").MySQLEscape() + "/" + mFeat.GetFieldAsString("ROADNAME_AR_P2").MySQLEscape(), mDistrictInfo != null ? mDistrictInfo.GetFieldAsString("NAMELATIN").MySQLEscape() : "", mDistrictInfo != null ? mDistrictInfo.GetFieldAsString("NAMEARABIC").MySQLEscape() : "", mEnvelope.MinX.ToString(mNumFormat), mEnvelope.MinY.ToString(mNumFormat), "", "", "Street_name_signs", SignType.streetNameSign, "" ); idxFeat++; if (idxFeat % pBatchSize == 0 || idxFeat == mNumFeatures) { var mSql = String.Format(sqlStatements.insertUpdateMyAbuDhabiNetSQL, mRecords.GetBuffer()); mStreamWriter.WriteLine(mSql); Debug.WriteLine(mSql); pFrm.pgBar.ProgressBar.Value = (idxFeat * 100) / mNumFeatures; mLineCount++; if (mLineCount % 500 == 0) { mFileCount++; mStreamWriter.Flush(); mStreamWriter.Close(); mStreamWriter = new StreamWriter(pSQLFileName + "." + mFileCount, pAppend, Encoding.UTF8, 1024); mLineCount = 0; } } } pFrm.Log("Done, processed " + idxFeat + " features..."); mSNSLyr.Dispose(); mSNSLyr = null; } // Process address guide signs var mAGSLyr = mSrcDSrc.GetLayerByName("Address_guide_sign"); if (mAGSLyr == null) { mRetVal.AddMessage("Could not find layer Address_guide_sign", true); return(mRetVal); } if (mAGSLyr != null) { pFrm.Log("Opened Address_guide_sign layer from FileGDB for reading..."); int mNumFeatures = mAGSLyr.GetFeatureCount(1); int idxFeat = 0; mFeat = null; while (null != (mFeat = mAGSLyr.GetNextFeature())) { var mGeom = mFeat.GetGeometryRef(); Envelope mOrigEnvelope = new Envelope(); mGeom.GetEnvelope(mOrigEnvelope); var mDistrictInfo = mDistricts.GetDistrictByPoint(mGeom, 32640); mGeom.Transform(mTransformation); Envelope mEnvelope = new Envelope(); mGeom.GetEnvelope(mEnvelope); string mWkt; mGeom.ExportToWkt(out mWkt); mRecords.AddToBuffer( mFeat.GetFieldAsString("QR_CODE"), "", mFeat.GetFieldAsString("ROADNAME_EN"), mFeat.GetFieldAsString("ROADNAME_AR"), mDistrictInfo != null ? mDistrictInfo.GetFieldAsString("NAMELATIN").MySQLEscape() : null, mDistrictInfo != null ? mDistrictInfo.GetFieldAsString("NAMEARABIC").MySQLEscape() : null, mEnvelope.MinX.ToString(mNumFormat), mEnvelope.MinY.ToString(mNumFormat), "", "", "Address_guide_sign", SignType.addressGuideSign, ""); idxFeat++; if (idxFeat % pBatchSize == 0 || idxFeat == mNumFeatures) { var mSql = String.Format(sqlStatements.insertUpdateMyAbuDhabiNetSQL, mRecords.GetBuffer()); mStreamWriter.WriteLine(mSql); pFrm.pgBar.ProgressBar.Value = (idxFeat * 100) / mNumFeatures; mLineCount++; if (mLineCount % 500 == 0) { mFileCount++; mStreamWriter.Flush(); mStreamWriter.Close(); mStreamWriter = new StreamWriter(pSQLFileName + "." + mFileCount, pAppend, Encoding.UTF8, 1024); mLineCount = 0; } } } pFrm.Log("Done, processed " + idxFeat + " features..."); mAGSLyr.Dispose(); mAGSLyr = null; } mStreamWriter.Flush(); mStreamWriter.Close(); mStreamWriter.Dispose(); mSrcProj.Dispose(); mSrcProj = null; mTgtProj.Dispose(); mTgtProj = null; mTransformation.Dispose(); mTransformation = null; mSrcDSrc.Dispose(); mSrcDSrc = null; mFileGDBDrv.Dispose(); mFileGDBDrv = null; return(mRetVal); }
public static ReturnValue ExportToMyAbuDhabiNet(string pSQLFilename, IPointLayer pLayer, ProjectionInfo pSrcProjection, ProjectionInfo pTgtProjection, Boolean pAppend = false, string pSignType = null) { NumberFormatInfo mNumFormat = new CultureInfo("en-US", false).NumberFormat; double[] mXY = new double[2]; var mRV = new ReturnValue(); var mTgtSRS = ExtFunctions.GetSpatialReferenceByEPSG(pTgtProjection.AuthorityCode); if (pSrcProjection != pTgtProjection) { pLayer.Reproject(pTgtProjection); } int mFeatureCount = pLayer.DataSet.Features.Count(), mBatchSize = 20, mBatchCounter = 1, mWriteCounter = 0; var mValues = new List <String>(); var mStreamWriter = new StreamWriter(pSQLFilename, pAppend, Encoding.UTF8, 1024); foreach (IFeature mFeature in pLayer.DataSet.Features) { var mGeom = OSGeo.OGR.Geometry.CreateFromWkb(mFeature.ToBinary()); Envelope mEnvelope = new Envelope(); mGeom.GetEnvelope(mEnvelope); string mWkt; mGeom.ExportToWkt(out mWkt); if (pSignType == SignType.addressUnitNumberSign) { mValues.Add(String.Format("('{0}','{1}','{2}','{3}',{4},'{5}','{6}','{7}','{8}','{9}','{10}',{11},{12})", mFeature.DataRow["QR_CODE"], "ANS", "Sign description", SignType.addressUnitNumberSign, mFeature.DataRow["ADDRESSUNITNR"], "Street name description", mFeature.DataRow["ROADNAME_EN"].ToString().MySQLEscape(), "Street name description Arabic", mFeature.DataRow["ROADNAME_AR"].ToString().MySQLEscape(), mFeature.DataRow["DISTRICT_EN"].ToString().MySQLEscape(), mFeature.DataRow["DISTRICT_AR"].ToString().MySQLEscape(), mEnvelope.MinX.ToString(mNumFormat), mEnvelope.MinY.ToString(mNumFormat) )); mWriteCounter++; } else if (pSignType == SignType.streetNameSign) { mValues.Add(String.Format("('{0}','{1}','{2}','{3}',{4},'{5}','{6}','{7}','{8}','{9}','{10}',{11},{12})", mFeature.DataRow["QR_CODE"], "SNS", "Sign description", SignType.streetNameSign, "null", "", "", "", "", "", "", mEnvelope.MinX.ToString(mNumFormat), mEnvelope.MinY.ToString(mNumFormat) )); mWriteCounter++; } else if (pSignType == SignType.addressGuideSign) { mValues.Add(String.Format("('{0}','{1}','{2}','{3}',{4},'{5}','{6}','{7}','{8}','{9}','{10}',{11},{12})", mFeature.DataRow["QR_CODE"], "AGS", "Sign description", SignType.addressGuideSign, "null", "", "", "", "", "", "", mEnvelope.MinX.ToString(mNumFormat), mEnvelope.MinY.ToString(mNumFormat) )); mWriteCounter++; } if (mBatchCounter++ < mBatchSize) { mBatchCounter++; } else { var mSql = String.Format(sqlStatements.insertUpdateMyAbuDhabiNetSQL, String.Join(",", mValues)); mBatchCounter = 1; mValues.Clear(); mStreamWriter.WriteLine(mSql); } } mRV.AddMessage("Parsed " + mWriteCounter + " features to SQL"); mStreamWriter.Flush(); mStreamWriter.Close(); return(mRV); }
/// <summary> /// Outputs the specified DotSpatial IFeatureLayer to an OGR layer /// </summary> /// <param name="pDrvNm">A name like KML, ESRI SHapefile or FileGDB etc.</param> /// <param name="pFLyr">A layer that implements the IFeatureLayer interface</param> /// <param name="pOPFn">The outputShapefileName of the output file</param> /// <param name="pSrcProj">The SRS of the source dataset</param> /// <param name="pTgtProj">The SRS of the output dataset</param> /// <param name="pHasTitle">A boolean flag that determines whether to create fieldAttributes special title field</param> /// <param name="pTitleFieldNames">A comma separated list of field names to be used in the special title field</param> /// <param name="pTitleFormat">The C# String.Format format of the special title field</param> /// <param name="pLCOpts">A string array of layer creation options in the form of OPTION=VALUE entries</param> /// <param name="pDSCOpts">A string of data source creation options in the form of OPTION=VALUE entries</param> /// <param name="pFieldMap">A dictionary of source and target field names to be translated on the source /// the title format field names and the special title field name all observe this field mapping.</param> /// <param name="pOnlyInFieldMap">If true, only writes fields that are included in the field-map</param> /// <returns>True on success, false on error</returns> /// <remarks> /// Presently implements special functions for KML that perhaps should be kept separate. /// </remarks> public static ReturnValue ExportFeatureLayerToOGR( string pDrvNm, IFeatureLayer pFLyr, string pOPFn, ProjectionInfo pSrcProj, ProjectionInfo pTgtProj, bool pHasTitle = false, string pTitleFieldNames = "", string pTitleFormat = "", List <string> pLCOpts = null, List <string> pDSCOpts = null, Dictionary <string, string> pFieldMap = null, bool pOnlyInFieldMap = false, bool pAppend = false) { var mReturnValue = new ReturnValue(true); //Check if data source and layer creation options are null and if so, create an empty list object for them pDSCOpts = (pDSCOpts != null) ? pDSCOpts : new List <string>(); pLCOpts = (pLCOpts != null) ? pLCOpts : new List <string>(); // May or may not be used, declared in any case; Dictionary <string, string> mTitleFieldValues = null; // Determine whether transformation is required bool mTransformRequired = (pSrcProj != pTgtProj) ? true : false; // Read the target SRS SpatialReference mTgtSRS = ExtFunctions.GetSpatialReferenceByEPSG(pTgtProj.AuthorityCode); // If transformation is needed, create fieldAttributes shared transformation object for the export if (mTransformRequired) { pFLyr.Projection = pSrcProj; pFLyr.Reproject(pTgtProj); } // Setup driver OSGeo.OGR.Driver drv = Ogr.GetDriverByName(pDrvNm); if (drv == null) { mReturnValue.AddMessage("Could not load driver", true); return(mReturnValue); } // Special handling of KML mFiles // Add special title name as datasource creation option if (pHasTitle && pDrvNm == "KML") { pDSCOpts.Add("NameField=" + ExtFunctions.TitleFieldName); } // Create fieldAttributes datasource DataSource ds = null; try { if (pAppend && (Directory.Exists(pOPFn) || File.Exists(pOPFn))) { ds = drv.Open(pOPFn, 1); } else { ds = drv.CreateDataSource(pOPFn, pDSCOpts.ToArray()); } if (ds == null) { mReturnValue.AddMessage("Could not create/open datasource"); return(mReturnValue); } } catch (Exception ex) { mReturnValue.AddMessage(ex.Message); return(mReturnValue); } // Find the geometry type of the source layer to determine output type wkbGeometryType mGeomType = DSGeomTypeToOgrGeomType(pFLyr.DataSet.FeatureType); if (mGeomType == wkbGeometryType.wkbNone) { mReturnValue.AddMessage("Could not parse geometry type of input layer"); return(mReturnValue); } // Create the new layer OSGeo.OGR.Layer l; if (pAppend && ds.HasLayer(pFLyr.LegendText.Replace(" ", "_"))) { l = ds.GetLayerByName(pFLyr.LegendText.Replace(" ", "_")); } else { l = ds.CreateLayer(pFLyr.LegendText, mTgtSRS, mGeomType, pLCOpts.ToArray()); } if (l == null) { mReturnValue.AddMessage("Failed to create file: "); return(mReturnValue); } // Create fieldAttributes list to hold the names of fields var mDataSourceFieldNames = new List <string>(); // Loop through all the fields foreach (DataColumn mDataColumn in pFLyr.DataSet.DataTable.Columns) { var mColType = DataTypeToOgrFieldType(mDataColumn.DataType); var mNewField = new FieldDefn(MapFieldName(mDataColumn.ColumnName, pFieldMap), mColType); if (mColType == FieldType.OFTString) { mNewField.SetWidth(mDataColumn.MaxLength); } if (pOnlyInFieldMap == false || (pOnlyInFieldMap && pFieldMap.Keys.Contains(mDataColumn.ColumnName))) { if (!pAppend || !l.HasField(mNewField.GetName())) { if (Ogr.OGRERR_NONE != l.CreateField(mNewField, 1)) { mReturnValue.AddMessage("Failed to add field: " + mDataColumn.ColumnName + " => " + MapFieldName(mDataColumn.ColumnName, pFieldMap)); return(mReturnValue); } } } mDataSourceFieldNames.Add(mNewField.GetName()); } // Add special title field, if the necessary information is present if (pHasTitle == true && !string.IsNullOrEmpty(pTitleFieldNames) && !string.IsNullOrEmpty(pTitleFormat) && !mDataSourceFieldNames.Contains(ExtFunctions.TitleFieldName)) { mTitleFieldValues = new Dictionary <string, string>(); var mTitleField = new FieldDefn(MapFieldName(TitleFieldName, pFieldMap), FieldType.OFTString); mTitleField.SetWidth(254); foreach (var mTitleFieldName in pTitleFieldNames.Split(',')) { mTitleFieldValues.Add(mTitleFieldName, ""); } l.CreateField(mTitleField, 1); } int createdFeatures = 0; // For each row in the source featureset for (int i = 0; i < pFLyr.DataSet.NumRows(); i++) { // Read the source feature IFeature mSrcFeature = pFLyr.DataSet.GetFeature(i); // Read and set the geometry on the new feature byte[] mSrcGeom = mSrcFeature.ToBinary(); var mFeature = new OSGeo.OGR.Feature(l.GetLayerDefn()); var mGeom = OSGeo.OGR.Geometry.CreateFromWkb(mSrcGeom); mFeature.SetGeometry(mGeom); // Set feature id mFeature.SetFID(i); // Handle fieldAttributes for (int j = 0; j < mDataSourceFieldNames.Count(); j++) { var mCurrentValue = mSrcFeature.DataRow[j]; if (pOnlyInFieldMap == false || (pOnlyInFieldMap && pFieldMap.Values.Contains(mDataSourceFieldNames[j]))) { if (mCurrentValue.GetType() == typeof(string)) { mFeature.SetField(mDataSourceFieldNames[j], Utilities.GetANSI((string)mCurrentValue)); } else if (mCurrentValue.GetType() == typeof(int)) { mFeature.SetField(mDataSourceFieldNames[j], (int)mCurrentValue); } else if (mCurrentValue.GetType() == typeof(double)) { mFeature.SetField(mDataSourceFieldNames[j], (double)mCurrentValue); } } if (pHasTitle && mTitleFieldValues.Keys.Contains(mDataSourceFieldNames[j])) { mTitleFieldValues[mDataSourceFieldNames[j]] = mCurrentValue.ToString(); } } // If title field is to be created, use field values collected above to construct fieldAttributes new string format // to go into the title-field if (pHasTitle == true) { var mTitleValue = string.Format(pTitleFormat, mTitleFieldValues.Values.ToArray <string>()); mFeature.SetField(MapFieldName(TitleFieldName, pFieldMap), Utilities.GetANSI(mTitleValue)); } // Create the feature try { l.CreateFeature(mFeature); createdFeatures++; } catch (Exception ex) { mReturnValue.AddMessage(ex.Message); mReturnValue.AddMessage("Tip: A datasource may appear read-only if the file location is inside a dropbox folder and the dropbox application is running"); } } if (mTransformRequired) { pFLyr.Reproject(pSrcProj); } // Dispose and cleanup l.Dispose(); ds.Dispose(); drv.Dispose(); mReturnValue.AddMessage(String.Format("Added {0} features to layer {1}", createdFeatures, pFLyr.LegendText)); return(mReturnValue); }
public static ReturnValue ExportDistrictsToMyAbuDhabiNet() { var r = new ReturnValue(); OSGeo.GDAL.Gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8"); var shpDir = Path.GetFileNameWithoutExtension(DistrictImport.GetShapefileName()); var ds = Ogr.OpenShared(DistrictImport.GetShapefileName(), 0); if (ds == null) { r.AddMessage("Data source doesn't exist", true); return(r); } var lyr = ds.GetLayerByIndex(0); if (lyr == null) { r.AddMessage("Layer doesn't destrict", true); return(r); } OSGeo.OGR.Feature nf; var srcProj = new SpatialReference(null); var tgtProj = new SpatialReference(null); srcProj.ImportFromEPSG(32640); tgtProj.ImportFromEPSG(4326); var trans = new CoordinateTransformation(srcProj, tgtProj); string geomWkt; Geometry geometry; string districtname_ar; string districtname_en; string districtabbreviation; StringBuilder sql = new StringBuilder(); //sql.AppendLine(sqlStatements.createDistrictTableSQL); sql.AppendLine(sqlStatements.emptyDistrictsSQL); while (null != (nf = lyr.GetNextFeature())) { geometry = nf.GetGeometryRef(); // Transformation goes here geometry.Transform(trans); geometry.ExportToWkt(out geomWkt); districtabbreviation = nf.GetFieldAsString("DISTRICTAB"); districtname_ar = nf.GetFieldAsString("NAMEARABIC"); districtname_en = nf.GetFieldAsString("NAMELATIN"); sql.AppendFormat(sqlStatements.insertDistrictsSQL, districtname_ar.MySQLEscape(), districtname_en.MySQLEscape(), geomWkt, districtabbreviation); } var dlg = new SaveFileDialog(); dlg.FileName = "districts.sql"; dlg.Filter = "SQL files|*.sql"; dlg.Title = "Select where to save districts SQL"; if (dlg.ShowDialog() == DialogResult.OK) { File.WriteAllText(dlg.FileName, sql.ToString()); r.Success = true; } return(r); }