How to use @ideditor/country-coder - 10 common examples

To help you get started, we’ve selected a few @ideditor/country-coder examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github osmlab / osm-community-index / lib / isValidLocation.js View on Github external
function isValidLocation(location, features) {
  features = features || {};

  if (Array.isArray(location)) {   // a [lon,lat] coordinate pair?
    return !!(
      location.length === 2 && Number.isFinite(location[0]) && Number.isFinite(location[1]) &&
      location[0] >= -180 && location[0] <= 180 && location[1] >= -90 && location[1] <= 90
    );

  } else if (/^\S+\.geojson$/i.test(location)) {   // a .geojson filename?
    let featureId = location.replace('.geojson', '');
    return !!features[featureId];

  } else {    // a country-coder string?
    let ccmatch = CountryCoder.feature(location);
    return !!ccmatch;
  }
}
github openstreetmap / iD / modules / ui / fields / address.js View on Github external
wrap = selection.selectAll('.form-field-input-wrap')
            .data([0]);

        wrap = wrap.enter()
            .append('div')
            .attr('class', 'form-field-input-wrap form-field-input-' + field.type)
            .merge(wrap);

        if (_entity) {
            var countryCode;
            if (context.inIntro()) {
                // localize the address format for the walkthrough
                countryCode = t('intro.graph.countrycode');
            } else {
                var center = _entity.extent(context.graph()).center();
                countryCode = countryCoder.iso1A2Code(center);
            }
            if (countryCode) updateForCountryCode(countryCode);
        }
    }
github openstreetmap / iD / modules / validations / outdated_tags.js View on Github external
newTags['brand:wikipedia'] = newTags.wikipedia;
                delete newTags.wikipedia;
            }
            // I considered setting `name` and other tags here, but they aren't unique per wikidata
            // (Q2759586 -> in USA "Papa John's", in Russia "Папа Джонс")
            // So users will really need to use a preset or assign `name` themselves.
        }

        // try key/value|name match against name-suggestion-index
        if (newTags.name) {
            for (var i = 0; i < nsiKeys.length; i++) {
                var k = nsiKeys[i];
                if (!newTags[k]) continue;

                var center = entity.extent(graph).center();
                var countryCode = countryCoder.iso1A2Code(center);
                var match = nsiMatcher.matchKVN(k, newTags[k], newTags.name, countryCode && countryCode.toLowerCase());
                if (!match) continue;

                // for now skip ambiguous matches (like Target~(USA) vs Target~(Australia))
                if (match.d) continue;

                var brand = brands.brands[match.kvnd];
                if (brand && brand.tags['brand:wikidata'] &&
                    brand.tags['brand:wikidata'] !== entity.tags['not:brand:wikidata']) {
                    subtype = 'noncanonical_brand';

                    var keepTags = ['takeaway'].reduce(function(acc, k) {
                        if (newTags[k]) {
                            acc[k] = newTags[k];
                        }
                        return acc;
github openstreetmap / iD / modules / ui / fields / combo.js View on Github external
} else {
            input = container.selectAll('input')
                .data([0]);
        }

        input = input.enter()
            .append('input')
            .attr('type', 'text')
            .attr('id', 'preset-input-' + field.safeid)
            .call(utilNoAuto)
            .call(initCombo, selection)
            .merge(input);

        if (isNetwork && _entity) {
            var center = _entity.extent(context.graph()).center();
            var countryCode = countryCoder.iso1A2Code(center);
            _countryCode = countryCode && countryCode.toLowerCase();
        }

        input
            .on('change', change)
            .on('blur', change);

        input
            .on('keydown.field', function() {
                switch (d3_event.keyCode) {
                    case 13: // ↩ Return
                        input.node().blur(); // blurring also enters the value
                        d3_event.stopPropagation();
                        break;
                }
            });
github ENT8R / NotesReview / js / badges.js View on Github external
export function country(coordinates) {
  const feature = CountryCoder.feature([...coordinates].reverse());
  // Return no badge if the note is not inside a known country
  if (!feature) {
    return;
  }

  const emoji = [];
  for (const codePoint of feature.properties.emojiFlag) {
    emoji.push(codePoint.codePointAt(0).toString(16));
  }
  const url = `https://twemoji.maxcdn.com/v/latest/svg/${emoji.join('-')}.svg`;

  return `<span class="my-1">
            <img src="${url}" class="icon">
          </span>`;
}
github osmlab / osm-community-index / lib / locationToFeature.js View on Github external
properties: {
          m49: '001',
          wikidata: 'Q2',
          nameEn: 'World',
          id:  '001',
          area: 510072000
        },
        geometry: {
          type: 'Polygon',
          coordinates: [[[-180, -90], [-180, 90], [180, 90], [180, -90], [-180, -90]]]
        }
      }
      return { type: 'countrycoder', feature: world }

    } else {
      let feature = CountryCoder.aggregateFeature(location);
      if (feature) {
        feature.properties = feature.properties || {};
        if (!feature.properties.area) {                            // ensure area property
          const area = calcArea.geometry(feature.geometry) / 1e6;  // m² to km²
          feature.properties.area = Number(area.toFixed(2));
        }
        return { type: 'countrycoder', feature: feature };
      } else {
        return null;
      }
    }
  }
}
github openstreetmap / iD / modules / ui / fields / maxspeed.js View on Github external
.call(speedCombo)
            .merge(input);

        input
            .on('change', change)
            .on('blur', change);

        var loc;
        if (_entity.type === 'node') {
            loc = _entity.loc;
        } else {
            var childNodes = context.graph().childNodes(context.entity(_entity.id));
            loc = childNodes[~~(childNodes.length/2)].loc;
        }

        _isImperial = countryCoder.roadSpeedUnit(loc) === 'mph';

        unitInput = wrap.selectAll('input.maxspeed-unit')
            .data([0]);

        unitInput = unitInput.enter()
            .append('input')
            .attr('type', 'text')
            .attr('class', 'maxspeed-unit')
            .call(unitCombo)
            .merge(unitInput);

        unitInput
            .on('blur', changeUnits)
            .on('change', changeUnits);
