我已经从网站上抓取了一些地理数据,放入json中,然后尝试将其映射到leaflet / d3中。
这里的原始JSON(仅一个例子):
var SFEvents = [[{"startDate": "2019-01-27", "performer": {"url": "https://www.bandsintown.com/a/1682883-schuster?came_from=244", "image": "", "@type": "MusicGroup", "name": "schuster"}, "endDate": "2019-01-27", "name": "schuster", "url": "https://www.bandsintown.com/e/1013085690-schuster-at-aqus-cafe?came_from=244", "image": "", "location": {"address": "Petaluma, CA", "geo": {"latitude": 38.2325, "@type": "GeoCoordinates", "longitude": -122.6355556}, "@type": "Place", "name": "Aqus Cafe"},
首先,我加载JSON:
d3.queue()
.defer(d3.csv, "data/SFEvents.json", function(cw) {
})
.await(ready);
function ready(error) {
console.log(SFEvents)
//concatenate two separate arrays into one
var allSFEvents = SFEvents[0].concat(SFEvents[1]);
控制台日志记录allSFEvents
,我看到其结构如下:
@context: "http://schema.org"
@type: "MusicEvent"
endDate: "2019-01-27"
image: ""
location:
@type: "Place"
address: "San Francisco, CA"
geo:
@type: "GeoCoordinates"
latitude: 37.775
longitude: -122.4183333
__proto__: Object
name: "Natoma Cabana"
__proto__: Object
name: "Winter Olympics"
performer: {url: "https://www.bandsintown.com/a/217960-
winter-olympics?came_from=244", image: "", @type: "MusicGroup", name: "Winter Olympics"}
startDate: "2019-01-27"
url: "https://www.bandsintown.com/e/1013280443-winter-olympics-at-natoma-cabana?came_from=244"
__proto__: Object
然后我将所有SFEvent转换为GeoJSON,因为我想映射坐标。
var outGeoJson = {}
outGeoJson['properties'] = allSFEvents
outGeoJson['type']= "Feature"
outGeoJson['geometry']= {"type": "Point", "coordinates":
[allSFEvents['latitude'], allSFEvents['longitude']]} // this is where I think the problem is, I'm not accessing "latitude" and "longitude" correctly here.
如果我从console.log中注销了GeoJson,我得到的似乎是功能齐全的geoJSON:
{properties: Array(36), type: "Feature", geometry: {…}}
geometry: {type: "Point", coordinates: Array(2)}
properties: Array(36)
0: {startDate: "2019-01-27", performer: {…}, endDate: "2019-
01-27", name: "schuster", url:
"https://www.bandsintown.com/e/1013085690-schuster-at-aqus-
cafe?came_from=244", …}
1: {startDate: "2019-01-27", performer: {…}, endDate: "2019-
01-27", name: "Winter Olympics", url:
"https://www.bandsintown.com/e/1013280443-winter-olympics-
at-natoma-cabana?came_from=244", …}
2: {startDate: "2019-01-27", performer: {…}, endDate: "2019-
01-
但是当我尝试使用以下内容读取坐标时:
outGeoJson.forEach(function(d) {
var coords = d.geometry.coordinates
console.log(coords)
}
我收到一条错误消息,说forEach不是函数。
同样,这可行,但是给了我未定义的坐标:
Object.keys(outGeoJson).forEach(function (key){
console.log(outGeoJson[key]);
});
所以我的两个问题是:1)我如何可以访问使用d.properties符号,所以我可以把它们映射坐标,和2)我不应该在GeoJSON的运行的forEach
谢谢。