Feature Your Map beta

get poi data from osm

当前为 2024-04-03 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Feature Your Map beta
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description get poi data from osm
  6. // @author KaKa
  7. // @match https://map-making.app/maps/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @grant GM_setClipboard
  11. // @license MIT
  12. // @icon https://www.google.com/s2/favicons?domain=geoguessr.com
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18.  
  19. const API_URL = "https://overpass-api.de/api/interpreter";
  20.  
  21. async function fetchData(query, isString,feature) {
  22. const requestBody = isString ?
  23. `[out:json][timeout:180]; area[name="${query}"]; (nwr(area)[${feature}]; );out geom;` :
  24. `[bbox:${query[1]},${query[0]},${query[3]},${query[2]}][out:json][timeout:180] ;( nwr[${feature}](${query[1]},${query[0]},${query[3]},${query[2]}););out geom;`;
  25. const response = await fetch(API_URL, {
  26. method: "POST",
  27. body: "data=" + encodeURIComponent(requestBody),
  28. });
  29.  
  30. if (!response.ok) {
  31. throw new Error("Network response was not ok");
  32. }
  33.  
  34. return response.json();
  35. }
  36.  
  37. async function getData(query) {
  38. try {
  39. const js = {
  40. "name": "",
  41. "customCoordinates": [],
  42. "extra": {
  43. "tags": {},
  44. "infoCoordinates": []
  45. }
  46. };
  47. for (let feature of features) {
  48. let requests = [];
  49. let elements = [];
  50. if (typeof query === 'string') {
  51. requests.push(fetchData(query, true, feature[1]));
  52. } else {
  53. requests = query.map(b => fetchData(b, false, feature[1]));
  54. }
  55.  
  56. const responses = await Promise.all(requests);
  57.  
  58. responses.forEach(response => {
  59. if (response.elements && response.elements.length > 0) {
  60. elements.push(...response.elements);
  61. }
  62. });
  63. writeData(elements, feature[0],js);
  64.  
  65. }
  66. GM_setClipboard(JSON.stringify(js));
  67. alert("JSON data has been copied to your clipboard!");
  68.  
  69. } catch (error) {
  70. console.error("Error fetching data:", error);
  71. }
  72. }
  73.  
  74. function writeData(coordinates, feature,js) {
  75. for (let i = 0; i < coordinates.length; i++) {
  76. if (coordinates[i].geometry) {
  77. let nodes = coordinates[i].geometry;
  78. let randomIndex = Math.floor(Math.random() * nodes.length);
  79. let randomCoordinate = nodes[randomIndex];
  80. let tag = coordinates[i].tags && coordinates[i].tags.highway ? coordinates[i].tags.highway : feature;
  81. js.customCoordinates.push({
  82. "lat": randomCoordinate.lat,
  83. "lng": randomCoordinate.lon,
  84. "extra": {
  85. "tags": [tag,feature]
  86. }
  87. });
  88. }else if (!isCoordinateExists(js.customCoordinates, coordinates[i].lat, coordinates[i].lon)) {
  89. js.customCoordinates.push({
  90. "lat": coordinates[i].lat,
  91. "lng": coordinates[i].lon,
  92. "extra": {
  93. "tags": [feature]
  94. }
  95. });
  96. }
  97. }
  98. }
  99.  
  100. function isCoordinateExists(coordinates, lat, lon) {
  101. for (let i = 0; i < coordinates.length; i++) {
  102. if (coordinates[i].lat === lat && coordinates[i].lng === lon) {
  103. return true;
  104. }
  105. }
  106.  
  107. return false;
  108. }
  109.  
  110. function getInput() {
  111. const option = confirm('Do you want to upload a Geojson file? If you click "Cancel",you will need to enter a location name');
  112.  
  113. if (option) {
  114. const input = document.createElement('input');
  115. input.type = 'file';
  116. input.accept = '.geojson';
  117. input.addEventListener('change', event => {
  118. const file = event.target.files[0];
  119. if (file) {
  120. readFile(file);
  121. document.body.removeChild(input);
  122. }
  123. });
  124.  
  125. document.body.appendChild(input);
  126. } else {
  127. const query = prompt('Please enter a location name(eg:Paris)');
  128. getData(query);
  129. }
  130. }
  131.  
  132. function readFile(file) {
  133. const reader = new FileReader();
  134.  
  135. reader.onload = function(event) {
  136. const jsonContent = event.target.result;
  137.  
  138. try {
  139. const data = JSON.parse(jsonContent);
  140.  
  141. if (data.features && data.features.length > 0) {
  142. const boundary = data.features.map(feature => feature.bbox);
  143. getData(boundary);
  144. } else {
  145. console.error('Invalid Geojson format.');
  146. }
  147. } catch (error) {
  148. console.error('Error parsing Geojson:', error);
  149. }
  150. };
  151.  
  152. reader.readAsText(file);
  153. }
  154.  
  155. let features=[]
  156. var mainButtonContainer = document.createElement('div');
  157. mainButtonContainer.style.position = 'fixed';
  158. mainButtonContainer.style.right = '20px';
  159. mainButtonContainer.style.top = '20px';
  160. document.body.appendChild(mainButtonContainer);
  161.  
  162. var buttonContainer = document.createElement('div');
  163. buttonContainer.style.position = 'fixed';
  164. buttonContainer.style.right = '20px';
  165. buttonContainer.style.bottom = '60px';
  166. buttonContainer.style.display = 'none';
  167. document.body.appendChild(buttonContainer);
  168.  
  169. function createCheckbox(text, tags) {
  170. var label = document.createElement('label');
  171. var checkbox = document.createElement('input');
  172. checkbox.type = 'checkbox';
  173. checkbox.value = text;
  174. checkbox.name = 'tags';
  175. checkbox.id = tags;
  176. label.appendChild(checkbox);
  177. label.appendChild(document.createTextNode(text));
  178. buttonContainer.appendChild(label);
  179. return checkbox;
  180. }
  181.  
  182. var triggerButton = document.createElement('button');
  183. triggerButton.textContent = 'Star Featuring';
  184. triggerButton.addEventListener('click', function() {
  185. var checkboxes = document.getElementsByName('tags');
  186. for (var i=0; i<checkboxes.length; i++) {
  187. if (checkboxes[i].checked) {
  188. if (!features.includes(checkboxes[i].id)) {
  189. features.push([checkboxes[i].value,checkboxes[i].id]);
  190. }
  191. }
  192. }
  193. getInput();
  194. });
  195. buttonContainer.appendChild(triggerButton);
  196.  
  197. var mainButton = document.createElement('button');
  198. mainButton.textContent = 'Feature Your Map';
  199. mainButton.addEventListener('click', function() {
  200. if (buttonContainer.style.display === 'none') {
  201. buttonContainer.style.display = 'block';
  202. } else {
  203. buttonContainer.style.display = 'none';
  204. }
  205. });
  206. mainButtonContainer.appendChild(mainButton);
  207.  
  208. createCheckbox('bridge', '"bridge"="yes"');
  209. createCheckbox('bus stop', '"highway"="bus_stop"');
  210.  
  211. })();