1 <?xml version="1.0" encoding="utf-8"?>
3 xmlns:mx="http://www.adobe.com/2006/mxml"
4 layout="vertical" showCloseButton="true"
5 horizontalAlign="center" title="Load vector file"
6 width="400" height="350" verticalGap="0">
8 <mx:DataGrid editable="true" width="100%" height="100%" id="dataGrid"
9 dataProvider="{vectorLayers}" itemEditEnd="dataEdited(event)">
11 <mx:DataGridColumn editable="false" dataField="visible" headerText="Show" width="45">
14 <mx:CheckBox selectedField="isSelected"
15 click="data.isSelected=!data.isSelected; this.parent.parent.dispatchEvent(new Event('visibility_changed'));"
20 <mx:DataGridColumn editable="true" dataField="name" headerText="Name"/>
21 <mx:DataGridColumn editable="false" dataField="url" headerText="URL"/>
22 <mx:DataGridColumn editable="false" dataField="style" headerText="Style">
26 selectedItem="{outerDocument.whichStyle(data)}"
27 change="outerDocument.styleEdited(data.name,selectedItem.url)"
28 dataProvider="{outerDocument.styles}"
35 <mx:HBox horizontalAlign="right" width="100%">
36 <mx:LinkButton label="Delete" click="removeLayer()" enabled="{dataGrid.selectedItem != null && dataGrid.selectedItem.isBackground}"/>
39 <mx:VBox width="100%" paddingTop="10">
40 <mx:Label htmlText="<b>Add new vector layer</b>" />
42 <mx:RadioButtonGroup id="filetype" />
43 <mx:RadioButton width="100%" groupName="filetype" value="gpx" id="gpx" label="GPX" selected="true" />
44 <mx:RadioButton width="100%" groupName="filetype" value="kml" id="kml" label="KML" />
45 <mx:RadioButton width="100%" groupName="filetype" value="osm" id="osm" label="OSM" />
46 <mx:RadioButton width="100%" groupName="filetype" value="shp" id="shp" label="Shapefile" />
49 <mx:Label text="Shapefile projection:" />
50 <mx:ComboBox id="projection">
52 <mx:Object label="Lat/long" data="" />
53 <mx:Object label="Ordnance Survey GB" data="EPSG:27700" />
54 <mx:Object label="NAD83" data="EPSG:4269" />
57 <mx:CheckBox width="100%" label="Simplify paths" selected="true" id="simplify" />
60 <mx:Text text="URL:"/>
61 <mx:TextInput width="100%" id="src" text="" />
62 <mx:Button label="Load" click="loadFiles(src.text, filetype.selectedValue.toString(), simplify.selected, projection.selectedItem.data);" enabled="{src.text == '' ? false : true}"/>
68 import net.systemeD.halcyon.Map;
69 import net.systemeD.halcyon.MapPaint;
70 import net.systemeD.halcyon.Globals;
71 import net.systemeD.halcyon.connection.Connection;
72 import net.systemeD.potlatch2.utils.Importer;
73 import net.systemeD.potlatch2.utils.GpxImporter;
74 import net.systemeD.potlatch2.utils.KmlImporter;
75 import net.systemeD.potlatch2.utils.ShpImporter;
76 import net.systemeD.potlatch2.utils.OsmImporter;
77 import net.systemeD.potlatch2.collections.Stylesheets;
78 import mx.managers.PopUpManager;
79 import mx.events.DataGridEvent;
80 import mx.events.CloseEvent;
81 import mx.core.Application;
82 import mx.collections.ArrayCollection;
83 import mx.controls.Alert;
87 public function styleEdited(name:String,stylesheet:String):void {
88 map.findLayer(name).setStyle(stylesheet);
92 public function init():void {
93 PopUpManager.addPopUp(this, Application(Application.application), true);
94 PopUpManager.centerPopUp(this);
95 this.addEventListener(CloseEvent.CLOSE, vectorDialog_close);
96 dataGrid.addEventListener("visibility_changed", toggleVisibility);
97 map = Globals.vars.root;
98 dispatchEvent(new Event("layers_changed"));
101 public function whichStyle(item:Object):Object {
102 for each (var style:Object in styles) {
103 if (style.url==item.style) { return style; }
108 [Bindable(event="bogus")]
109 public function get styles():ArrayCollection {
110 return new ArrayCollection(Stylesheets.instance().collection);
113 [Bindable(event="layers_changed")]
114 private function get vectorLayers():Array {
116 for each (var a:MapPaint in map.getLayers() ) {
117 v.push( { name:a.connection.name, visible:a.visible, url:a.connection.apiBase, style:a.style, isBackground:a.isBackground } );
122 private function toggleVisibility(event:Event):void {
123 map.findLayer(dataGrid.selectedItem.name).visible = !map.findLayer(dataGrid.selectedItem.name).visible;
126 private function dataEdited(event:DataGridEvent):void {
127 if (event.dataField=='name') {
129 var a:String=dataGrid.selectedItem.name;
130 var b:String=dataGrid.itemEditorInstance['text']; // this is, unbelievably, how you get data within an itemEditEnd event
131 if (a==b || map.findLayer(b)) {
132 event.preventDefault();
134 map.findLayer(a).connection.name = b;
136 dispatchEvent(new Event("layers_changed"));
140 private function removeLayer():void {
141 map.removeLayerByName(dataGrid.selectedItem.name);
142 dispatchEvent(new Event("layers_changed"));
145 private function vectorDialog_close(evt:CloseEvent):void {
146 PopUpManager.removePopUp(this);
149 private function loadFiles(url:String,type:String,simplify:Boolean,projection:String=""):void {
150 var names:Array=url.split('/'); var name:String=names[names.length-1];
151 var stylesheet:String="stylesheets/potlatch.css";
152 if (type=='gpx') { stylesheet="stylesheets/gpx.css"; }
154 var connection:Connection = new Connection(name, url, null, null);
156 var filesLoaded:Function = function(success:Boolean,message:String=null):void {
158 var paint:MapPaint = map.addLayer(connection, stylesheet);
159 paint.updateEntityUIs(false, false);
160 dispatchEvent(new Event("layers_changed"));
162 Alert.show(message, 'Error', mx.controls.Alert.OK);
167 var gpx:GpxImporter=new GpxImporter(connection, map, [url], filesLoaded, simplify);
168 } else if (type=='kml') {
169 var kml:KmlImporter=new KmlImporter(connection, map, [url], filesLoaded, simplify);
170 } else if (type=='osm') {
171 var osm:OsmImporter=new OsmImporter(connection, map, [url], filesLoaded, simplify);
174 we're currently hardcoding the projection values. We could populate this directly from
175 proj4as, or better still, parse the WKT in the .PRJ file:
176 http://trac.osgeo.org/proj4js/ticket/47
177 http://trac.osgeo.org/proj4js/changeset/1873
179 var re:RegExp=/.shp$/i; url=url.replace(re,'');
180 var shp:ShpImporter=new ShpImporter(connection,
182 [url+".shp",url+".shx",url+".dbf"], filesLoaded, simplify, projection);