Published
Edited
Feb 21, 2020
Insert cell
md`# GEO Data Generate`
Insert cell
md`## Example Using
[保定着色](https://observablehq.com/@yhyddr/d3-geo)
`
Insert cell
viewof apiKey = secret("apiKey", {
description:
"This is a secret API token whose value is only available to you!",
submit: "Set secret"
})
Insert cell
FileAttachment("baoding.json").json()
Insert cell
JSON.parse(jsondata)
Insert cell
jsondata = JSON.stringify(await GeoData("保定"))
Insert cell
GeoData("保定")
Insert cell
md`## Functions`
Insert cell
GeoDataJSON = async name => {
const city = await loadGeoJSON(name);
const data = {
type: 'FeatureCollection',
features: []
};

const limits = city.districts[0].districts.length;
const colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00'];
for (let i = 0; i < limits; i++) {
const district = city.districts[0].districts[i].name;

const resp = await loadGeoJSON(city.districts[0].districts[i].name);
const feature = amapTranslation(resp.districts[0].polyline);
feature.properties = {
name: district,
color: colors[i % colors.length]
};
data.features.push(feature);
}
return JSON.stringify(data);
}
Insert cell
GeoData = async name => {
const city = await loadGeoJSON(name);
const data = {
type: 'FeatureCollection',
features: []
};

const limits = city.districts[0].districts.length;
const colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00'];
for (let i = 0; i < limits; i++) {
const district = city.districts[0].districts[i].name;

const resp = await loadGeoJSON(city.districts[0].districts[i].name);
const feature = amapTranslation(resp.districts[0].polyline);
feature.properties = {
name: district,
color: colors[i % colors.length]
};
data.features.push(feature);
}
return data;
}
Insert cell
loadGeoJSON = async city => {
const api = `https://restapi.amap.com/v3/config/district?key=${apiKey}&keywords=${city}&subdistrict=1&extensions=all`;

const resp = await fetch(api);

return await resp.json();
}
Insert cell
amapTranslation = polyline => {
return {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: polyline.split(';').map(val => val.split(','))
}
};
}
Insert cell
md`## Imports`
Insert cell
import { secret } from "@tmcw/secret"
Insert cell
d3 = require('d3')
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more