All pastes #915930 Raw Edit

WMS with modestmaps.

public text v1 · immutable
#915930 ·published 2008-02-24 05:07 UTC
rendered paste body
// ModestWMS.as
// hacked together by brentp

package {
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    
    import com.modestmaps.core.MapExtent;
    import com.modestmaps.extras.MapControls;
    import com.modestmaps.Map;
    import com.modestmaps.mapproviders.yahoo.YahooHybridMapProvider;    

    public class ModestWMS extends Sprite {
                
        private var map:Map;
        private const PADDING:int = 20;

        
        public function ModestWMS() 
        {
        	// setup stage
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.addEventListener(Event.RESIZE, onResize);	            	   
            
            // create child components
            createChildren();    	
            onResize();
            
        }

        private function createChildren():void 
        {			


            var url:String = "http://128.32.8.100/SOD/sod.wms?REQUEST=getmap&VERSION=1.1.1&LAYERS=ca_parks&WIDTH=256&HEIGHT=256&STYLES=&FORMAT=image/png&SRS=EPSG:900913&BBOX="
            map = new Map(stage.stageWidth - 2 * PADDING
                        , stage.stageHeight - 2 * PADDING
                        , true
                        , new YahooHybridMapProvider(url)
                        );
                        // simple, working WMS code (over commerical mapproviders)

            map.x = map.y = PADDING;
	        
            addChild(map);
            map.addChild(new MapControls(map));	
            map.setExtent(new MapExtent(37.829853, 37.700121, -122.212601, -122.514725));
	        
        }

        private function onResize(event:Event = null):void 
        {
            var w:Number = stage.stageWidth - 2 * PADDING;
            var h:Number = stage.stageHeight - 2 * PADDING;
	    	
            map.x = map.y = PADDING;
            map.setSize(w, h);
        }
        
    }
}

/*888888888888888888888888888888888888888888888888888888888888888888888888888888888*/
// YahooHybridMapProvider.as

package com.modestmaps.mapproviders.yahoo
{
    import com.modestmaps.core.Coordinate;
    import com.modestmaps.io.MapProviderPaintThrottledRequest;
    import com.modestmaps.events.*;
    import com.modestmaps.geo.Location
    import com.modestmaps.geo.Transformation;
    import com.modestmaps.geo.MercatorProjection;
    import com.modestmaps.mapproviders.IMapProvider;
    import com.modestmaps.mapproviders.yahoo.AbstractYahooMapProvider;
    import flash.display.Sprite;
    
    /**
     * @author darren
     * $Id: YahooHybridMapProvider.as 447 2008-01-25 00:54:38Z tom $
     */
    public class YahooHybridMapProvider 
        extends AbstractYahooMapProvider 
        implements IMapProvider
    {

        private var __url:String = "";

        public function YahooHybridMapProvider(url:String=""){
            super();

            __url = url;

        }

        override public function toString():String
        {
            return "YAHOO_HYBRID";
        }
        
        /**
         * Yahoo sprites are 258x258 to deal with Flash pixel fudge, we mask and offset them by
         * one pixel so they show up correctly.
         */
        override public function paint(sprite:Sprite, coord:Coordinate):void 
        {
            var bg:Sprite = new Sprite();
            bg.name = "bg";
            sprite.addChild(bg);
            
            var overlay:Sprite = new Sprite();
            overlay.name = "overlay";
            sprite.addChild(overlay);



            var request:MapProviderPaintThrottledRequest = new MapProviderPaintThrottledRequest(bg, getBGTileUrl( coord ), coord );
            request.addEventListener(ThrottledRequestEvent.REQUEST_ERROR, onRequestError);
            request.addEventListener(ThrottledRequestEvent.RESPONSE_COMPLETE, onResponseComplete);
            request.addEventListener(ThrottledRequestEvent.RESPONSE_ERROR, onResponseError);
            request.send();
    
            request = new MapProviderPaintThrottledRequest(overlay, getOverlayTileUrl( coord ), coord );
            request.addEventListener(ThrottledRequestEvent.REQUEST_ERROR, onRequestError);
            request.addEventListener(ThrottledRequestEvent.RESPONSE_COMPLETE, onResponseComplete);
            request.addEventListener(ThrottledRequestEvent.RESPONSE_ERROR, onResponseError);
            request.send();

            if(__url != ''){
                var wms:Sprite = new Sprite();
                wms.name = "wms";
                sprite.addChild(wms);
                request = new MapProviderPaintThrottledRequest(wms, getWMSTileUrl( coord ), coord );
                request.addEventListener(ThrottledRequestEvent.REQUEST_ERROR, onRequestError);
                request.addEventListener(ThrottledRequestEvent.RESPONSE_COMPLETE, onResponseComplete);
                request.addEventListener(ThrottledRequestEvent.RESPONSE_ERROR, onResponseError);
                request.send();
                wms.x = wms.y = -.5;
            }

            bg.x = bg.y = -.5;
            overlay.x = overlay.y = -.5;
    
            createMask( sprite );       
        }   
    
        private function getBGTileUrl(coord:Coordinate):String
        {       
            return "http://us.maps3.yimg.com/aerial.maps.yimg.com/tile?v=1.7&t=a" + getZoomString(sourceCoordinate(coord));
        }
    
        private function getOverlayTileUrl(coord:Coordinate):String
        {       
            return "http://us.maps3.yimg.com/aerial.maps.yimg.com/png?v=2.2&t=h" + getZoomString(sourceCoordinate(coord));
        }

        /* 
            take a lat/lon location, and return a new location in mercator meters. suitable for use with projections:
            EPSG:54004 http://spatialreference.org/ref/epsg/54004/
            EPSG:900913 http://spatialreference.org/ref/epsg/900913/
            math is copied from here:
            http://search.cpan.org/src/DARNOLD/Geo-Mercator-1.01/lib/Geo/Mercator.pm
            and john deck's blog.
        */
        private function toMercator(loc:Location):Location {

            var sma:Number    = 6378137.0;
            var ecc:Number    = 0.0818191913108718138;

            var x:Number = loc.lon * sma * Math.PI / 180.;
            var y:Number = loc.lat > 89.5 ? 89.5 : 
                                  (loc.lat < -89.5 ? -89.5 : loc.lat) 
            y *= Math.PI / 180.;
            var l:Number = ecc * Math.sin(y);

            y = sma * Math.log(Math.tan((y + Math.PI / 2 ) / 2) * Math.pow( ((1 - l) / (1 + l)) , (ecc/2)));
            return new Location(y, x);

        }
        
        
        private function getWMSTileUrl(coord:Coordinate):String
        {       
            var row:int = coord.row;

            var up_left:Location = toMercator(coordinateLocation(sourceCoordinate(new Coordinate(row, coord.column, coord.zoom))));

            var down_right:Location = toMercator(coordinateLocation(sourceCoordinate(new Coordinate(row + 1, coord.column + 1, coord.zoom))));

            return __url + up_left.lon.toString()   + "," + down_right.lat.toString() + "," 
                + down_right.lon.toString() + "," + up_left.lat.toString();

        }


        private function getZoomString( coord:Coordinate ):String
        {       
            var row:Number = ( Math.pow( 2, coord.zoom ) /2 ) - coord.row - 1;
            return "&x=" + coord.column + "&y=" + row + "&z=" + (18 - coord.zoom);
        }   
    
        // Event Handlers
        
        override protected function onResponseComplete(event:ThrottledRequestEvent):void
        {
        }
    }
}