private void TryCreateSpatialRef() { try { _projectionRef = Dataset.GetProjectionRef(); if (!string.IsNullOrWhiteSpace(_projectionRef)) { _spatialRef = SpatialReferenceFactory.GetSpatialReferenceByWKT(_projectionRef, enumWKTSource.EsriPrjFile); } if (string.IsNullOrWhiteSpace(_projectionRef)) { TryReadSpatialRefFromSecondaryFile(); } if (_spatialRef == null) { _coordType = enumCoordType.Raster; } else { _coordType = _spatialRef.ProjectionCoordSystem != null ? enumCoordType.PrjCoord : enumCoordType.GeoCoord; } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
protected override void FillEntityToCotrols() { if (_isNew) { Text = "新建空间要素类..."; if (_dataset != null) { FillDataset(_dataset); } return; } else { Text = "编辑空间要素类属性..."; } SpatialFeatureClass ds = _entity as SpatialFeatureClass; if (ds.SpatialFeatureDataset != null) { FillDataset(ds.SpatialFeatureDataset); } txtName.Text = _entity.Name; txtDecription.Text = _entity.Description; txtMapScale.Value = ds.MapScale; txtSource.Text = ds.Source; if (ds.SpatialRef != null) { ISpatialReference sref = SpatialReferenceFactory.GetSpatialReferenceByWKT(ds.SpatialRef, enumWKTSource.EsriPrjFile); ucSpatialRef1.SpatialReference = sref; } }
public void ParserTiff() { string spreftxt = File.ReadAllText("f:\\2.prj"); ISpatialReference spref = SpatialReferenceFactory.GetSpatialReferenceByWKT(spreftxt, enumWKTSource.GDAL); Assert.NotNull(spref); Console.Write(spref.ToWKTString()); }
private void button18_Click(object sender, EventArgs e) { string wkt = "GEOGCS[\"等经纬度投影\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]]"; wkt = "PROJCS[\"Hammer投影\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137,298.257223563]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Hammer-Aitoff (world)\"]PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],PARAMETER[\"central_meridian\",105],UNIT[\"Meter\",1]]"; ISpatialReference srcSpatialRef = SpatialReferenceFactory.GetSpatialReferenceByWKT(wkt, enumWKTSource.EsriPrjFile); string proj4 = srcSpatialRef.ToProj4String(); ISpatialReference inverSpatialRef = SpatialReferenceFactory.GetSpatialReferenceByProj4String(proj4); inverSpatialRef.IsSame(srcSpatialRef); IProjectionTransform tr = ProjectionTransformFactory.GetProjectionTransform(srcSpatialRef, SpatialReference.GetDefault()); double[] x = new double[] { 1534910 }; double[] y = new double[] { 4255978 }; x = new double[] { 121.913 }; y = new double[] { 38.957 }; tr.InverTransform(x, y); tr.Transform(x, y); }
public FetClassWriterDb(ICatalogItem locationItem) { if (locationItem is CatalogDatabaseConn) { _dbConn = DbConnectionFactory.CreateDbConnection(((locationItem as CatalogDatabaseConn).Tag as SpatialDatabaseConn).ConnectionString); } else if (locationItem is CatalogFeatureDataset) { CatalogFeatureDataset cfd = locationItem as CatalogFeatureDataset; SpatialFeatureDataset sfd = cfd.Tag as SpatialFeatureDataset; _dbConn = DbConnectionFactory.CreateDbConnection((sfd as CatalogEntityBase)._connString); _datasetId = sfd.Id; _dstSpatialRef = SpatialReferenceFactory.GetSpatialReferenceByWKT(sfd.SpatialRef, enumWKTSource.EsriPrjFile); } else { throw new NotSupportedException("类型为\"" + locationItem + "\"的位置暂不支持写。"); } _adapter = DiffDbAdapterFactory.GetDiffDbAdapter(_dbConn); _dbConn.Open(); }
private static ISpatialReference TryGetSpatialRefForTilFile(string fname, out double[] GTs) { GTs = new double[6]; string[] allLines = File.ReadAllLines(fname); if (allLines == null || allLines.Length == 0) { return(null); } //filename = "08MAR25030758-M2AS_R1C1-052827221030_01_P001.TIF"; string atif = null; foreach (string aLine in allLines) { if (aLine.Contains("filename = \"") && aLine.EndsWith(".TIF\";")) { atif = aLine.Replace("filename = \"", string.Empty).Replace("\";", string.Empty).Trim(); break; } } if (atif == null) { return(null); } atif = Path.Combine(Path.GetDirectoryName(fname), atif); if (!File.Exists(atif)) { return(null); } using (Dataset ds = Gdal.Open(atif, Access.GA_ReadOnly)) { ds.GetGeoTransform(GTs); string spatialRefString = ds.GetProjectionRef(); if (string.IsNullOrWhiteSpace(spatialRefString)) { return(null); } return(SpatialReferenceFactory.GetSpatialReferenceByWKT(spatialRefString, enumWKTSource.GDAL)); } }
private void btnOk_Click(object sender, EventArgs e) { string path = txtInputDir.Text; string filter = txtInputFilter.Text; string projectionInfo = txtProjectionInfo.Text; string outdir = txtOutputDir.Text; string bandNames = txtDataNames.Text; string resolutionStr = txtResolution.Text; if (string.IsNullOrWhiteSpace(path) || !Directory.Exists(path)) { ShowMsg("参数不正确,输入路径为空或不存在"); return; } if (string.IsNullOrWhiteSpace(projectionInfo)) { return; } if (string.IsNullOrWhiteSpace(bandNames))//输入参数不正确,没有定义波段 { ShowMsg("参数不正确,没有定义波段"); return; } if (string.IsNullOrWhiteSpace(outdir)) { ShowMsg("参数不正确,没有定义输出路径"); return; } string[] files = GetFiles(path, filter); if (files == null || files.Length == 0) { ShowMsg("输入路径下获取文件为空"); return; } double resolution; if (!double.TryParse(resolutionStr, out resolution)) { ShowMsg("分辨率输入错误"); return; } CodeToCoordDef coordCode = new CodeToCoordDef(); ISpatialReference spatialRef = SpatialReferenceFactory.GetSpatialReferenceByWKT(projectionInfo, enumWKTSource.EsriPrjFile); try { if (!Directory.Exists(outdir)) { Directory.CreateDirectory(outdir); } StartProgress(); for (int fi = 0; fi < files.Length; fi++) { string file = files[fi]; Progress((int)((fi + 1) * 100f / files.Length)); PrjEnvelope envelope = GetEnvelopeFromFilename(file, coordCode); if (envelope == null || envelope.IsEmpty) { continue; } string[] args = new string[] { "datasets=" + bandNames }; using (IRasterDataProvider raster = RasterDataDriver.Open(file, args) as IRasterDataProvider) { if (raster == null) { continue; } if (raster.BandCount == 0) { continue; } using (IRasterDataProvider outRaster = CreateOutRaster(outdir, raster, spatialRef, envelope, resolution, resolution, bandNames)) { short[] buffer = new short[raster.Width * raster.Height]; GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); try { IntPtr ptr = handle.AddrOfPinnedObject(); for (int i = 0; i < raster.BandCount; i++) { raster.GetRasterBand(i + 1).Read(0, 0, raster.Width, raster.Height, ptr, raster.DataType, raster.Width, raster.Height); outRaster.GetRasterBand(i + 1).Write(0, 0, raster.Width, raster.Height, ptr, raster.DataType, raster.Width, raster.Height); } } finally { if (handle.IsAllocated) { handle.Free(); } } } } } UpdateArgs(); ShowMsg("执行结束"); } catch (Exception ex) { ShowMsg(ex.Message); } finally { FinishProgress(); } }