Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active June 29, 2021 22:43
Show Gist options
  • Select an option

  • Save ThomasG77/0f103baa5001934bf981bb7855d60a5e to your computer and use it in GitHub Desktop.

Select an option

Save ThomasG77/0f103baa5001934bf981bb7855d60a5e to your computer and use it in GitHub Desktop.
from pathlib import Path
import PythonMagick
files = Path('.').glob('openstreetmap-legend-*.png')
for f in sorted(files):
img = PythonMagick.Image(f"{f}")
print(f, img.signature())
# Conlusion
# openstreetmap-legend
# same from 0 to 6 included
# unique for 7, 8, 9, 10, 11, 12
# same for 13, 14
# same from 15 to 18
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({
defaultViewport: {
width: 1920,
height: 2000
}
});
const page = await browser.newPage();
var lang = 'fr';
await page.setExtraHTTPHeaders({
'Accept-Language': lang
});
// Pass the User-Agent Test.
const userAgent = 'Mozilla/5.0 (X11; Linux x86_64)' +
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36';
await page.setUserAgent(userAgent);
await page.goto('https://www.openstreetmap.org/#map=0/47.21966/-1.55467', {
waitUntil: 'networkidle0'
});
/*
page.on('console', msg => {
for (let i = 0; i < msg._args.length; ++i)
console.log(`${i}: ${msg._args[i]}`);
});
*/
await page.waitForSelector('.key-ui > .section');
var result = await page.evaluate(() => {
document.querySelector('.control-key > a').click();
return document.querySelector('h2').innerHTML
});
console.log(result);
await page.waitFor(3000);
/*
await page.screenshot({
path: 'openstreetmap.png'
});
*/
async function generatePageScreenShot(page, zoom_level, lang) {
const element = await page.$('.key-ui > .section');// declare a variable with an ElementHandle
const box = await element.boundingBox(); // this method returns an array of geometric parameters of the element in pixels.
const x = box['x']; // coordinate x
const y = box['y']; // coordinate y
const w = box['width']; // area width
const h = box['height']; // area height
await page.screenshot({
'path': `openstreetmap-legend-${lang}-${String(zoom_level).padStart(2, '0')}.png`,
'clip': {
'x': x,
'y': y,
'width': w,
'height': h
}
});
}
var ranges = [...Array(19 + 1).keys()];
for (const zoom_level of ranges) {
if (zoom_level !== 0) {
await page.evaluate(() => {
document.querySelector('.zoom > a').click();
});
await page.waitFor(3000);
}
await generatePageScreenShot(page, zoom_level, lang);
}
//await element.screenshot({path: 'openstreetmap-legend.png'});
await browser.close();
})();
{
"name": "download-osm-legend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"puppeteer": "^10.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment