阅读:2135回复:2
[转帖]北京ArcGis Server开发培训例子(整理)
<DIV class=blog_content>
<P>前段时间到北京去参加了ArcGis Server应用开发的培训,发现基本上的例子都和Flyingis上面的Arcgsi Server 开发系列的文章一样,只有最后两个例子没有。由于培训方不提供源代码,所有在培训时做的试验例子都无法拷出来。唉,没有办法,只有培训完回来以后,重新整理一下放上来,以备查!</P> <P> </P> <P><STRONG>整理的例子包括</STRONG></P> <P>1、Web ADF 编程——查询功能的实现</P> <P>2、Graphics data sources——高亮功能的实现</P> <P>3、ArcGIS Server data sources——Buffer功能的实现</P> <P>4、自定义Tool</P> <P>5、自定义Task</P> <P>6、扩展Arcgis Server</P> <P> </P> <P>由于前面5个例子在Flyingis的博客里都作了详细的介绍,我这里就不作介绍了,这里主要介绍最后一个例子,扩展Arcgis Server。</P> <P><STRONG>目标:</STRONG></P> <P>扩展Arcgis Server,根据省份名称,得到省份面积</P> <P>当然这里只是一个例子,大家可以做很复杂的扩展</P> <P> </P> <P><STRONG>试验前准备:</STRONG></P> <P>1、准备一个全国各省的地图数据</P> <P>2、创建一个province的地图文档</P> <P>3、通过province的地图文档发布一个地图服务</P> <P> </P> <P><STRONG>试验步骤:</STRONG></P> <P><STRONG>1、编写实现IServerObjectExtension的类,也就是你想添加的功能,新建DAANSOE项目,添加一个class文件,改名为</STRONG><STRONG>GetArea.cs,代码如下:</STRONG></P> <DIV class=dp-highlighter> <DIV class=bar> <DIV class=tools>C#代码 <a href="#" target="_blank" ><IMG src="http://www.gisempire.com/images/icon_copy.gif"></A></DIV></DIV> <OL> <LI><SPAN style=<FONT color=#0000ff>"COLOR: #000000"</FONT>><STRONG><FONT color=#7f0055>using</FONT></STRONG> System; </LI> <LI><STRONG><FONT color=#7f0055>using</FONT></STRONG> System.Collections.Generic; </LI> <LI><STRONG><FONT color=#7f0055>using</FONT></STRONG> System.Text; </LI> <LI><STRONG><FONT color=#7f0055>using</FONT></STRONG> System.EnterpriseServices; </LI> <LI><STRONG><FONT color=#7f0055>using</FONT></STRONG> ESRI.ArcGIS.ADF.ArcGISServer; </LI> <LI><STRONG><FONT color=#7f0055>using</FONT></STRONG> ESRI.ArcGIS.Server; </LI> <LI><STRONG><FONT color=#7f0055>using</FONT></STRONG> System.Runtime.InteropServices; </LI> <LI><STRONG><FONT color=#7f0055>using</FONT></STRONG> ESRI.ArcGIS.Carto; </LI> <LI><STRONG><FONT color=#7f0055>using</FONT></STRONG> ESRI.ArcGIS.Geodatabase; </LI> <LI><STRONG><FONT color=#7f0055>using</FONT></STRONG> ESRI.ArcGIS.Geometry; </LI> <LI> </LI> <LI> </LI> <LI><STRONG><FONT color=#7f0055>namespace</FONT></STRONG> DAANSOE </LI> <LI>{ </LI> <LI> [Guid(<FONT color=#0000ff>"560680E2-1DFB-438c-BA66-27A144BECE55"</FONT>)] </LI> <LI> <STRONG><FONT color=#7f0055>public</FONT></STRONG> <STRONG><FONT color=#7f0055>interface</FONT></STRONG> IActcArea </LI> <LI> { </LI> <LI> <STRONG><FONT color=#7f0055>double</FONT></STRONG> GetArea(<STRONG><FONT color=#7f0055>string</FONT></STRONG> sql); </LI> <LI> } </LI> <LI> </LI> <LI> [AutomationProxy(<STRONG><FONT color=#7f0055>true</FONT></STRONG>), ClassInterface(ClassInterfaceType.AutoDual), Guid(<FONT color=#0000ff>"68F301BC-7A2F-44ea-A8B1-042431321719"</FONT>)] </LI> <LI> <STRONG><FONT color=#7f0055>public</FONT></STRONG> <STRONG><FONT color=#7f0055>class</FONT></STRONG> SOEServer : ServicedComponent, IActcArea, IServerObjectExtension </LI> <LI> { </LI> <LI> </LI> <LI> <STRONG><FONT color=#7f0055>private</FONT></STRONG> IServerObjectHelper m_SOH; </LI> <LI></LI> <LI></LI> <LI><FONT color=#808080> #region IActcArea 成员 </FONT> </LI> <LI> </LI> <LI> <FONT color=#008200>/*次方法为测试时候的方法 </FONT> </LI> <LI><FONT color=#008200> public double GetArea(double x, double y) </FONT> </LI> <LI><FONT color=#008200> { </FONT> </LI> <LI><FONT color=#008200> return x + y; </FONT> </LI> <LI><FONT color=#008200> } </FONT> </LI> <LI><FONT color=#008200> */</FONT> </LI> <LI> </LI> <LI> <STRONG><FONT color=#7f0055>public</FONT></STRONG> <STRONG><FONT color=#7f0055>double</FONT></STRONG> GetArea(<STRONG><FONT color=#7f0055>string</FONT></STRONG> sql) </LI> <LI> { </LI> <LI> IMapServer mapServer = m_SOH.ServerObject <STRONG><FONT color=#7f0055>as</FONT></STRONG> IMapServer; </LI> <LI> IMapServerObjects mapServerObjs = mapServer <STRONG><FONT color=#7f0055>as</FONT></STRONG> IMapServerObjects; </LI> <LI> IMap map = mapServerObjs.get_Map(mapServer.DefaultMapName); </LI> <LI> IFeatureLayer pflayer = map.get_Layer(0) <STRONG><FONT color=#7f0055>as</FONT></STRONG> IFeatureLayer; </LI> <LI> IFeatureClass pfc = pflayer.FeatureClass; </LI> <LI> </LI> <LI> ISpatialFilter psf = <STRONG><FONT color=#7f0055>new</FONT></STRONG> SpatialFilterClass(); </LI> <LI> psf.WhereClause = sql; </LI> <LI> psf.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects; </LI> <LI> </LI> <LI> IFeatureCursor pfCursor = pfc.Search(psf <STRONG><FONT color=#7f0055>as</FONT></STRONG> IQueryFilter, <STRONG><FONT color=#7f0055>false</FONT></STRONG>); </LI> <LI> IFeature pfeature = pfCursor.NextFeature(); </LI> <LI> </LI> <LI> <STRONG><FONT color=#7f0055>if</FONT></STRONG> (pfeature == <STRONG><FONT color=#7f0055>null</FONT></STRONG>) </LI> <LI> <STRONG><FONT color=#7f0055>return</FONT></STRONG> -2; </LI> <LI> IPolygon polygon = pfeature.Shape <STRONG><FONT color=#7f0055>as</FONT></STRONG> IPolygon; </LI> <LI> </LI> <LI> IArea pArea = polygon <STRONG><FONT color=#7f0055>as</FONT></STRONG> IArea; </LI> <LI> </LI> <LI> <STRONG><FONT color=#7f0055>return</FONT></STRONG> pArea.Area; </LI> <LI> } </LI> <LI></LI> <LI><FONT color=#808080> #endregion </FONT> </LI> <LI></LI> <LI><FONT color=#808080> #region IServerObjectExtension 成员 </FONT> </LI> <LI> </LI> <LI> <STRONG><FONT color=#7f0055>void</FONT></STRONG> IServerObjectExtension.Init(IServerObjectHelper pSOH) </LI> <LI> { </LI> <LI> m_SOH = pSOH; </LI> <LI> } </LI> <LI> </LI> <LI> <STRONG><FONT color=#7f0055>void</FONT></STRONG> IServerObjectExtension.Shutdown() </LI> <LI> { </LI> <LI> m_SOH = <STRONG><FONT color=#7f0055>null</FONT></STRONG>; </LI> <LI> } </LI> <LI></LI> <LI><FONT color=#808080> #endregion </FONT> </LI> <LI> } </LI> <LI> </LI> <LI>} </LI> <LI></SPAN> </LI></OL></DIV><PRE>using System; using System.Collections.Generic; using System.Text; using System.EnterpriseServices; using ESRI.ArcGIS.ADF.ArcGISServer; using ESRI.ArcGIS.Server; using System.Runtime.InteropServices; using ESRI.ArcGIS.Carto; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geometry; namespace DAANSOE { [Guid("560680E2-1DFB-438c-BA66-27A144BECE55")] public interface IActcArea { double GetArea(string sql); } [AutomationProxy(true), ClassInterface(ClassInterfaceType.AutoDual), Guid("68F301BC-7A2F-44ea-A8B1-042431321719")] public class SOEServer : ServicedComponent, IActcArea, IServerObjectExtension { private IServerObjectHelper m_SOH; #region IActcArea 成员 /*次方法为测试时候的方法 public double GetArea(double x, double y) { return x + y; } */ public double GetArea(string sql) { IMapServer mapServer = m_SOH.ServerObject as IMapServer; IMapServerObjects mapServerObjs = mapServer as IMapServerObjects; IMap map = mapServerObjs.get_Map(mapServer.DefaultMapName); IFeatureLayer pflayer = map.get_Layer(0) as IFeatureLayer; IFeatureClass pfc = pflayer.FeatureClass; ISpatialFilter psf = new SpatialFilterClass(); psf.WhereClause = sql; psf.SpatialRel = ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelIntersects; IFeatureCursor pfCursor = pfc.Search(psf as IQueryFilter, false); IFeature pfeature = pfCursor.NextFeature(); if (pfeature == null) return -2; IPolygon polygon = pfeature.Shape as IPolygon; IArea pArea = polygon as IArea; return pArea.Area; } #endregion #region IServerObjectExtension 成员 void IServerObjectExtension.Init(IServerObjectHelper pSOH) { m_SOH = pSOH; } void IServerObjectExtension.Shutdown() { m_SOH = null; } #endregion } } </PRE> <P> </P> <P><STRONG>2、使用Regasm注册Server Object Extension</STRONG></P> <P>进入vs2005的命令提示框:</P> <P>E:\Program Files\Microsoft Visual Studio 8\VC></P> <P> </P> <P>将当前目录切换到你上面那个dll所在的目录:</P> <P>E:\project\北京培训\BeiJingTraining\Exercise11\DAANSOE\bin\Debug></P> <P> </P> <P>执行如下命令进行注册:</P> <P>regasm /tlb:DAANSOE.tlb /codebase DAANSOE.dll</P> <P> </P> <P>执行完成后可以到dll目录下验证相应的.tlb是否生成</P> <P> </P> <P>(如果要注销注册,使用 regasm /u DAANSOE.dll /tlb DAANSOE.tlb /codebase 命令)</P> <P> </P> <P><STRONG>3、注册Server Object Extension到Gis Server</STRONG></P> <P>我们将通过一段代码将Server Object Extension注册到Gis Server中。</P> <P>新建一个Console项目,命令为RegisterToGisServer,在Main方法里添加如下代码:</P> <DIV class=dp-highlighter> <DIV class=bar> <DIV class=tools>C#代码 <a href="#" target="_blank" ><IMG src="http://www.gisempire.com/images/icon_copy.gif"></A></DIV></DIV> <OL> <LI><SPAN style=<FONT color=#0000ff>"COLOR: #000000"</FONT>>Identity identity = <STRONG><FONT color=#7f0055>new</FONT></STRONG> Identity(<FONT color=#0000ff>"username"</FONT>, <FONT color=#0000ff>"888888"</FONT>, <FONT color=#0000ff>"domain"</FONT>); </LI> <LI> AGSServerConnection connection = <STRONG><FONT color=#7f0055>new</FONT></STRONG> AGSServerConnection(<FONT color=#0000ff>"fmc-pca187"</FONT>, identity); </LI> <LI> connection.Connect(); </LI> <LI> </LI> <LI> IServerObjectAdmin2 soa = connection.ServerObjectAdmin <STRONG><FONT color=#7f0055>as</FONT></STRONG> IServerObjectAdmin2; </LI> <LI> IServerObjectExtensionType soe = soa.CreateExtensionType(); </LI> <LI> soe.CLSID = <FONT color=#0000ff>"DAANSOE.SOEServer"</FONT>; </LI> <LI> soe.Name = <FONT color=#0000ff>"SOEServer"</FONT>; </LI> <LI> soe.Description = <FONT color=#0000ff>"Test Application"</FONT>; </LI> <LI> soa.AddExtensionType(<FONT color=#0000ff>"MapServer"</FONT>, soe);</SPAN> </LI></OL></DIV><PRE>Identity identity = new Identity("username", "888888", "domain"); AGSServerConnection connection = new AGSServerConnection("fmc-pca187", identity); connection.Connect(); IServerObjectAdmin2 soa = connection.ServerObjectAdmin as IServerObjectAdmin2; IServerObjectExtensionType soe = soa.CreateExtensionType(); soe.CLSID = "DAANSOE.SOEServer"; soe.Name = "SOEServer"; soe.Description = "Test Application"; soa.AddExtensionType("MapServer", soe);</PRE> <P> 运行这段代码,执行完后可以到<ArcGIS Server Install Location>\server\system folder 打开ServerTypesExt.dat进行验证</P> <P> </P> <P><STRONG>4、停止province服务,右键切换到Service Properties的Capabilities,将SOEServer打上钩,重新启动province服务</STRONG></P> <P><STRONG></STRONG> </P> <P><STRONG>5、使用Server Object Extension</STRONG></P> <P>新建一个web应用,新建一个aspx页面,改名为UseExtensionGisServer.aspx,加入相应控件,在button的click事件里实现如下代码:</P> <DIV class=dp-highlighter> <DIV class=bar> <DIV class=tools>C#代码 <a href="#" target="_blank" ><IMG src="http://www.gisempire.com/images/icon_copy.gif"></A></DIV></DIV> <OL> <LI><SPAN style=<FONT color=#0000ff>"COLOR: #000000"</FONT>>Identity identity = <STRONG><FONT color=#7f0055>new</FONT></STRONG> Identity(<FONT color=#0000ff>"username"</FONT>, <FONT color=#0000ff>"888888"</FONT>, <FONT color=#0000ff>"domain"</FONT>); </LI> <LI> AGSServerConnection connection = <STRONG><FONT color=#7f0055>new</FONT></STRONG> AGSServerConnection(<FONT color=#0000ff>"fmc-pca187"</FONT>, identity); </LI> <LI> connection.Connect(); </LI> <LI> </LI> <LI> IServerObjectManager som = connection.ServerObjectManager <STRONG><FONT color=#7f0055>as</FONT></STRONG> IServerObjectManager; </LI> <LI> IServerContext serverContext = som.CreateServerContext(<FONT color=#0000ff>"province"</FONT>, <FONT color=#0000ff>"MapServer"</FONT>); </LI> <LI> IServerObjectExtensionManager soem = serverContext.ServerObject <STRONG><FONT color=#7f0055>as</FONT></STRONG> IServerObjectExtensionManager; </LI> <LI> IServerObjectExtension soe = soem.FindExtensionByTypeName(<FONT color=#0000ff>"SOEServer"</FONT>); </LI> <LI> </LI> <LI> IActcArea pAear = soe <STRONG><FONT color=#7f0055>as</FONT></STRONG> IActcArea; </LI> <LI> <STRONG><FONT color=#7f0055>double</FONT></STRONG> area = pAear.GetArea(TextBox1.Text.ToString()); </LI> <LI> </LI> <LI> serverContext.ReleaseContext(); </LI> <LI> Response.Write(<FONT color=#0000ff>"<script>alert("</FONT> + area.ToString() + <FONT color=#0000ff>");</script>"</FONT>);</SPAN> </LI></OL></DIV><PRE>Identity identity = new Identity("username", "888888", "domain"); AGSServerConnection connection = new AGSServerConnection("fmc-pca187", identity); connection.Connect(); IServerObjectManager som = connection.ServerObjectManager as IServerObjectManager; IServerContext serverContext = som.CreateServerContext("province", "MapServer"); IServerObjectExtensionManager soem = serverContext.ServerObject as IServerObjectExtensionManager; IServerObjectExtension soe = soem.FindExtensionByTypeName("SOEServer"); IActcArea pAear = soe as IActcArea; double area = pAear.GetArea(TextBox1.Text.ToString()); serverContext.ReleaseContext(); Response.Write("<script>alert(" + area.ToString() + ");</script>");</PRE> <P> 预览效果如下:</P> <P> <IMG src="http://p.blog.csdn.net/images/p_blog_csdn_net/junmail/EntryImages/20080801/1633532030194492616.jpg"></P> <P>至此,如果上面一切顺利的话,整个功能就完成了,单击确定 ,就可以弹出四川省的面积。</P> <P>注意事项:</P> <P>如果Server Object Extension注册到Arcgis Server以后,又启动了province服务,这个时候要重新生成DAANSOE的话,需要把province停掉,不然会抱错,然后重新使用regasm工具注册修改后的dll。</P> <P>最后,我把以上几个例子的整个项目作为附件上传上来,这些例子都是我在本机调试通过的,大家下载以后需要对identity的参数作相应的修改,大家有什么问题可以在这里提出来!</P> <P> </P> <P> </P></DIV> <DIV class=attachments> <UL> <LI><a href="http://dl.javaeye.com/topics/download/b4881dfe-b939-331d-9b91-44842c592098" target="_blank" ><FONT color=#108ac6>BeiJingTraining.rar</FONT></A> (605.9 KB)</LI> <LI>下载次数: 190</LI></UL></DIV> <UL> <LI><a href="http://dl.javaeye.com/topics/download/c07b232a-0f0e-3d76-89f4-27a8ce65a03b" target="_blank" ><FONT color=#108ac6>Province.rar</FONT></A> (2.2 MB)</LI> <LI>下载次数: 165</LI></UL> <P>原帖地址:<a href="http://junmail.javaeye.com/blog/222872" target="_blank" >http://junmail.javaeye.com/blog/222872</A></P> |
|
1楼#
发布于:2009-10-26 10:46
<img src="images/post/smile/dvbbs/em06.gif" />
|
|
2楼#
发布于:2009-11-29 12:24
谢谢分享,学习ArcGIS Server很好的例子
|
|