Process huge GEOJson file with jq

后端 未结 4 1978
庸人自扰
庸人自扰 2021-01-24 07:26

Given a GEOJson file as follows:-

{
  \"type\": \"FeatureCollection\",
  \"features\": [
   {
     \"type\": \"Feature\",
     \"properties\": {
     \"FEATCODE\         


        
4条回答
  •  逝去的感伤
    2021-01-24 07:33

    An alternative solution could be for example:

    jq '.features |= map_values(.tippecanoe.minzoom = 13)'
    

    To test this, I created a sample JSON as

    d = {'features': [{"type":"Feature", "properties":{"FEATCODE": 15014}} for i in range(0,N)]}
    

    and inspected the execution time as a function of N. Interestingly, while the map_values approach seems to have linear complexity in N, .features[].tippecanoe.minzoom = 13 exhibits quadratic behavior (already for N=50000, the former method finishes in about 0.8 seconds, while the latter needs around 47 seconds)

    Alternatively, one might just do it manually with, e.g., Python:

    import json
    import sys
    
    data = {}
    with open(sys.argv[1], 'r') as F:
        data = json.load(F)
    
    extra_item = {"minzoom" : 13}
    for feature in data['features']:
        feature["tippecanoe"] = extra_item
    
    with open(sys.argv[2], 'w') as F:
        F.write(json.dumps(data))
    

提交回复
热议问题