经纬度生成点图层

更新时间:2024-03-17 12:08:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

经纬度生成点图层

private IFeatureClass CreateShapefile(string folderPath, DatabaseRecordset model,string strFeatureClassName, int GeometryType) {

IWorkspaceFactory pFeatWorkspaceFac = new ShapefileWorkspaceFactory();

IFeatureWorkspace pFeatWorkspace =

pFeatWorkspaceFac.OpenFromFile(folderPath, 0) as IFeatureWorkspace; if (pFeatWorkspace == null) return null; ESRI.ArcGIS.Geodatabase.IField oField = new ESRI.ArcGIS.Geodatabase.FieldClass();

ESRI.ArcGIS.Geodatabase.IFields oFields = new ESRI.ArcGIS.Geodatabase.FieldsClass(); IFieldsEdit oFieldsEdit = null; IFieldEdit oFieldEdit = null;

IFeatureClass oFeatureClass = null; long FCount = model.GetFieldCount(); try {

oFieldsEdit = oFields as ESRI.ArcGIS.Geodatabase.IFieldsEdit; oFieldEdit = oField as ESRI.ArcGIS.Geodatabase.IFieldEdit; oFieldEdit.Name_2 = \

oFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; IGeometryDef pGeo = new GeometryDefClass(); IGeometryDefEdit pGeoEdit = (IGeometryDefEdit)pGeo; if (GeometryType == 0) {

pGeoEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; }

else if (GeometryType == 1) {

pGeoEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;

} else {

pGeoEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon; }

//依据GeometryType创建相应几何类型的要素类

string ProjPath = Application.StartupPath + \\\\\xian1980 gk zone 18.prj\

ISpatialReferenceFactory2 spatialReferenceFactory = new SpatialReferenceEnvironmentClass();

ISpatialReference2 spatialReference =

spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(ProjPath) as ISpatialReference2;

pGeoEdit.SpatialReference_2 = spatialReference; //新要素类的空间参考依据投影文件xian1980 gk zone 18.prj pGeoEdit.SpatialReference.SetDomain(-450359962737.05, 450359962737.05, -450359962737.05, 450359962737.05); oFieldEdit.GeometryDef_2 = pGeo; oFieldsEdit.AddField(oField);

oField = new ESRI.ArcGIS.Geodatabase.FieldClass(); oFieldEdit = oField as ESRI.ArcGIS.Geodatabase.IFieldEdit; oFieldEdit.Name_2 = \

oFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID; oFieldsEdit.AddField(oField); for (int k = 0; k < FCount; k++) {

oField = new FieldClass(); oFieldEdit = oField as IFieldEdit;

oFieldEdit.Name_2 = model.GetFieldName(k); switch (model.GetFieldType(k)) {

case ADODB.DataTypeEnum.adBigInt: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; break;

case ADODB.DataTypeEnum.adBoolean: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger; break;

case ADODB.DataTypeEnum.adBSTR: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; break;

case ADODB.DataTypeEnum.adChar: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; break;

case ADODB.DataTypeEnum.adCurrency: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; break;

case ADODB.DataTypeEnum.adDate: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate; break;

case ADODB.DataTypeEnum.adDBDate: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate; break;

case ADODB.DataTypeEnum.adDBTime: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate; break;

case ADODB.DataTypeEnum.adDBTimeStamp: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate; break;

case ADODB.DataTypeEnum.adDecimal: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; break;

case ADODB.DataTypeEnum.adDouble: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; break;

case ADODB.DataTypeEnum.adInteger: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; break;

case ADODB.DataTypeEnum.adLongVarChar: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; break; case ADODB.DataTypeEnum.adLongVarWChar: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; break;

case ADODB.DataTypeEnum.adNumeric: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; break;

case ADODB.DataTypeEnum.adSingle: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeSingle; break;

case ADODB.DataTypeEnum.adSmallInt: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; break;

case ADODB.DataTypeEnum.adTinyInt: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; break;

case ADODB.DataTypeEnum.adUnsignedBigInt: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; break; case ADODB.DataTypeEnum.adUnsignedInt: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; break; case ADODB.DataTypeEnum.adUnsignedSmallInt: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; break; case ADODB.DataTypeEnum.adUnsignedTinyInt: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; break;

case ADODB.DataTypeEnum.adVarChar: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; break;

case ADODB.DataTypeEnum.adVarWChar: oFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; break; }

//以上实现从ADO的字段类型到ArcGIS的字段类型

oFieldEdit.Length_2 = model.GetFieldPrecision(k); oFieldEdit.Precision_2 = model.GetFieldPrecision(k); oFieldEdit.Scale_2 = model.GetFieldNumericScale(k); oFieldsEdit.AddField(oField); }

//if(File.Exists(folderPath+\\\\\\\ //{

// IFeatureClass fea = pFeatWorkspace.OpenFeatureClass(strFeatureClassName);

// if (fea != null) DeleteSDELayer(fea); // oFeatureClass = null; //}

if (File.Exists(folderPath + \+ strFeatureClassName + \

File.Delete(folderPath + \+ strFeatureClassName + \

if (File.Exists(folderPath + \+ strFeatureClassName + \ File.Delete(folderPath + \+ strFeatureClassName + \

if (File.Exists(folderPath + \+ strFeatureClassName + \ File.Delete(folderPath + \+ strFeatureClassName + \

if (File.Exists(folderPath + \+ strFeatureClassName + \ File.Delete(folderPath + \+ strFeatureClassName + \

if (File.Exists(folderPath + \+ strFeatureClassName + \ File.Delete(folderPath + \+ strFeatureClassName + \

if (File.Exists(folderPath + \+ strFeatureClassName + \ File.Delete(folderPath + \+ strFeatureClassName + \

oFeatureClass = pFeatWorkspace.CreateFeatureClass(strFeatureClassName, oFields, null, null, esriFeatureType.esriFTSimple, \\ }

catch(Exception e) { } finally { try {

System.Runtime.InteropServices.Marshal.ReleaseComObject(oField);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oF

ields);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldsEdit);

System.Runtime.InteropServices.Marshal.ReleaseComObject(oFieldEdit); }

catch { } GC.Collect(); }

return oFeatureClass; }

public bool CreateValidGISStation(string foderPath,string strFeatureClassName)

//该方法实现创建合法站点 {

DatabaseRecordset pRecordset = new DatabaseRecordset(); if (!ConnectToDatabase(pRecordset)) return false;//依据连接参数paras,自动初始化DatabaseRecordset的连接对象

if (!pRecordset.OpenTable(paras.GetValidStationName())) return false;//打开合法站点属性表

IFeatureClass pfc = CreateShapefile(foderPath, pRecordset, strFeatureClassName, 0);

if (pfc == null) return false;

IWorkspaceFactory pFeatWorkspaceFac = new ShapefileWorkspaceFactory();

IFeatureWorkspace pFeatWorkspace = pFeatWorkspaceFac.OpenFromFile(foderPath, 0) as IFeatureWorkspace; if (pFeatWorkspace == null) return false;

int Lon = pRecordset.GetFiledIndex(\经度字段的索引 int Lat = pRecordset.GetFiledIndex(\纬度字段的索引 if (Lon == -1) return false; if (Lat == -1) return false;

IWorkspaceEdit pEdit = (IWorkspaceEdit)pFeatWorkspace; pEdit.StartEditing(false);

long Count = pRecordset.GetRecordCount();//记录数 long FCount = pRecordset.GetFieldCount();//字段数

ISpatialReferenceFactory2 spatialReferenceFactory = new SpatialReferenceEnvironmentClass();

ISpatialReference2 FromSp = spatialReferenceFactory.CreateGeographicCoordinateSystem(4610) as ISpatialReference2;

string ProjPath = Application.StartupPath + \\\\\xian1980 gk zone 18.prj\

ISpatialReference2 ToSp = spatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile(ProjPath) as ISpatialReference2; IPoint pt = new PointClass(); for (long k = 0; k < Count; k++) {

List vs = pRecordset.GetValueAsList();

object[] os = vs.ToArray(); if (os[Lon] is DBNull) {

pRecordset.MoveNext(); continue; }

if (os[Lat] is DBNull) {

pRecordset.MoveNext(); continue; }

double x = Convert.ToDouble(os[Lon]);//经度 double y = Convert.ToDouble(os[Lat]);//纬度 if (x <= 0 || y <= 0) {

pRecordset.MoveNext(); continue; }

pt.X = x; pt.Y = y;

pt.SpatialReference = FromSp;

pt.Project(ToSp);//转换点的经纬度坐标为西安80坐标系投影坐标

IFeature pFeature = pfc.CreateFeature(); pFeature.Shape = (IGeometry)pt; for (int j = 0; j < FCount; j++) {

//string fName=pFeature.Fields.get_Field(j).Name.ToUpper(); if (os[j] is DBNull) continue;

//MessageBox.Show(Convert.ToString(os[j]));

pFeature.set_Value(j+2 , Convert.ToString(os[j]));//依据合法站点表的字段值为SDE合法站点相应字段赋值

}

pFeature.Store(); pRecordset.MoveNext(); }

pEdit.StopEditing(true); return true; }

本文来源:https://www.bwwdw.com/article/fyd8.html

Top