(转载)ArcGIS Flex API配合LibertyGISI加载SHP文件

本文转载自使用LibertyGIS在Flex中加载Shape数据http://www.dancecoder.com/2012/255.html和《ArcGIS Flex API加载SHP文件http://www.gisall.com/html/66/102766-4308.html

某些情况下,如果在WebGIS系统中能够加载本地Map数据,会给用户带来很大的方便。SHP是使用范围很广泛的数据格式,而且数据结构早已公开,已经存在很多能够对SHP文件进行读取操作的类库。我们今天借助LibertyGIS(http://www.libertyvanguard.com/,一个开源的Flex GIS Framework)来实现在浏览器端加载SHP数据。

首先来介绍下LibertyGIS,LibertyGIS是开源的shape数据展现框架(LibertyGIS is an open source flex mapping framework for displaying ShapeFiles),官网地址:http://code.google.com/p/liberty-gis/;该框架包括一个独立的Map对象(com.liberty.controls.Map)以及加载shape文件所需的shp文件相关对象( com.liberty.files.dbf.DBF; com.liberty.files.shapefile.ShapeFile),此外,LibertyGIS还提供了一个图层对象DataLayer(com.liberty.core.DataLayer)和自己的MapEvent事件。使用LibertyGIS能够很容易的展现我们客户端的shp数据。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
			   xmlns:local="*" xmlns:esri="http://www.esri.com/2008/ags"
			   xmlns:shapefile="com.liberty.files.shapefile.*">
	<fx:Script>
		<![CDATA[
			import com.esri.ags.Graphic;
			import com.esri.ags.SpatialReference;
			import com.esri.ags.geometry.MapPoint;
			import com.esri.ags.geometry.Polygon;
			import com.esri.ags.geometry.Polyline;
			import com.esri.ags.utils.WebMercatorUtil;
			import com.liberty.files.shapefile.IShapeFileContent;
			import com.liberty.files.shapefile.ShapeFileRecord;
			
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			private var sps:SimpleMarkerSymbol=new SimpleMarkerSymbol();
			private var sls:SimpleLineSymbol=new SimpleLineSymbol("solid");
			private var sfs:SimpleFillSymbol=new SimpleFillSymbol("solid",0xff0000,0.7,sls);
			private function loadShpData():void
			{
				shpFile.source="assets/data/cities.shp";
			}
			
			private function shpFileLoadComplete(event:Event):void
			{
				var graphics:ArrayCollection=new ArrayCollection();
				var records:Array=shpFile.records;
				for (var i:int=0; i < records.length; i++)
				{
					var record:ShapeFileRecord=records[i];
					var content:IShapeFileContent=record.content;
					var points:Vector.<Number>=content.getPoints();
					
					switch(shpFile.type)
					{
						case 1:
						{
							var mapPoint:MapPoint=new MapPoint(points[0], points[1],new SpatialReference(4326));
							if(map.spatialReference.wkid!=4326)
							{
								mapPoint=WebMercatorUtil.geographicToWebMercator(mapPoint) as MapPoint;
							}
							var graphic:Graphic=new Graphic(mapPoint);
							graphic.symbol=sps;
							graphics.addItem(graphic);
							graphicsLayer.add(graphic);
							break;
						}
						case 3:
						{
							var pArrLine:Array=new Array();
							for(var j:Number=0;j<points.length;j+=2)
							{
								var point:MapPoint=new MapPoint(points[j],points[j+1],new SpatialReference(4326));
								if(map.spatialReference.wkid!=4326)
								{
									point=WebMercatorUtil.geographicToWebMercator(point) as MapPoint;
								}
								pArrLine.push(point);
							}
							var polyline:Polyline=new Polyline();
							polyline.addPath(pArrLine);
							var grPolyline:Graphic=new Graphic(polyline);
							grPolyline.symbol=sls;
							graphics.addItem(grPolyline);
							graphicsLayer.add(grPolyline);
							break;
						}
						case 5:
						{
							var pArrPolygon:Array=new Array();
							for(var k:Number=0;k<points.length-1;k+=2)
							{
								var pPoint:MapPoint=new MapPoint(points[k],points[k+1],new SpatialReference(4326));
								if(map.spatialReference.wkid!=4326)
								{
									pPoint=WebMercatorUtil.geographicToWebMercator(pPoint) as MapPoint;
								}
								pArrPolygon.push(pPoint);
							}
							var polygon:Polygon=new Polygon();
							polygon.addRing(pArrPolygon);
							var grPolygon:Graphic=new Graphic(polygon);
							grPolygon.symbol=sfs;
							graphics.addItem(grPolygon);
							graphicsLayer.add(grPolygon);
							break;
						}
						default:
						{
							Alert.show("LibertyGIS不支持您的数据格式!","提示");
							return;
						}
					}
				}
				graphicsLayer.graphicProvider=graphics;
			}
			
			private function onShapeFileProgress(event:ProgressEvent):void
			{
				progressBar.setProgress((event.bytesLoaded / event.bytesTotal) * 100, 100);
				progressBar.label=int(event.bytesLoaded / 1000).toString() + "K";
			}
		]]>
	</fx:Script>
	<fx:Declarations>
		<shapefile:ShapeFile id="shpFile" complete="shpFileLoadComplete(event)"
							 progress="onShapeFileProgress(event)"/>
	</fx:Declarations>
	<esri:Map id="map">
		<esri:ArcGISTiledMapServiceLayer 
			url="http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer"/>
		<esri:GraphicsLayer id="graphicsLayer">
			<esri:symbol>
				<esri:SimpleMarkerSymbol color="0xff0000" size="5">
					<esri:outline>
						<esri:SimpleLineSymbol color="0x000000"/>
					</esri:outline>
				</esri:SimpleMarkerSymbol>
			</esri:symbol>
		</esri:GraphicsLayer>
	</esri:Map>
	<mx:HBox width="100%" backgroundColor="0xff0000">
		<s:Button label="Load SHP Data" click="loadShpData()"/>
		<mx:ProgressBar id="progressBar" minimum="0" maximum="100" mode="manual" color="0xFFFFFF"
						labelPlacement="right"/>
	</mx:HBox>
</s:Application>


效果如下


(转载)ArcGIS Flex API配合LibertyGISI加载SHP文件_第1张图片

你可能感兴趣的:((转载)ArcGIS Flex API配合LibertyGISI加载SHP文件)