网络分析(最近设施)

dojo.require("esri.tasks.closestfacility");

var closestFacilityTask, params;
var facilityPointGraphicsLayerName = "facilityPointGraphicsLayer";
var incidentPointGraphicsLayerName = "incidentPointGraphicsLayer";
var routeGraphicsLayerName = "routeGraphicsLayer";
var facilitiesGraphicsLayer = null;
var incidentsGraphicsLayer = null;
var routeGraphicLayer = null;
var mapOnClick_addIncidents_connect, mapOnClick_addFacilities_connect;

//安置点符号
function getFacilitySymbol()
{
    var facilityPointSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 20,
              new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([89,95,35]), 2), new dojo.Color([130,159,83,0.40])
            );
    return facilityPointSymbol;
}

//转移点符号
function getIncidentSymbol()
{
    var incidentPointSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 16,
              new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([89,95,35]), 2), new dojo.Color([130,159,83,0.40])
            );
    return incidentPointSymbol;
}

//路径符号
function getRouteSymbol()
{
    var routePolylineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([89,95,35]), 4.0);
    return routePolylineSymbol;
}

//安置点高亮符号
function getFacilityHighlightSymbol()
{
    var facilityHighlightSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 16,
              new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,255,0]), 2), new dojo.Color([255,0,0,0.40])
            );
    return facilityHighlightSymbol;
}

//转移点高亮符号
function getIncidentHighlightSymbol()
{    
    var highlightSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,255,255],0.25), 4.5);
    return highlightSymbol;
}

//路径高亮符号
function getRouteHighlightSymbol()
{
    var routePolylineSymbol = new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 4.0);
    return routePolylineSymbol;
}

//安置点图层
function getFacilityPointGraphicsLayer()
{
    if(facilitiesGraphicsLayer == null)
    {
        facilitiesGraphicsLayer = getGraphicsLayer(facilityPointGraphicsLayerName);
        var facilityPointSymbol = getFacilitySymbol();
        var facilityRenderer = new esri.renderer.SimpleRenderer(facilityPointSymbol);
        facilitiesGraphicsLayer.setRenderer(facilityRenderer);        
    }
    return facilitiesGraphicsLayer;
}

//转移点图层
function getIncidentPointGraphicsLayer()
{
    if(incidentsGraphicsLayer == null)
    {
        incidentsGraphicsLayer = getGraphicsLayer(incidentPointGraphicsLayerName);
        var incidentPointSymbol = getIncidentSymbol();
        var incidentsRenderer = new esri.renderer.SimpleRenderer(incidentPointSymbol);
        incidentsGraphicsLayer.setRenderer(incidentsRenderer);
        //高亮显示
        map.graphics.enableMouseEvents();
        dojo.connect(incidentsGraphicsLayer,"onMouseOver", function(evt){
            var graphic = evt.graphic;
            map.graphics.clear();
            var routeGraphicsLayer = getRouteGraphicsLayer();
            var facilityPointGraphicsLayer = getFacilityPointGraphicsLayer();
            for(var i=0; i<routeGraphicsLayer.graphics.length; i++)
            {
                var graphicRoute = routeGraphicsLayer.graphics[i];
                if(graphic.attributes && graphicRoute.attributes.IncidentID==graphic.attributes.ObjectID)
                {
                    var highlightSymbol = getRouteHighlightSymbol();
                    var highlightGraphic = new esri.Graphic(graphicRoute.geometry,highlightSymbol);            
                    map.graphics.add(highlightGraphic);
                    //
                    var FacilityID = graphicRoute.attributes.FacilityID;                    
                    for(var i=0; i<facilityPointGraphicsLayer.graphics.length; i++)
                    {
                        var graphicFacility = facilityPointGraphicsLayer.graphics[i];
                        if(graphicFacility.attributes && graphicFacility.attributes.ObjectID==FacilityID)
                        {
                            var facilityHighlightSymbol = getFacilityHighlightSymbol();
                            var highlightGraphic = new esri.Graphic(graphicFacility.geometry,facilityHighlightSymbol);            
                            map.graphics.add(highlightGraphic);
                        }
                    }
                }
            }
        });
        dojo.connect(incidentsGraphicsLayer,"onMouseOut", function(evt){
            //alert("OK");
            map.graphics.clear();
        });
    }
    return incidentsGraphicsLayer;
}

