WME Data Store

Store objects when panning the map to compile lists for export (i.e. cities, places, segments)

2014-09-28 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

// ==UserScript==
// @name                WME Data Store
// @author		davielde
// @description         Store objects when panning the map to compile lists for export (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.3.1
// @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 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 zoom = Waze.map.zoom;
    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','NearLongitude','NearLatitude'
                   ];
    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.replace(/,/g, '.');
            var countryObj = Waze.model.countries.get(currentCities[i].countryID);
            var countryName = countryObj.name.replace(/,/g, '.');
            
            //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, ' + e);
    }  
    
    //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++)
      	{
            var venueFID = currentVenues[i].fid;
            var venueName = currentVenues[i].attributes.name.replace(/,/g, '.');
            if(currentVenues[i].geometry.CLASS_NAME == 'OpenLayers.Geometry.Point'){var venueType = 'Point'}else{var venueType = 'Area'}
            var venueLockRank = currentVenues[i].attributes.lockRank + 1;
            var venueBrand = currentVenues[i].attributes.brand;
            var venueCreatedOn = new Date(parseInt(currentVenues[i].attributes.createdOn));
            var venueCreatedByUserObj = Waze.model.users.get(currentVenues[i].attributes.createdBy);
            var venueCreatedByName = venueCreatedByUserObj.name;
            var venueUpdatedOn = ((currentVenues[i].attributes.updatedOn == null) ? "" : new Date(parseInt(currentVenues[i].attributes.updatedOn)));
            
            venueData.push('\n'+venueFID,venueName,venueBrand,venueLockRank,currentVenues[i].attributes.categories[0],venueType
                           ,venueCreatedOn,venueCreatedByName,venueUpdatedOn
                          );
      	} 
    }
    catch(e){
        console.log('WME Data Store: unable to process venue list, ' + e);
    }  
    
    //if(zoom >= 4){
        var segmentData = ['SegmentID','CountryName','StateName','CityName','PrimaryStreetName','RoadType','RoadTypeName'
                           ,'FwdToll','ReverseToll','FwdDirection','ReverseDirection','Elevation','Rank','LengthInMeters'
                           ,'CreateDateTime','CreatedByName','CreatedByRank','UpdateDateTime','UpdatedByName','UpdatedByRank'
                          ];
        try{
            var currentSegments = Waze.model.segments.additionalInfo;
            //var lastSegUpdate = Math.max.apply(Math,currentSegments.map(function(o){return parseInt(o.attributes.updatedOn);}));
            //var lastSegUpdateDateTime = new Date(lastSegUpdate);
            //console.log('WME Data Store: max test = ' + lastSegUpdateDateTime);
            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.replace(/,/g, '.');
                var segCityObj = Waze.model.cities.get(primaryStreetObj.cityID);
                var segCityName = segCityObj.name.replace(/,/g, '.');
                var segStateObj = Waze.model.states.get(segCityObj.stateID);
                var segStateName = segStateObj.name.replace(/,/g, '.');
                var segCountryObj = Waze.model.countries.get(segCityObj.countryID);
                var segCountryName = segCountryObj.name.replace(/,/g, '.');
                var segRoadType = currentSegments[i].attributes.roadType;
                var segRoadTypeName = I18n.translations[locale].segment.road_types[segRoadType];
                var segCreatedOn = new Date(parseInt(currentSegments[i].attributes.createdOn));
                var segCreatedByUserObj = Waze.model.users.get(currentSegments[i].attributes.createdBy);
                var segCreatedByName = segCreatedByUserObj.userName.replace(/,/g, '.');
                var segCreatedByRank = segCreatedByUserObj.rank + 1;
                var segUpdatedOn = ((currentSegments[i].attributes.updatedOn == null) ? "" : new Date(parseInt(currentSegments[i].attributes.updatedOn))); //segments with no updates will throw errors
                var segUpdatedByUserObj = ((currentSegments[i].attributes.updatedOn == null) ? "" : Waze.model.users.get(currentSegments[i].attributes.updatedBy));
                var segUpdatedByName = ((currentSegments[i].attributes.updatedOn == null) ? "" : segUpdatedByUserObj.userName.replace(/,/g, '.'));
                var segUpdatedByRank = ((currentSegments[i].attributes.updatedOn == null) ? "" : 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,segRoadTypeName
                                 ,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, ' + e);
        }  
    //}
    
    //console.log('WME Data Store: ' + segmentData);

    //Export div
    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 = '#F8F8F8';
    divJDS.innerHTML = 'Export ';
    document.body.appendChild(divJDS);
    
	//Export Cities link
    var a = divJDS.appendChild(
        document.createElement('a')
    );
    a.id = 'divJDS_' + 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 = '#F8F8F8';
    a.innerHTML = cityLabel + ' ';
    
    //Export Places link
    var a = divJDS.appendChild(
        document.createElement('a')
    );
    a.id = 'divJDS_' + 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 = '#F8F8F8';
    a.innerHTML = venueLabel + ' ';
    
    //Export Segments link
    var a = divJDS.appendChild(
        document.createElement('a')
    );
    a.id = 'divJDS_' + 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 = '#F8F8F8';
    a.innerHTML = segmentLabel + ' ';

}


function initializeDS()
{    
    
    //Waze.map.events.register("mergeend", Waze.map, getWMEData());
    Waze.map.events.register("moveend", Waze.map, getWMEData());
    //Waze.map.events.register("zoomend", Waze.map, getWMEData());
     
}

bootstrapDS();