GIS的积累
It is never to late to learn

导航

 

1、IExtractionOp

  public static string  RasterClip(IRasterDataset pRasterDataset, IEnvelope pEnvelope, string rootDir, string fileName)
    {

        IRaster pRaster = pRasterDataset.CreateDefaultRaster();
        IRasterProps pProps = pRaster as IRasterProps;
        object cellSizeProvider = pProps.MeanCellSize().X;
        IGeoDataset pInputDataset = pRaster as IGeoDataset;
        IExtractionOp pExtractionOp = new RasterExtractionOpClass();
        IRasterAnalysisEnvironment pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment;
        pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
        object extentProvider = pEnvelope;
        object snapRasterData = Type.Missing;
        pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);
        IGeoDataset pOutputDataset = pExtractionOp.Rectangle(pInputDataset, pEnvelope, true);
      
        IRaster clipRaster;
        if (pOutputDataset is IRasterLayer)
        {
            IRasterLayer rasterLayer = pOutputDataset as IRasterLayer;
            clipRaster = rasterLayer.Raster;
        }
        else if (pOutputDataset is IRasterDataset)
        {
            IRasterDataset rasterDataset = pOutputDataset as IRasterDataset;
            clipRaster = rasterDataset.CreateDefaultRaster();
        }
        else if (pOutputDataset is IRaster)
        {
            clipRaster = pOutputDataset as IRaster;
        }
        else
        {
            return "";
        }
       
        IWorkspaceFactory pWKSF = new RasterWorkspaceFactoryClass();
        IWorkspace pWorkspace = pWKSF.OpenFromFile(rootDir, 0);
        ISaveAs pSaveAs = clipRaster as ISaveAs;
        pSaveAs.SaveAs(fileName + ".jpg", pWorkspace, "JPG");
       
        return @"\\meng\temp" + "\\" + fileName + ".jpg";


    }

2、ExportMapImage