//路径图层
function getRouteGraphicsLayer()
{
    if(routeGraphicLayer == null)
    {
        routeGraphicLayer = getGraphicsLayer(routeGraphicsLayerName);
        var routePolylineSymbol = getRouteSymbol();
        var routeRenderer = new esri.renderer.SimpleRenderer(routePolylineSymbol);
        routeGraphicLayer.setRenderer(routeRenderer);
    }
    return routeGraphicLayer;
}


/********************************************/
//添加转移点
function addIncidents() {
    removeEventHandlers();
    mapOnClick_addIncidents_connect = dojo.connect(map, "onClick", addIncident);
}

//添加转移点事件响应
function addIncident(evt) {
    var inPoint = new esri.geometry.Point(evt.mapPoint.x,evt.mapPoint.y,map.spatialReference);
    var location = new esri.Graphic(inPoint);
    graphicsLayer = getIncidentPointGraphicsLayer();
    graphicsLayer.add(location);
    params.incidents.features.push(location);
}

//添除转移点
function clearIncidents() {
    removeEventHandlers();
    graphicsLayer = getIncidentPointGraphicsLayer();
    for (var i=params.incidents.features.length-1; i>=0; i--) {
        graphicsLayer.remove(params.incidents.features.splice(i, 1)[0]);
    }
    params.incidents.features = [];
}
  
/********************************************/
//添加安置点
function addFacilities() {
    removeEventHandlers();
    mapOnClick_addFacilities_connect = dojo.connect(map, "onClick", addFacility);
}

//添加安置点事件响应
function addFacility(evt) {
    var inPoint = new esri.geometry.Point(evt.mapPoint.x,evt.mapPoint.y,map.spatialReference);
    var location = new esri.Graphic(inPoint);
    graphicsLayer = getFacilityPointGraphicsLayer();
    graphicsLayer.add(location);
    params.facilities.features.push(location);
}

//添除安置点
function clearFacilities() {
    removeEventHandlers();
    graphicsLayer = getFacilityPointGraphicsLayer();
    for (var i=params.facilities.features.length-1; i>=0; i--) {
        graphicsLayer.remove(params.facilities.features.splice(i, 1)[0]);
    }
    params.facilities.features = [];
} 
/********************************************/ 
  
//取消添加安置点与转移点事件
function removeEventHandlers() {
    dojo.disconnect(mapOnClick_addIncidents_connect);
    dojo.disconnect(mapOnClick_addFacilities_connect);
}

