Allow user to set coarseness of Douglas-Peucker
authorRichard Fairhurst <richard@systemeD.net>
Tue, 1 Nov 2011 11:34:55 +0000 (11:34 +0000)
committerRichard Fairhurst <richard@systemeD.net>
Tue, 1 Nov 2011 11:34:55 +0000 (11:34 +0000)
net/systemeD/potlatch2/options/OptionsDialog.mxml
net/systemeD/potlatch2/tools/Simplify.as

index 28a092c..33d45ec 100644 (file)
     import net.systemeD.halcyon.Map;
     import net.systemeD.halcyon.connection.Connection;
     import net.systemeD.halcyon.connection.StatusFetcher;
+
+       private var userState:SharedObject=SharedObject.getLocal("user_state");
     
     public function init():void {
         PopUpManager.addPopUp(this, Application(FlexGlobals.topLevelApplication), true);
         PopUpManager.centerPopUp(this);
         this.addEventListener(CloseEvent.CLOSE, optionsDialog_close);
 
-        var obj:SharedObject = SharedObject.getLocal("user_state");
-
         tbcheck.selected = FlexGlobals.topLevelApplication.toolbox.visible;
         cursorcheck.selected = FlexGlobals.topLevelApplication.theController.cursorsEnabled;
-        tigercheck.selected = obj.data['tiger_highlighted'];
+        tigercheck.selected = userState.data['tiger_highlighted'];
         latlongcheck.selected = FlexGlobals.topLevelApplication.coordsbox.visible;
                licencecheck.selected = Map(Globals.vars.root).editableLayer.connection.statusFetcher!=null;
+               simplifyslider.value = userState.data['simplify_tolerance'] ? userState.data['simplify_tolerance']/0.00001 : 5;
     }
     
     private function optionsDialog_close(evt:CloseEvent):void {
         Globals.vars.highlightTiger = tigercheck.selected;
         FlexGlobals.topLevelApplication.theMap.redraw();
 
-        var obj:SharedObject = SharedObject.getLocal("user_state");
-        obj.setProperty("tiger_highlighted",tigercheck.selected);
-        obj.flush();
+        userState.setProperty("tiger_highlighted",tigercheck.selected);
+        userState.flush();
     }
 
+       private function simplifySet():void {
+        userState.setProperty("simplify_tolerance",simplifyslider.value*0.00001);
+        userState.flush();
+       }
+
        private function licenceToggle():void {
                // ** FIXME: this is an inelegant patch for the short-term issue of highlighting licensing status
                var conn:Connection=Map(Globals.vars.root).editableLayer.connection;
@@ -53,7 +58,7 @@
                        // ** FIXME: needs to also switch map style
                }
        }
-
+       
               ]]>
   </fx:Script>
        <mx:CheckBox width="100%" label="Show toolbox" selected="true" id="tbcheck"
     <mx:CheckBox width="100%" label="Show licence status" selected="false" id="licencecheck"
         change="licenceToggle()" />
 
+       <mx:HBox width="100%" verticalAlign="middle">
+    <mx:Label text="Simplify accuracy: "/>
+       <mx:HSlider id="simplifyslider"
+               minimum="1" maximum="5"
+               dataTipPlacement="top" tickColor="black" labels="[High,Low]" 
+               snapInterval="1" tickInterval="1"
+               allowTrackClick="true" liveDragging="true"
+               change="simplifySet();"/>
+       </mx:HBox>
+
   <mx:ControlBar>
     <mx:Spacer width="100%"/>
     <mx:Button label="Ok" click="PopUpManager.removePopUp(this);" styleName="titleWindowButton" />
index 5e41447..3955aa2 100644 (file)
@@ -4,23 +4,30 @@ package net.systemeD.potlatch2.tools {
        import net.systemeD.halcyon.connection.Way;
        import net.systemeD.halcyon.connection.Node;
        import net.systemeD.halcyon.connection.MainUndoStack;
+       import flash.net.SharedObject;
 
        /** Tool to reduce the number of nodes in a way by filtering out the "least important" ones, using the Douglas-Peucker algorithm. */
        public class Simplify {
 
-               private static const TOLERANCE:Number=0.00005;
-
                /** Carries out simplification on a way, adding an entry to global undo stack.
                 * @param way Way to be simplified.
                 * @param map Map it belongs to, for computing offscreen-ness.
                 * @param keepOffscreen If true, don't delete any nodes that are not currently visible. 
+                * @param tolerance Curve tolerance.
                 * */
 
         /* FIXME this should take an action, and push the work onto that. Simplify is called from various places
         * so shouldn't be adding to the global undo stack */
                  
-               public static function simplify(way:Way, map:Map, keepOffscreen:Boolean):void {
+               public static function simplify(way:Way, map:Map, keepOffscreen:Boolean, tolerance:Number=NaN):void {
                        if (way.length<3) { return; }
+                       if (isNaN(tolerance)) {
+                               if (SharedObject.getLocal("user_state").data['simplify_tolerance']!=undefined) {
+                                       tolerance=Number(SharedObject.getLocal("user_state").data['simplify_tolerance']);
+                               } else {
+                                       tolerance=0.00005;
+                               }
+                       }
 
                        var action:CompositeUndoableAction = new CompositeUndoableAction("Simplify");
                        
@@ -46,7 +53,7 @@ package net.systemeD.potlatch2.tools {
                                // find furthest-out point
                                for (i=anchor+1; i<float; i+=1) {
                                        d=getDistance(xa,ya,xb,yb,l,way.getNode(i).lon,way.getNode(i).latp);
-                                       if (d>furthdist && d>TOLERANCE) { furthest=i; furthdist=d; }
+                                       if (d>furthdist && d>tolerance) { furthest=i; furthdist=d; }
                                }
                        
                                if (furthest==0) {