WME Data Store

Store objects as you pan the map to compile a list (i.e. cities, places, segments)

目前為 2014-09-26 提交的版本,檢視 最新版本

// ==UserScript==
// @name                WME Data Store
// @author				davielde
// @description         Store objects as you pan the map to compile a list (i.e. cities, places, segments)
// @include             https://www.waze.com/editor/*
// @include             https://www.waze.com/*/editor/*
// @include             https://editor-beta.waze.com/*
// @version             0.2
// @grant               none
// @namespace           https://greasyfork.org/users/5252
// ==/UserScript==


function bootstrapDS()
{
    var bGreasemonkeyServiceDefined = false;
    
    try {
        bGreasemonkeyServiceDefined = (typeof Components.interfaces.gmIGreasemonkeyService === "object");
    }
    catch (err) { /* Ignore */ }
    
    if (typeof unsafeWindow === "undefined" || ! bGreasemonkeyServiceDefined) {
        unsafeWindow    = ( function () {
            var dummyElem = document.createElement('p');
            dummyElem.setAttribute('onclick', 'return window;');
            return dummyElem.onclick();
        }) ();
    }
    
    setTimeout(initializeDS, 999);

}


function initializeDS()
{    

    Waze.map.events.register("moveend", Waze.map, getWMEData);
    Waze.map.events.register("zoomend", Waze.map, getWMEData);
    
}


function getBounds()
{
   	var currentBounds = Waze.map.getExtent();

    currentBounds.transform(new OpenLayers.Projection("EPSG:900913"),new OpenLayers.Projection("EPSG:4326"));
    //console.log('WME Data Store: Current bounds = Left ' + currentBounds.left + ', Right ' + currentBounds.right + ', Bottom ' + currentBounds.bottom + ', Top ' + currentBounds.top);//verify transform
    
    return currentBounds;
}


