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

时间:2015-05-04 19:10:09

标签: python json geojson

我对geojson spec相当新...并且格式化导致了havok。

我要做的就是建立一个新的“功能”列表(仅限点数),我为其添加了新的“属性”。然后我把它写进(test.json)。

现在我正在返回,其中有"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"
}
}
]
}

但是,我正在尝试返回

{"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)

1 个答案:

答案 0 :(得分:0)

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