github openstreetmap / iD / modules / ui / fields / localized.js View on Github external
function loadCountryCode() {
        var center = _entity.extent(context.graph()).center();
        var countryCode = countryCoder.iso1A2Code(center);
        _countryCode = countryCode && countryCode.toLowerCase();
    }
github openstreetmap / iD / modules / ui / fields / input.js View on Github external
.attr('placeholder', field.placeholder() || t('inspector.unknown'))
            .classed(field.type, true)
            .call(utilNoAuto)
            .merge(input);

        input
            .classed('disabled', !!isLocked)
            .attr('readonly', isLocked || null)
            .on('input', change(true))
            .on('blur', change())
            .on('change', change());


        if (field.type === 'tel' && _entity) {
            var center = _entity.extent(context.graph()).center();
            var countryCode = countryCoder.iso1A2Code(center);
            var format = countryCode && dataPhoneFormats[countryCode.toLowerCase()];
            if (format) {
                wrap.selectAll('#' + fieldID)
                    .attr('placeholder', format);
            }

        } else if (field.type === 'number') {
            var rtl = (textDirection === 'rtl');

            input.attr('type', 'text');

            var buttons = wrap.selectAll('.increment, .decrement')
                .data(rtl ? [1, -1] : [-1, 1]);

            buttons.enter()
                .append('button')
github openstreetmap / iD / modules / ui / preset_list.js View on Github external
function inputevent() {
            var value = search.property('value');
            list.classed('filtered', value.length);
            var entity = context.entity(_entityID);
            var results, messageText;
            if (value.length && entity) {
                var center = entity.extent(context.graph()).center();
                var countryCode = countryCoder.iso1A2Code(center);

                results = presets.search(value, geometry, countryCode && countryCode.toLowerCase());
                messageText = t('inspector.results', {
                    n: results.collection.length,
                    search: value
                });
            } else {
                results = context.presets().defaults(geometry, 36);
                messageText = t('inspector.choose');
            }
            list.call(drawList, results);
            message.text(messageText);
        }

@ideditor/country-coder

Convert longitude-latitude pairs to ISO 3166-1 codes quickly and locally

ISC
Latest version published 1 year ago

Package Health Score

45 / 100
Full package analysis