function getWMEData(){
    
    var locale = I18n.locale;
    var cityLabel = I18n.translations[locale].layers.name["cities"];
    var venueLabel = I18n.translations[locale].layers.name["landmarks"];
    var segmentLabel = I18n.translations[locale].layers.name["segments"];
    
    var currentBounds = getBounds();
    var center900913 = Waze.map.getCenter();
    var center4326 = center900913.transform(new OpenLayers.Projection("EPSG:900913"),new OpenLayers.Projection("EPSG:4326"));
    
    console.log('WME Data Store: new execution');
    
    var cityData = ['CountryID','Country','StateID','State','CityID'
                    ,'CityName','CityEnglishName','IsEmpty','Longitude','Latitude'];
    try{
        var currentCities = Waze.model.cities.additionalInfo;
        for(i=0; i<currentCities.length; i++)
      	{
            var stateObj = Waze.model.states.get(currentCities[i].stateID);
            var stateName = stateObj.name;
            var countryObj = Waze.model.countries.get(currentCities[i].countryID);
            var countryName = countryObj.name;
            
            //console.log('WME Data Store: countryID = ' + currentCities[i].countryID + ', stateID = ' + currentCities[i].stateID 
            //            + ', cityID = ' + currentCities[i].id + ', city name = ' + currentCities[i].name 
            //            + ', city english name = ' + currentCities[i].englishName + ', isEmpty = ' + currentCities[i].isEmpty);
            cityData.push('\n'+currentCities[i].countryID,countryName,currentCities[i].stateID,stateName
                          ,currentCities[i].id,currentCities[i].name,currentCities[i].englishName,currentCities[i].isEmpty,center4326);
      	} 
    }
    catch(e){
        console.log('WME Data Store: unable to process city list');
    }  
    
    //console.log('WME Data Store: ' + cityData);
    
    
    var venueData = ['fid','PlaceName','Brand','Lock','PrimaryCategory','Type'
                     ,'CreateDateTime','CreatedByName','UpdateDateTime'];
    try{
        var currentVenues = Waze.model.venues.additionalInfo;
        for(i=0; i<currentVenues.length; i++)
      	{
            if(currentVenues[i].geometry.CLASS_NAME == 'OpenLayers.Geometry.Point'){var venueType = 'Point'}else{var venueType = 'Area'}
            var lockRank = currentVenues[i].attributes.lockRank + 1;
            var brand = currentVenues[i].attributes.brand;
            var createdOn = new Date(parseInt(currentVenues[i].attributes.createdOn));
            var createdByUserObj = Waze.model.users.get(currentVenues[i].attributes.createdBy);
            var createdByName = createdByUserObj.name;
            var updatedOn = new Date(parseInt(currentVenues[i].attributes.updatedOn));
            
            venueData.push('\n'+currentVenues[i].fid,currentVenues[i].attributes.name,brand,lockRank,currentVenues[i].attributes.categories[0],venueType
                           ,createdOn,createdByName,updatedOn);
      	} 
    }
    catch(e){
        console.log('WME Data Store: unable to process venue list');
    }  
    
    
    var segmentData = ['SegmentID','CountryName','StateName','CityName','PrimaryStreetName','RoadType'
                       ,'FwdToll','ReverseToll','FwdDirection','ReverseDirection','Elevation','Rank','LengthInMeters'
                       ,'CreateDateTime','CreatedByName','CreatedByRank','UpdateDateTime','UpdatedByName','UpdatedByRank'];
    try{
        var currentSegments = Waze.model.segments.additionalInfo;
        for(i=0; i<currentSegments.length; i++)
      	{
            var segmentID = currentSegments[i].fid;
            var primaryStreetObj = Waze.model.streets.get(currentSegments[i].attributes.primaryStreetID);
            var primaryStreetName = primaryStreetObj.name;
            var segCityObj = Waze.model.cities.get(primaryStreetObj.cityID);
            var segCityName = segCityObj.name;
			var segStateObj = Waze.model.states.get(segCityObj.stateID);
            var segStateName = segStateObj.name;
            var segCountryObj = Waze.model.countries.get(segCityObj.countryID);
            var segCountryName = segCountryObj.name;
            var segRoadType = currentSegments[i].attributes.roadType;
            var segCreatedOn = new Date(parseInt(currentSegments[i].attributes.createdOn));
            var segCreatedByUserObj = Waze.model.users.get(currentSegments[i].attributes.createdBy);
            var segCreatedByName = segCreatedByUserObj.userName;
            var segCreatedByRank = segCreatedByUserObj.rank + 1;
            var segUpdatedOn = new Date(parseInt(currentSegments[i].attributes.updatedOn));
            var segUpdatedByUserObj = Waze.model.users.get(currentSegments[i].attributes.updatedBy);
            var segUpdatedByName = segUpdatedByUserObj.userName;
            var segUpdatedByRank = segUpdatedByUserObj.rank + 1;
            var segFwdToll = currentSegments[i].attributes.fwdToll;
            var segRevToll = currentSegments[i].attributes.revToll;
            var segFwdDirection = currentSegments[i].attributes.fwdDirection;
            var segRevDirection = currentSegments[i].attributes.revDirection;
            var segElevation = currentSegments[i].attributes.level;
            var segRank = currentSegments[i].attributes.lockRank + 1;
            var segLength = currentSegments[i].attributes.length;

            segmentData.push('\n'+segmentID,segCountryName,segStateName,segCityName,primaryStreetName,segRoadType
                             ,segFwdToll,segRevToll,segFwdDirection,segRevDirection,segElevation,segRank,segLength
                             ,segCreatedOn,segCreatedByName,segCreatedByRank,segUpdatedOn,segUpdatedByName,segUpdatedByRank);
      	} 
    }
    catch(e){
        console.log('WME Data Store: unable to process segment list');
    }  
    

    divJDS = document.createElement('div');
    divJDS.id = 'divJDS';
    divJDS.style.position = 'absolute';
    divJDS.style.bottom = '45px';
    divJDS.style.left = '425px';
   	divJDS.style.text = 'white';
    divJDS.style.backgroundColor = 'transparent';
    divJDS.style.borderWidth = '2px';
    divJDS.style.borderStyle = 'groove';
    divJDS.style.boxShadow = '1px 1px 1px Grey';
    divJDS.style.padding = '1px';
    divJDS.style.color = 'Silver';
    divJDS.innerHTML = 'Export ';
    document.body.appendChild(divJDS);

    var a = divJDS.appendChild(
        document.createElement('a')
    );
    a.id = cityLabel + ' ' + currentBounds.left + '_' + currentBounds.top + '_' + currentBounds.right + '_' + currentBounds.bottom;
    a.download = cityLabel + '_' + currentBounds.left + '_' + currentBounds.top + '_' + currentBounds.right + '_' + currentBounds.bottom + '.csv';
    a.href = 'data:text/csv;base64,' + btoa(cityData);
    a.style.color = 'Silver';
    a.innerHTML = cityLabel + ' ';
    
    /*var a = divJDS.appendChild(
        document.createElement('a')
    );
    a.id = venueLabel + ' ' + currentBounds.left + '_' + currentBounds.top + '_' + currentBounds.right + '_' + currentBounds.bottom;
    a.download = venueLabel + '_' + currentBounds.left + '_' + currentBounds.top + '_' + currentBounds.right + '_' + currentBounds.bottom + '.csv';
    a.href = 'data:text/csv;base64,' + btoa(venueData);
    a.style.color = 'Silver';
    a.innerHTML = venueLabel + ' ';
    */
            
    var a = divJDS.appendChild(
        document.createElement('a')
    );
    a.id = segmentLabel + ' ' + currentBounds.left + '_' + currentBounds.top + '_' + currentBounds.right + '_' + currentBounds.bottom;
    a.download = segmentLabel + '_' + currentBounds.left + '_' + currentBounds.top + '_' + currentBounds.right + '_' + currentBounds.bottom + '.csv';
    a.href = 'data:text/csv;base64,' + btoa(segmentData);
    a.style.color = 'Silver';
    a.innerHTML = segmentLabel + ' ';
}

bootstrapDS();