User:Teester/ClaimMaps.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
var entityID = mw.config.get( "wbEntityId" );
var claimmaps_latitude;
var claimmaps_longitude;

if (entityID) {
    $.ajax({
        datatype: "json",
        url: "https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=" + entityID
    })
    .done(function( data ) {
        claimmaps_addMaps(data);
    })
    .fail(function() {
        console.log( 'The ajax request failed.' );
    });
}

function claimmaps_addMaps(data) {
    
    if (data.entities[entityID].claims.P625) {
        console.log(data.entities[entityID].claims.P625)
        claimmaps_location = data.entities[entityID].claims.P625[0];
        try {
        	claimmaps_latitude = claimmaps_location.mainsnak.datavalue.value.latitude;
        	claimmaps_longitude = claimmaps_location.mainsnak.datavalue.value.longitude;
        } catch (e) {
        	// Uncaught TypeError: Cannot read property 'value' of undefined e.g. [[Q105807880]]
        	return;
        }
    }
    
    // P402: OSM Relation ID
    if (data.entities[entityID].claims.P402) {
         $.ajax({
            datatype: "json",
            url: "https://maps.wikimedia.org/geoshape?getgeojson=1&ids=" + entityID
        })
        .done(function( data ) {
            claimmaps_addGeoshape_P402(data);
        })
        .fail(function() {
            console.log( 'The ajax request failed.' );
        });
    }
    
    // P3896: geoshape
    if (data.entities[entityID].claims.P3896) {
        // get the geoshape from commons
        geoshape = data.entities[entityID].claims.P3896[0].mainsnak.datavalue.value;
        $.ajax({
            datatype: "json",
            url: "https://commons.wikimedia.org/w/api.php?action=jsondata&origin=*&formatversion=2&format=json&title=" + geoshape.substring(5)
        })
        .done(function( data ) {
            claimmaps_addGeoshape_P3896(data);
        })
        .fail(function() {
            console.log( 'The ajax request failed.' );
        });
    }
}

/**
 * Adds a kartographer mapframe with OSM relation data 
 **/
function claimmaps_addGeoshape_P402(data) {

    propertyDivP402 = document.getElementById("P402");
    $(propertyDivP402).find(".wikibase-snakview-body").append("<div class='mapP402'/>");
    var divP402 = $(propertyDivP402).find(".mapP402");
    
    mw.loader.using( [ 'ext.kartographer.box' ], function () {

        var kartoBox = mw.loader.require( 'ext.kartographer.box' ), map;

        map = kartoBox.map( {
            container: $(divP402 ).empty().css( { height: 250 } )[ 0 ],
            center: [ claimmaps_latitude, claimmaps_longitude ],
            zoom: 6,
            allowFullScreen: false,
            data: [data]

        } );
    } );
}

/**
 * Adds a kartographer mapframe with geoshape data 
 **/
function claimmaps_addGeoshape_P3896(data) {
    propertyDivP3896 = document.getElementById("P3896");
    $(propertyDivP3896).find(".wikibase-snakview-body").append("<div class='mapP3896'/>");
    var divP3896 = $(propertyDivP3896).find(".mapP3896");
    if (data.jsondata.data.features) {
        geojson = data.jsondata.data.features;
    } else {
        geojson = [data.jsondata.data];
    }
    mw.loader.using( [ 'ext.kartographer.box' ], function () {

        var kartoBox = mw.loader.require( 'ext.kartographer.box' ), map;

        map = kartoBox.map( {
            container: $(divP3896 ).empty().css( { height: 250 } )[ 0 ],
            center: [ data.jsondata.latitude, data.jsondata.longitude ],
            zoom: data.jsondata.zoom,
            allowFullScreen: false,
            data : geojson
        } );
    } );
}