1 public string ExportImage(string catalogName, int objectID, double XMin, double YMin, double XMax, double YMax,int Height,int Width)
2 {
3
4 if (catalogName == "" || objectID.ToString() == "" || XMin.ToString() == "" || YMin.ToString() == "" || XMax.ToString() == "" ||
5 YMax.ToString() == "" || Width.ToString() == "" || Height.ToString() == "")
6 return "请检查参数输入是否完整";
7
8 IWorkspace pWorkspace = AEHelper.ConnectSDE();
9
10 IRasterWorkspaceEx pRasterWorkspaceEx = (IRasterWorkspaceEx)pWorkspace;
11 IRasterCatalog pRasterCatalog = pRasterWorkspaceEx.OpenRasterCatalog(catalogName);
12
13 IFeatureClass pFeatureClass = (IFeatureClass)pRasterCatalog;
14 int f_serverid_index = pFeatureClass.FindField("F_SERVERID");
15 IFeature pFeature = pFeatureClass.GetFeature(objectID);
16 string f_serverid = pFeature.get_Value(f_serverid_index).ToString();
17
18 IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
19 ITable pServerTable = pFeatureWorkspace.OpenTable("PRJIMAGEDB.TBIMG_SERVERS");
20 IQueryFilter pQueryFilter = new QueryFilterClass();
21 pQueryFilter.WhereClause = "F_ID" + " = " + "'" + f_serverid + "'";
22 ICursor pServerCursor = pServerTable.Search(pQueryFilter, true);
23 IRow pServerRow = pServerCursor.NextRow();
24 int f_address_index = pServerTable.FindField("F_ADDRESS");
25 string f_address = pServerRow.get_Value(f_address_index).ToString();
26
27 ITable pDataPathTable = pFeatureWorkspace.OpenTable("PRJIMAGEDB.TBIMG_DATAPATH");
28 pQueryFilter = new QueryFilterClass();
29 pQueryFilter.WhereClause = "F_OBJECTID" + " = " + objectID;
30 ICursor pDataPathCursor = pDataPathTable.Search(pQueryFilter, true);
31 IRow pDataPathRow = pDataPathCursor.NextRow();
32 int f_filelocation_index = pDataPathTable.FindField("F_FILELOCATION");
33 string f_filelocation = "";
34 string temp_filelocation = "";
35 while (pDataPathRow != null)
36 {
37 temp_filelocation = pDataPathRow.get_Value(f_filelocation_index).ToString();
38 if (temp_filelocation.Substring(temp_filelocation.LastIndexOf('.') + 1, 3) == "tif")
39 {
40 f_filelocation = temp_filelocation;
41 break;
42 }
43 pDataPathRow = pDataPathCursor.NextRow();
44 }
45 f_filelocation = f_filelocation.Replace('/', '\\');
46 string finalpath = f_address + f_filelocation;
47
48 int imgindex = finalpath.LastIndexOf('\\');
49 string imgpath = finalpath.Substring(0, imgindex);
50 string imgname = finalpath.Substring(imgindex + 1);
51
52 IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
53 IRasterWorkspace rasterWorkspace = (IRasterWorkspace)(workspaceFactory.OpenFromFile(imgpath, 0));
54 IRasterDataset rasterDataset = rasterWorkspace.OpenRasterDataset(imgname);
55
56 IMapControl2 pMapControl = new MapControlClass();
57 IRasterLayer rasterLayer = new RasterLayerClass();
58 rasterLayer.CreateFromDataset(rasterDataset);
59
60 pMapControl.ClearLayers();
61 pMapControl.AddLayer(rasterLayer, 0);
62 pMapControl.Extent = pMapControl.FullExtent;
63
64
65 IEnvelope pEnvelope = new EnvelopeClass();
66 pEnvelope.PutCoords(XMin, YMin, XMax, YMax);
67
68 IActiveView pActiveView = (IActiveView)pMapControl.Map;
69 pActiveView.Extent = pEnvelope;
70 pActiveView.Refresh();
71
72
73 IExport export = new ExportJPEGClass();
74 string fileName = Guid.NewGuid().ToString();
75 string finalName = @"\\meng\temp" + "\\" + fileName + ".jpg";
76 export.ExportFileName = finalName;
77
78 // Microsoft Windows default DPI resolution
79   export.Resolution = 384; //96*4
80 tagRECT exportRECT = new tagRECT();
81 exportRECT.left = 0;
82 exportRECT.top = 0;
83 exportRECT.right = Width;
84 exportRECT.bottom = Height;
85
86 IEnvelope envelope = new EnvelopeClass();
87 envelope.PutCoords(exportRECT.left, exportRECT.top, exportRECT.right, exportRECT.bottom);
88 export.PixelBounds = envelope;
89 System.Int32 hDC = export.StartExporting();
90 pActiveView.Output(hDC, (System.Int16)export.Resolution, ref exportRECT, null, null);
91
92 // Finish writing the export file and cleanup any intermediate files
93 export.FinishExporting();
94 export.Cleanup();
95
96
97 return finalName;
98 }

3、动态生成WMS

