无效的 GeoJson(数据不是 JSON 可序列化的)

Invalid GeoJson (Data was not JSON serializable)

我是 geojson spec 的新手...格式化导致了严重破坏。

我想做的就是建立一个新的 "features" 列表(仅针对积分),我正在为其添加新的 'properties'。然后我把它写成 (test.json).

现在我 returning 有间歇性的 "type": "FeatureCollection" 情况(我只希望看到它一次 - 在文件顶部)和一些错误的 } 语法错误:

{"type": "FeatureCollection", 
"features": [
{"geometry": {
"type": "Point", 
"coordinates": [-122.3447075, 47.6821492]}, 
"type": "Feature", 
"properties": {
"marker-color": "#808080", 
"timestamp": "2013-08-17T22:41:18Z", 
"version": 3, 
"user": "seattlefyi", 
"last_updated": "over a year ago", 
"id": 427307160, 
"marker-size": "small"
}}, ## what??
]} ## what??
{"type": "FeatureCollection",  
"features": [
{"geometry": {
"type": "Point", 
"coordinates": [-122.3447075, 47.6821492]}, 
"type": "Feature", 
"properties": {
"marker-color": "#808080", 
"timestamp": "2013-08-17T22:41:18Z", 
"version": 3, 
"user": "seattlefyi", 
"last_updated": "over a year ago", 
"id": 427307160, 
"marker-size": "small"
}}, ## what...no "type": "FeatureCollection" on this one?
{"geometry": {
"type": "Point", 
"coordinates": [-122.377932, 47.5641566]}, 
"type": "Feature", 
"properties": {
"marker-color": "#808080", 
"timestamp": "2009-07-11T04:04:51Z", 
"version": 1, 
"user": "Rob Lanphier", 
"last_updated": "over a year ago", 
"id": 439976119, 
"marker-size": "small"
}
}
]
}

但是,我正在尝试 return

{"type": "FeatureCollection", 
"features": [
{"type": "Feature", 
"geometry": {
"type": "Point", 
"coordinates": [-122.3447075, 47.6821492]}, 
"properties": {
"marker-color": "#808080", 
"timestamp": "2013-08-17T22:41:18Z", 
"version": 3, 
"user": "ralph", 
"last_updated": "over a year ago", 
"id": 427307160, 
"marker-size": "small"
}},
{"type": "Feature", 
"geometry": {
"type": "Point", 
"coordinates": [-122.377932, 47.5641566]}, 
"properties": {
"marker-color": "#808080", 
"timestamp": "2009-07-11T04:04:51Z", 
"version": 1, 
"user": "Rob Lanphier", 
"last_updated": "over a year ago", 
"id": 439976119, 
"marker-size": "small"
}
}
]
}

代码是:

def write_to_features(source, class_time, color):
    """ write the json into geojson
        takes all the items from one node ("lat", "lon", "id", "user", "tags", "timestamp")
        writes all the items with new tags "last_updated","marker-color","marker-size"
        returns a dict
    """



    pt = {
        "type": "Feature",
        "geometry": {
            "type": 'Point',
            "coordinates": [float(source['lon']), float(source['lat'])]
            },
        "properties": {
            "user": source['user'],
            "id": source['id'],
            "version": source['version'],
            "timestamp": source['timestamp'],
            "last_updated": classified_time,
            "marker-color": marker_color,
            "marker-size": "small"
            }
        }

    return pt

    def __main__():

    geojson = { "type": "FeatureCollection", "features": [] }

    outfile = r'.\test.json'

    with open(outfile, 'w') as geojson_file:

        for item in all_data_dict['elements']:
            point_dict = write_to_features(item, data_w_update, data_item_color)
            geojson['features'].append(point_dict)
            json.dump(geojson, geojson_file)

你的 json.dump(geojson, geojson_file) 不应该在你的循环之外吗?你 append 在它上面的那一行..所以我想问你为什么要多次 dump/write 文件?我认为您应该只调用 json.dump 一次。