//进行路径分析
function solveClosestFacility() {
    if(params.incidents.features.length<1) return;
    removeEventHandlers();
    /*
    params.facilities = [];
    closestFacilityTask.solve(params,completeCallback,errorCallback);
    */
    var geometryArray = [];
    for(var i=0; i<params.incidents.features.length; i++)
    {
        geometryArray.push(params.incidents.features[i].geometry);
    }
    var bufferParameters = new esri.tasks.BufferParameters();
    bufferParameters.distances = [4];    
    bufferParameters.bufferSpatialReference = map.spatialReference;    
    bufferParameters.outSpatialReference = map.spatialReference;
    bufferParameters.unit = esri.tasks.GeometryService.UNIT_KILOMETER;
    bufferParameters.geometries = geometryArray;
    geometryService.buffer(bufferParameters, function (bufferedGeometries){
        geometryService.union(bufferedGeometries, function (unionGeometry){
            //var urlArray = ["http://10.1.102.171:6080/arcgis/rest/services/jcxx/TranferRoad/MapServer/13"];
            var urlArray = config.operatorLayers.placement;
            var fieldsArray = [];
            var sql = "1=1";
            GisLayerAccess(urlArray, fieldsArray, sql, unionGeometry, function (resultData){
                var facilities = new esri.tasks.FeatureSet();
                facilities.features = resultData[0].features;    
                params.facilities = facilities;
                closestFacilityTask.solve(params,completeCallback,errorCallback);
            }, true, true, true);
        });
    });
    
    function completeCallback(solveResult)
    {
        //alert(Ext.encode(solveResult));
        var routeGraphicLayer = getRouteGraphicsLayer();        
        var facilityPointGraphicsLayer = getFacilityPointGraphicsLayer();
        var incidentPointGraphicsLayer = getIncidentPointGraphicsLayer();        
        //var directions = solveResult.directions;
        var routes = solveResult.routes;
        var facilities = solveResult.facilities;
        var incidents = solveResult.incidents;
        routeGraphicLayer.clear();
        for(var i=0; i<routes.length; i++)
        {
            var route = routes[i];
            routeGraphicLayer.add(route);
        }
        //
        facilityPointGraphicsLayer.clear();
        for(var i=0; i<facilities.length; i++)
        {
            var facility = facilities[i];
            facilityPointGraphicsLayer.add(facility);
        }
        //
        incidentPointGraphicsLayer.clear();
        for(var i=0; i<incidents.length; i++)
        {
            var incident = incidents[i];
            incidentPointGraphicsLayer.add(incident);
        }
        if(solveResult.messages.length > 0){
          //alert(solveResult.messages[0]);
        }
    }
    function errorCallback(err)
    {
        Ext.encode(err);
    }
}

//初始化网络分析功能
function initNetwork() {
    params = new esri.tasks.ClosestFacilityParameters();
    params.travelDirection = esri.tasks.NATravelDirection.TO_FACILITY;
    params.defaultTargetFacilityCount = 1;
    params.impedenceAttribute = "METERS";
    //params.impedenceAttributeName = "METERS";
    params.directionsTimeAttribute = "MINUTES";
    //params.directionsTimeAttributeName = "MINUTES";
    params.restrictUTurns = esri.tasks.NAUTurn.ALLOW_BACKTRACK;
    params.useHierarchy = false;
    params.returnDirections = false;
    params.returnRoutes = true;
    params.returnFacilities = true;
    params.returnIncidents = true;
    params.returnPointBarriers = false;
    params.returnPolygonBarriers = false;
    params.returnPolylineBarriers = false;
    //
    params.directionsLanguage = "en-US";
    params.directionsOutputType = "complete";
    params.outputLines = esri.tasks.NAOutputLine.TRUE_SHAPE_WITH_MEASURE;
    params.outputGeometryPrecisionUnits = "esriKilometers";
    params.directionsLengthUnits = "esriKilometers";
    params.timeOfDayUsage = "start";    
    //
    params.facilities = new esri.tasks.FeatureSet();
    params.facilities.features = [];
    //
    params.incidents = new esri.tasks.FeatureSet();
    params.incidents.features = [];
    //
    params.outSpatialReference = map.spatialReference;   
    closestFacilityTask = new esri.tasks.ClosestFacilityTask(config.map.network.closestFacility); 
    //("http://10.1.102.171:6080/arcgis/rest/services/jcxx/TranferRoad/NAServer/ClosestFacility");
}

//清除最近设施分析结果
function clearClosestFacilityResult() {
    map.graphics.clear();
    graphicsLayer = getFacilityPointGraphicsLayer();
    graphicsLayer.clear();
    graphicsLayer = getIncidentPointGraphicsLayer();
    graphicsLayer.clear();
    graphicsLayer = getRouteGraphicsLayer();
    graphicsLayer.clear();
    clearParams();
} 

//清除最近设施分析参数
function clearParams()
{
    params.facilities.features = [];
    params.incidents.features = [];
}
posted @ 2016-08-20 09:58  ParamousGIS  阅读(784)  评论(0编辑  收藏  举报