[WebMethod(Description = "导出WMS影像")]
public string ExportWMSImage(string catalogName, int objectID, double XMin, double YMin, double XMax, double YMax, int Height, int Width)
{

if (catalogName == "" || objectID.ToString() == "" || XMin.ToString() == "" || YMin.ToString() == "" || XMax.ToString() == "" ||
YMax.ToString()
== "" || Width.ToString() == "" || Height.ToString() == "")
return "请检查参数输入是否完整";

IWorkspace pWorkspace
= AEHelper.ConnectSDE();

IRasterWorkspaceEx pRasterWorkspaceEx
= (IRasterWorkspaceEx)pWorkspace;
IRasterCatalog pRasterCatalog
= pRasterWorkspaceEx.OpenRasterCatalog(catalogName);

IFeatureClass pFeatureClass
= (IFeatureClass)pRasterCatalog;
int f_serverid_index = pFeatureClass.FindField("F_SERVERID");
IFeature pFeature
= pFeatureClass.GetFeature(objectID);
string f_serverid = pFeature.get_Value(f_serverid_index).ToString();

IFeatureWorkspace pFeatureWorkspace
= (IFeatureWorkspace)pWorkspace;
ITable pServerTable
= pFeatureWorkspace.OpenTable("PRJIMAGEDB.TBIMG_SERVERS");
IQueryFilter pQueryFilter
= new QueryFilterClass();
pQueryFilter.WhereClause
= "F_ID" + " = " + "'" + f_serverid + "'";
ICursor pServerCursor
= pServerTable.Search(pQueryFilter, true);
IRow pServerRow
= pServerCursor.NextRow();
int f_address_index = pServerTable.FindField("F_ADDRESS");
string f_address = pServerRow.get_Value(f_address_index).ToString();

ITable pDataPathTable
= pFeatureWorkspace.OpenTable("PRJIMAGEDB.TBIMG_DATAPATH");
pQueryFilter
= new QueryFilterClass();
pQueryFilter.WhereClause
= "F_OBJECTID" + " = " + objectID;
ICursor pDataPathCursor
= pDataPathTable.Search(pQueryFilter, true);
IRow pDataPathRow
= pDataPathCursor.NextRow();
int f_filelocation_index = pDataPathTable.FindField("F_FILELOCATION");
string f_filelocation = "";
string temp_filelocation = "";
while (pDataPathRow != null)
{
temp_filelocation
= pDataPathRow.get_Value(f_filelocation_index).ToString();
if (temp_filelocation.Substring(temp_filelocation.LastIndexOf('.') + 1, 3) == "tif")
{
f_filelocation
= temp_filelocation;
break;
}
pDataPathRow
= pDataPathCursor.NextRow();
}
f_filelocation
= f_filelocation.Replace('/', '\\');
string finalpath = f_address + f_filelocation;

int imgindex = finalpath.LastIndexOf('\\');
string imgpath = finalpath.Substring(0, imgindex);
string imgname = finalpath.Substring(imgindex + 1);

string url = CreateMWS(imgpath, imgname, XMin, YMin, XMax, YMax, Height, Width);

return url;

}
private string CreateMWS(string imgpath, string imgname, double XMin, double YMin, double XMax, double YMax, int Height, int Width)
{
//获取发布的主机名称,服务名称,发布文件的地址
string hostname = ConfigurationManager.AppSettings["WMSServer"].ToString();
string servicename = Guid.NewGuid().ToString();

//IGISServerConnection pGISServerConnection = new GISServerConnectionClass();
//请注意:connect输入的主机名,表示要连接的服务器
//pGISServerConnection.Connect(hostname); //IServerContext.CreateObject???

//ServerConnection sc = new ServerConnection();
//sc.Host = hostname;
//sc.Connect();

ESRI.ArcGIS.ADF.Identity identity
= new ESRI.ArcGIS.ADF.Identity("Administrator", "000", "meng");
ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection gisconnection
= new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("meng",
identity);
gisconnection.Connect();


IServerObjectAdmin pServerObjectAdmin
= gisconnection.ServerObjectAdmin;
IServerObjectConfiguration2 configuration
= (IServerObjectConfiguration2)pServerObjectAdmin.CreateConfiguration();

//获得server服务器列表,判断发布的服务是否存在
IEnumServerObjectConfiguration ss = pServerObjectAdmin.GetConfigurations();
IServerObjectConfiguration2 pp;
for (int i = 0; i < ss.Count; i++)
{
pp
= (IServerObjectConfiguration2)ss.Next();
String name
= pp.Name;
if (name == servicename)
{
return "发布的服务名称已经存在!";
}
}

IWorkspaceFactory workspaceFactory
= new RasterWorkspaceFactoryClass();
IRasterWorkspace rasterWorkspace
= (IRasterWorkspace)(workspaceFactory.OpenFromFile(imgpath, 0));
IRasterDataset rasterDataset
= rasterWorkspace.OpenRasterDataset(imgname);

// Create a raster for viewing
ESRI.ArcGIS.Carto.IRasterLayer rasterLayer = new ESRI.ArcGIS.Carto.RasterLayerClass();
rasterLayer.CreateFromDataset(rasterDataset);

IMapControlDefault pMapCtrl
= new MapControlClass();
pMapCtrl.AddLayer(rasterLayer
as ILayer, 0);

//保存mxd文件
string tempMXD = @"\\meng\mxd" + "\\temp.mxd";
IMxdContents pMxdC
= (IMxdContents)pMapCtrl.Map;
IMapDocument pMapDocument
= new MapDocumentClass();

if (File.Exists(tempMXD))
File.Delete(tempMXD);

pMapDocument.New(tempMXD);
IActiveView pActiveView
= pMapCtrl.Map as IActiveView;
pMapDocument.ReplaceContents(pMxdC);
pMapDocument.Save(
true, true);


configuration.Name
= servicename;//发布Service的名称,必填
configuration.TypeName = "MapServer";//发布服务的类型,如:MapServer,GeocodeServer
IPropertySet props = configuration.Properties;
//props.SetProperty("FilePath", @"E:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\World\world.mxd");//设置MXD的路径
props.SetProperty("FilePath", tempMXD);//设置MXD的路径
//一下的property并非必须,只要一个filepath就可以发布


props.SetProperty(
"OutputDir", "c:\\arcgisserver\\arcgisoutput");//图片的输出目录
string VirtualOutPutDir = "http://" + hostname + "/arcgisoutput";
props.SetProperty(
"VirtualOutPutDir", VirtualOutPutDir);//图片输出的虚拟路径
props.SetProperty("SupportedImageReturnTypes", "URL");//支持的图片类型
props.SetProperty("MaxImageHeight", "2048");//图片的最大高度
props.SetProperty("MaxRecordCount", "500");//返回记录的最大条数
props.SetProperty("MaxBufferCount", "100");//缓冲区分析的最大数目
props.SetProperty("MaxImageWidth", "2048");//图片的最大宽度
props.SetProperty("IsCached", "false");//是否切片
props.SetProperty("CacheOnDemand", "false");//是否主动切片
props.SetProperty("IgnoreCache", "false");//是否忽略切片
props.SetProperty("ClientCachingAllowed", "true");//是否允许客户端缓冲
string CacheDir = "c:\\arcgisserver\\arcgiscache\\" + servicename;
props.SetProperty(
"CacheDir", CacheDir);//切片的输出路径
props.SetProperty("SOMCacheDir", "c:\\arcgisserver\\arcgiscache");//som的切片输出路径


//设置WMS
configuration.set_ExtensionEnabled("WMSServer", true);
IPropertySet wmsprops
= configuration.get_ExtensionProperties("WMSServer");
wmsprops.SetProperty(
"CustomGetCapabilities", "false");
wmsprops.SetProperty(
"PathToCustomGetCapabilitiesFiles", "");
wmsprops.SetProperty(
"Name", "WMS");
wmsprops.SetProperty(
"Title", servicename);
wmsprops.SetProperty(
"Abstract", "");
wmsprops.SetProperty(
"Keyword", "");
string OnlineResource = hostname + "/arcgis/services/" + servicename + "/MapServer/WMSServer";
wmsprops.SetProperty(
"OnlineResource", OnlineResource);
configuration.set_ExtensionProperties(
"WMSServer", wmsprops);
IPropertySet wmsinfo
= configuration.get_ExtensionInfo("WMSServer");
wmsinfo.SetProperty(
"WebEnabled", "true");
wmsinfo.SetProperty(
"WebCapabilities", "");
configuration.set_ExtensionInfo(
"WMSServer", wmsinfo);


configuration.Description
= servicename;//Service的描述
configuration.IsolationLevel = esriServerIsolationLevel.esriServerIsolationHigh;//或者esriServerIsolationLow,esriServerIsolationAny
configuration.IsPooled = true;//是否池化
configuration.MaxInstances = 1;//最多的实例数
configuration.MinInstances = 1;//最少的实例数

//设置刷新
IPropertySet recycleProp = configuration.RecycleProperties;
recycleProp.SetProperty(
"StartTime", "00:00");//刷新开始时间
recycleProp.SetProperty("Interval", "3600");//刷新间隔
//设置是否开启REST服务
IPropertySet infoProp = configuration.Info;
infoProp.SetProperty(
"WebEnabled", "true");//是否提供REST服务
infoProp.SetProperty("WebCapabilities", "Map,Query,Data");//提供何种服务
configuration.StartupType = esriStartupType.esriSTAutomatic;//或者esriSTManual
configuration.UsageTimeout = 120;//客户端占用一个服务的最长时间
configuration.WaitTimeout = 120;//客户端申请一个服务的最长等待时间


configuration.set_ExtensionEnabled(
"WMSServer", true);
//添加服务到Server
pServerObjectAdmin.AddConfiguration(configuration);
//删除服务
//pServerObjectAdmin.DeleteConfiguration("NewService", "MapServer");
//启动服务
pServerObjectAdmin.StartConfiguration(servicename, "MapServer");

return "http://" + hostname + "/arcgis/services/" + servicename + "/MapServer/WMSServer?" +
"version=1.3.0&request=getmap&layers=0&styles=population&crs=4326&bbox=" +
XMin.ToString()
+ "," + YMin.ToString() + "," + XMax.ToString() + "," + YMax.ToString() + "&width=" + Width.ToString() +
"&height=" + Height.ToString() + "&format=image/gif";

}

posted on 2011-07-01 21:28  GIS的学习  阅读(1076)  评论(0)    收藏  举报