zoukankan      html  css  js  c++  java
  • ArcGIS Server for Flex 常用代码

    java

    将json格式的xy值转为点对象
     1     private IGeometry getGeometryFromXYs(String xys) {
    2 IGeometry geo = null;
    3 try {
    4 if (xys == null)
    5 return null;
    6 JSONArray jsonArray = new JSONArray(xys);
    7 int count = jsonArray.length();
    8 if (count < 1) {
    9 return null;
    10 } else if (count == 1) { // point
    11
    12 try {
    13 JSONObject jo = jsonArray.getJSONObject(0);
    14 Point p = new Point();
    15 p.setX(jo.getDouble("x"));
    16 p.setY(jo.getDouble("y"));
    17 return (IGeometry) p;
    18 } catch (AutomationException e) {
    19 e.printStackTrace();
    20 return null;
    21 } catch (IOException e) {
    22 e.printStackTrace();
    23 return null;
    24 }
    25
    26 }
    27
    28 for (int i = 0; i < jsonArray.length(); i++) {
    29 System.out.println("item " + i + " :" + jsonArray.getString(i));
    30
    31 }
    32 } catch (JSONException e) {
    33 e.printStackTrace();
    34 }
    35 return null;
    36 }
    初始化server
     1     public void initServer() {
    2 /** * 连接到ArcGIS Sever */
    3 try {
    4 new ServerInitializer().initializeServer(domain, user, password);
    5 conn = new ServerConnection();
    6 conn.connect(host);
    7 som = conn.getServerObjectManager();
    8 serverContext = som.createServerContext(servicesName, "MapServer");
    9 mapserver = (MapServer) serverContext.getServerObject();
    10 maps = mapserver.getMap(mapserver.getDefaultMapName());
    11 System.out.print("已连接到ArcGIS Sever");
    12 } catch (Exception e) {
    13 e.printStackTrace();
    14 mapserver = null;
    15 maps = null;
    16 som = null;
    17 }
    18 }
    新增feature
     1     /* 
    2 * 执行添加feature的操作
    3 * tablename:表名
    4 * map:待保存的feature各属性键值对
    5 * geo:待保存的feature的geometry
    6 */
    7 private boolean StoreFeature(String tablename, Map<String, String> map,
    8 IGeometry geo) {
    9 // 先判断Server的连接状态
    10 if (maps == null || mapserver == null || som == null
    11 || serverContext == null || conn == null)
    12 this.initServer();
    13 if (maps == null || mapserver == null || som == null
    14 || serverContext == null || conn == null) {
    15 System.out.print("不能连接到ArcGIS Server!");
    16 return false;
    17 }
    18 try {
    19 IFeatureLayer pFeatLayer = (IFeatureLayer) serverContext
    20 .createObject(FeatureLayer.getClsid());
    21 pFeatLayer = getFeatureLayer(maps, tablename);
    22
    23 IFeatureClass pfeatureclass = pFeatLayer.getFeatureClass();
    24 geo.setSpatialReferenceByRef(maps.getSpatialReference());
    25 IDataset dataset = new IDatasetProxy(pfeatureclass);
    26
    27 // begin edit
    28 IWorkspaceEdit pWorkspaceEdit = new IWorkspaceEditProxy(dataset
    29 .getWorkspace());
    30 pWorkspaceEdit.startEditing(true);
    31 pWorkspaceEdit.startEditOperation();
    32
    33 // set field value
    34 IFeatureBuffer featureBuffer = pfeatureclass.createFeatureBuffer();
    35 IFeatureCursor featureCursor = new FeatureCursor(pfeatureclass
    36 .IFeatureClass_insert(true));
    37 featureBuffer.setShapeByRef(geo);
    38
    39 if (map != null) {
    40 for (Object o : map.keySet()) {
    41 String field = String.valueOf(o);
    42 int index = featureBuffer.getFields().findField(field);
    43 if (index >= 0) {
    44 // do not store objectid
    45 if (field.indexOf("OBJECTID") == -1) {
    46 featureBuffer.setValue(featureBuffer.getFields()
    47 .findField(field), map.get(o));
    48 }
    49 }
    50 }
    51 }
    52 featureCursor.insertFeature(featureBuffer);
    53 featureCursor.flush();
    54 pWorkspaceEdit.stopEditOperation();
    55 pWorkspaceEdit.stopEditing(true);
    56 System.out.print("\n已添加!");
    57 Cleaner.release(featureBuffer);// release Point of AO
    58 mapserver.refreshServerObjects();
    59 serverContext.releaseContext();
    60 } catch (Exception e) {
    61 e.printStackTrace();
    62 try {
    63 mapserver.refreshServerObjects();
    64 serverContext.releaseContext();
    65 } catch (AutomationException e1) {
    66 e1.printStackTrace();
    67 } catch (IOException e1) {
    68 e1.printStackTrace();
    69 } finally {
    70 mapserver = null;
    71 maps = null;
    72 som = null;
    73 return false;
    74 }
    75 }
    76 return true;
    77 }
    删除feature
     1     //delete single feature
    2 public boolean deleteFeature(String tablename,int id ){
    3 // 先判断Server的连接状态
    4 if (maps == null || mapserver == null || som == null
    5 || serverContext == null || conn == null)
    6 this.initServer();
    7 if (maps == null || mapserver == null || som == null
    8 || serverContext == null || conn == null) {
    9 System.out.print("不能连接到ArcGIS Server!");
    10 return false;
    11 }
    12 try {
    13
    14 IFeatureLayer pFeatLayer = (IFeatureLayer) serverContext
    15 .createObject(FeatureLayer.getClsid());
    16 pFeatLayer = getFeatureLayer(maps, tablename);
    17
    18 IFeatureClass pfeatureclass = pFeatLayer.getFeatureClass();
    19 IDataset dataset = new IDatasetProxy(pfeatureclass);
    20 // begin edit
    21 IWorkspaceEdit pWorkspaceEdit = new IWorkspaceEditProxy(dataset
    22 .getWorkspace());
    23 pWorkspaceEdit.startEditing(true);
    24 pWorkspaceEdit.startEditOperation();
    25
    26 IFeature feature=pfeatureclass.getFeature(id);
    27 if(feature!=null)
    28 feature.delete();
    29 pWorkspaceEdit.stopEditOperation();
    30 pWorkspaceEdit.stopEditing(true);
    31 System.out.print("\n已删除!");
    32 mapserver.refreshServerObjects();
    33 serverContext.releaseContext();
    34 } catch (Exception e) {
    35 e.printStackTrace();
    36 try {
    37 mapserver.refreshServerObjects();
    38 serverContext.releaseContext();
    39 } catch (AutomationException e1) {
    40 // TODO Auto-generated catch block
    41 e1.printStackTrace();
    42 } catch (IOException e1) {
    43 // TODO Auto-generated catch block
    44 e1.printStackTrace();
    45 } finally {
    46 mapserver = null;
    47 maps = null;
    48 som = null;
    49
    50 return false;
    51 }
    52 }
    53 return true;
    54 }
    更新feature属性
     1     //update fields(without geometry)
    2 public boolean updateFields(String tablename,int id,Map<String, String> map){
    3 // 先判断Server的连接状态
    4 if (maps == null || mapserver == null || som == null
    5 || serverContext == null || conn == null)
    6 this.initServer();
    7 if (maps == null || mapserver == null || som == null
    8 || serverContext == null || conn == null) {
    9 System.out.print("不能连接到ArcGIS Server!");
    10 return false;
    11 }
    12 try {
    13 IFeatureLayer pFeatLayer = (IFeatureLayer) serverContext
    14 .createObject(FeatureLayer.getClsid());
    15 pFeatLayer = getFeatureLayer(maps, tablename);
    16
    17 IFeatureClass pfeatureclass = pFeatLayer.getFeatureClass();
    18 IDataset dataset = new IDatasetProxy(pfeatureclass);
    19
    20 // begin edit
    21 IWorkspaceEdit pWorkspaceEdit = new IWorkspaceEditProxy(dataset
    22 .getWorkspace());
    23 pWorkspaceEdit.startEditing(true);
    24 pWorkspaceEdit.startEditOperation();
    25
    26 IFeature feature=pfeatureclass.getFeature(id);
    27 if (map != null) {
    28 for (Object o : map.keySet()) {
    29 String field = String.valueOf(o);
    30 int index = feature.getFields().findField(field);
    31 if (index >= 0) {
    32 // do not store objectid
    33 if (field.indexOf("OBJECTID") == -1) {
    34 feature.setValue(feature.getFields()
    35 .findField(field), map.get(o));
    36 }
    37 }
    38 }
    39 }
    40 feature.store();
    41 pWorkspaceEdit.stopEditOperation();
    42 pWorkspaceEdit.stopEditing(true);
    43 System.out.print("\n操作成功!");
    44 mapserver.refreshServerObjects();
    45 serverContext.releaseContext();
    46 } catch (Exception e) {
    47 e.printStackTrace();
    48 try {
    49 mapserver.refreshServerObjects();
    50 serverContext.releaseContext();
    51 } catch (AutomationException e1) {
    52 // TODO Auto-generated catch block
    53 e1.printStackTrace();
    54 } catch (IOException e1) {
    55 // TODO Auto-generated catch block
    56 e1.printStackTrace();
    57 } finally {
    58 mapserver = null;
    59 maps = null;
    60 som = null;
    61
    62 return false;
    63 }
    64 }
    65 return true;
    66 }



    flex

    得到Geometry的n倍范围
     1         //get N * size extent from geometry
    2 public static function getNExtentFromGeo(geo:Geometry,n:Number):Extent{
    3 var extent:Extent=getExtentFromGeo(geo);
    4 var newExtent:Extent=new Extent();
    5 newExtent.xmax=extent.xmax+extent.width*(n-1)/2;
    6 newExtent.xmin=extent.xmin-extent.width*(n-1)/2;
    7 newExtent.ymax=extent.ymax+extent.height*(n-1)/2;
    8 newExtent.ymin=extent.ymin-extent.height*(n-1)/2;
    9 return newExtent;
    10 }
    得到点的缓冲范围
    1         //get extent from geometry
    2 public static function getBuffExtentOfPoint(point:MapPoint,buffer:Number):Extent{
    3 var extent:Extent=new Extent(point.x-buffer,point.y-buffer,point.x+buffer,point.y+buffer);
    4 return extent;
    5 }
    得到geometry的范围
     1         //get extent from geometry
    2 public static function getExtentFromGeo(geo:Geometry):Extent{
    3 var extent:Extent=new Extent();
    4 switch (geo.type) {
    5 case Geometry.MAPPOINT: {
    6 extent=getBuffExtentOfPoint(geo as MapPoint,0.01);
    7 break;
    8 }
    9 case Geometry.POLYLINE: {
    10 var polyline:Polyline=geo as Polyline;
    11 extent=polyline.extent;
    12 break;
    13 }
    14 case Geometry.POLYGON: {
    15 var poly:Polygon=geo as Polygon;
    16 extent=poly.extent;
    17 break;
    18 }
    19 }
    20 return extent;
    21 }
    得到整个graphicLayer的图形范围
     1         //get graphicslayer's extent
    2 public static function getGraphicsExtent(graphicsLayer:GraphicsLayer):Extent{
    3
    4 var extent:Extent=null;
    5 var tempExtent:Extent=null;
    6 var graphic:Graphic=null;
    7 var geo:Geometry=null;
    8 for(var i:int=0;i<graphicsLayer.numGraphics;i++)
    9 {
    10 graphic=Graphic(graphicsLayer.getChildAt(i));
    11 geo=graphic.geometry;
    12 tempExtent=getNExtentFromGeo2(geo,10);
    13 if(extent==null){
    14 extent=tempExtent;
    15 }else{
    16 extent=extent.union(tempExtent);
    17 }
    18 }
    19
    20 return extent;
    21 }
    得到几何对象的中心点
     1         //得到几何对象的中心点
    2 public static function getGeomCenter(geom:Geometry):MapPoint
    3 {
    4 var pt:MapPoint;
    5 switch (geom.type)
    6 {
    7 case Geometry.MAPPOINT:
    8 {
    9 pt = geom as MapPoint;
    10 break;
    11 }
    12
    13 case Geometry.POLYLINE:
    14 {
    15 var pl:Polyline = geom as Polyline;
    16 pt = pl.extent.center;
    17 if(pl.paths != null && pl.paths.length>0){
    18 var path:Array = pl.paths[0];
    19 if(path != null && path.length > 0){
    20 var mid:int = path.length/2;
    21 pt = path[mid];
    22 if(path.length == 2){ // path里面只有两个点的情况
    23 //pt.x = pl.getPoint(0,path[0]).x;
    24 var x1:Number = (path[0] as MapPoint).x;
    25 var y1:Number = (path[0] as MapPoint).y;
    26 var x2:Number = (path[1] as MapPoint).x;
    27 var y2:Number = (path[1] as MapPoint).y;
    28 pt.x = x2 + (x1 - x2)/2 ;
    29 pt.y = y2 + (y1 - y2)/2;
    30 }
    31 }
    32 }
    33
    34 //pl.getPoint(pl.paths[0],pl.paths[0][]);
    35 break;
    36 }
    37
    38 case Geometry.POLYGON:
    39 {
    40 var poly:Polygon = geom as Polygon;
    41 pt = poly.extent.center;
    42 break;
    43 }
    44 }
    45 return pt;
    46 }
    根据包含坐标的json字符串生成几何对象
     1         //根据包含坐标的json字符串生成几何对象
    2 public static function getGeoFromXYs(xys:String):Geometry{
    3
    4 var geo:Geometry=null;
    5
    6 //对xys进行解析,转换为geometry
    7 var data:String = xys.replace( /\s/g, '' );
    8 var collection:ArrayCollection = new ArrayCollection(JSON.decode(data) as Array);
    9 var count:int=collection.length;
    10 if(count<1){ //no xy information
    11 Alert.show("无坐标信息!");
    12 }else if(count==1){ //point
    13
    14 var point:MapPoint=new MapPoint(collection[0].x,collection[0].y);
    15 geo=point as Geometry;
    16 }else if(count>1){
    17 if(collection[0].x==collection[count-1].x && collection[0].y==collection[count-1].y ){ //polygon
    18 var paths:Array=new Array();
    19 var path:Array=new Array();
    20 for(var i:int=0;i<=count-1;i++){
    21 var p:MapPoint=new MapPoint(collection[i].x,collection[i].y);
    22 path.push(p);
    23 }
    24 paths.push(path);
    25 var po:Polygon=new Polygon(paths,null);
    26 geo=po as Geometry;
    27 }else{ //line
    28 var paths:Array=new Array();
    29 var path:Array=new Array();
    30 for(var i:int=0;i<=count-1;i++){
    31 var p:MapPoint=new MapPoint(collection[i].x,collection[i].y);
    32 path.push(p);
    33 }
    34 paths.push(path);
    35 var line:Polyline=new Polyline(paths,null);
    36 geo=line as Geometry;
    37 }
    38 }
    39 return geo;
    40 }
    从json字符串中加载graphics
     1         //add graphics from jsonString
    2 public static function addGraphicsFromJsonString(glayer:GraphicsLayer,jsonString:String):void{
    3
    4 var data:String = jsonString.replace( /\s/g, '' );
    5 var obj:Object = JSON.decode(data) as Object;
    6 var tableName:String=obj.tablename;//tablename
    7 var type:int=obj.type;//0:point 1:polyline 2:polygon
    8 var id:int=obj.id; // polyline||polygon 's id
    9 var xys:Array=obj.geodata; //xy json array
    10 var count:int=xys.length;
    11 var i:int=0;
    12 var geo:Geometry=null;
    13 var graphic:Graphic=null;
    14 var infoData:Object =null;//store info data
    15
    16 if(type==0){//points
    17 for(i=0;i<count;i++){
    18 var point:MapPoint=new MapPoint(xys[i].x,xys[i].y);
    19 geo=point as Geometry;
    20 graphic=getGraphicFromGeo(geo);
    21 infoData =
    22 {
    23 tablename: tableName,
    24 id:xys[i].id
    25 };
    26 graphic.attributes=infoData;
    27 glayer.add(graphic);
    28 }
    29 }else if(type==1){//polyline
    30 var paths:Array=new Array();
    31 var path:Array=new Array();
    32 for(i=0;i<=count-1;i++){
    33 var p:MapPoint=new MapPoint(xys[i].x,xys[i].y);
    34 path.push(p);
    35 }
    36 paths.push(path);
    37 var polyline:Polyline=new Polyline(paths,null);
    38 geo=polyline as Geometry;
    39
    40 infoData =
    41 {
    42 tablename: tableName,
    43 id:id
    44 };
    45 graphic=getGraphicFromGeo(geo);
    46 graphic.attributes=infoData;
    47 glayer.add(graphic);
    48 }else if(type==2){//polygon
    49 var paths1:Array=new Array();
    50 var path1:Array=new Array();
    51 for(i=0;i<=count-1;i++){
    52 var p1:MapPoint=new MapPoint(xys[i].x,xys[i].y);
    53 path1.push(p1);
    54 }
    55 paths1.push(path1);
    56 var polygon:Polygon=new Polygon(paths1,null);
    57 geo=polygon as Geometry;
    58
    59 infoData =
    60 {
    61 tablename: tableName,
    62 id:id
    63 };
    64 graphic=getGraphicFromGeo(geo);
    65 graphic.attributes=infoData;
    66 glayer.add(graphic);
    67 }
    68 }
    根据几何对象生成渲染后的图形
     1         //根据几何对象生成渲染后的图形
    2 public static function getGraphicFromGeo(geo:Geometry):Graphic{
    3
    4 var sls:SimpleLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, 0xFF0000, 0.8,4);
    5 var sms:SimpleMarkerSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_CIRCLE, 30, 0xFF0000, 0.5);
    6 var sfs:SimpleFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, 0xFF0000, 0.1);
    7 sfs.outline = new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, 0xFF0000, 1.0, 2);
    8
    9 var g:Graphic=null;
    10 switch (geo.type)
    11 {
    12 case Geometry.MAPPOINT: {
    13 g=new Graphic(geo,sms,null);
    14 break;
    15 }
    16 case Geometry.POLYLINE: {
    17 g=new Graphic(geo,sls,null);
    18 break;
    19 }
    20 case Geometry.POLYGON: {
    21 g=new Graphic(geo,sfs,null);
    22 break;
    23 }
    24 }
    25 return g;
    26 }
    用点对graphicLayer进行查询
     1         //query graphic by point
    2 public static function queryGraphicByPoint(graphicsLayer:GraphicsLayer,point:MapPoint): Graphic {
    3 var extent:Extent = getBuffExtentOfPoint(point,0.02);
    4 var graphic: Graphic= null;
    5 var geo:Geometry=null;
    6 var tempExtent:Extent=null;
    7
    8 for(var i:int=0;i<graphicsLayer.numGraphics;i++)
    9 {
    10 graphic=Graphic(graphicsLayer.getChildAt(i));
    11 geo=graphic.geometry;
    12 tempExtent=getNExtentFromGeo(geo,3);
    13 if(ifTwoExtentsIntersect(extent,tempExtent)){
    14 return graphic;
    15 }
    16 }
    17 return null;
    18 }
    判断两个范围是否相交
     1        //judge if two extents intersect
    2 public static function ifTwoExtentsIntersect(extent1:Extent,extent2:Extent):Boolean{
    3
    4 var point11:MapPoint=new MapPoint(extent1.xmin,extent1.ymin,null);
    5 var point12:MapPoint=new MapPoint(extent1.xmax,extent1.ymin,null);
    6 var point13:MapPoint=new MapPoint(extent1.xmin,extent1.ymax,null);
    7 var point14:MapPoint=new MapPoint(extent1.xmax,extent1.ymax,null);
    8 var point21:MapPoint=new MapPoint(extent2.xmin,extent2.ymin,null);
    9 var point22:MapPoint=new MapPoint(extent2.xmax,extent2.ymin,null);
    10 var point23:MapPoint=new MapPoint(extent2.xmin,extent2.ymax,null);
    11 var point24:MapPoint=new MapPoint(extent2.xmax,extent2.ymax,null);
    12 if( ifPointInExtent(point11,extent2)||ifPointInExtent(point12,extent2)||
    13 ifPointInExtent(point13,extent2)||ifPointInExtent(point14,extent2)||
    14 ifPointInExtent(point21,extent1)||ifPointInExtent(point22,extent1)||
    15 ifPointInExtent(point23,extent1)||ifPointInExtent(point24,extent1)){
    16
    17 return true;
    18 }
    19
    20 return false;
    21 }
    判断点是否在范围内
    1        //judge if point in extent
    2 public static function ifPointInExtent(point:MapPoint,extent:Extent):Boolean{
    3 //Alert.show("point.x="+point.x+"extent.xmax="+extent.xmax+"extent.xmin="+extent.xmin+
    4 //"point.y="+point.y+"extent.ymax="+extent.ymax+"extent.ymin="+extent.ymin);
    5 if(point.x<extent.xmax&&point.x>extent.xmin&&point.y<extent.ymax&&point.y>extent.ymin){
    6 return true;
    7 }
    8 return false;
    9 }














    end

  • 相关阅读:
    Reactor系列(四)subscribe订阅
    Reactor系列(三)创建Flux,Mono(续)
    Reactor系列(二)Flux Mono创建
    Reactor系列(一)基本概念
    Stream系列(十五)File方法使用
    Stream系列(十四)parallet方法使用
    OpenCV二值化、归一化操作
    C# 队列
    linux shell脚本程序路径作为变量
    C++中头文件(.h)和源文件(.cpp)都应该写些什么
  • 原文地址:https://www.cnblogs.com/myparamita/p/2361346.html
Copyright © 2011-2022 走看看