如何修复/删除动态创建的geoJson文件(Python)中不需要的“ \ n”新行

问题描述 投票:0回答:1

我通过接受pologon坐标并生成生成的geoJson文件来动态设置geoJson文件,然后将其附加到“主” geoJson文件或直接在地图上读取。

我已经能够生成geoJson文件,但是它以错误的格式打印。我需要怎么做才能获得正确的格式输出?

我尝试使用:filecontent = [line.rstrip()用于最近文件中的行]

消除“ \ n”字符,但仍将奇数字符打印到文件中。

我有这些未格式化的输出(开始)。这是每行上每个值前后的新行

{
    "type": "FeatureCollection",
    "features": [
        "{\n    \"type\": \"Feature\",\n    \"geometry\": {\n        \"type\": \"Polygon\",\n        \"coordinates\": [\n            [\n                [\n                    -96.00362,\n                    30.09309,\n                    0.0\n                ],\n                [\n                    -96.00095,\n                    30.09731,\n                    0.0\n                ],\n                [\n                    -95.99735,\n                    30.09713,\n                    0.0\n                ],\n                [\n                    -95.99848,\n                    30.09348,\n                    0.0\n                ],\n                [\n                    -96.00362,\n                    30.09309,\n                    0.0\n                ]\n            ]\n        ]\n    },\n    \"properties\": {\n        \"name\": \"Pasture 7\",\n        \"styleUrl\": \"#poly-4F2682-3000-128\",\n        \"styleHash\": \"-50cd947a\",\n        \"styleMapHash\": {\n            \"normal\": \"#poly-4F2682-3000-128-normal\",\n            \"highlight\": \"#poly-4F2682-3000-128-highlight\"\n        },\n        \"description\": \"Future site for\\u00a0\\u201cComprehensive Hi-Tech Agricultural Complex\\u201d\",\n        \"stroke\": \"#4f2682\",\n        \"stroke-opacity\": 1,\n        \"stroke-width\": 3,\n        \"fill\": \"#4f2682\",\n        \"fill-opacity\": 0.5019607843137255\n    }\n}{\n    \"type\": \"Feature\",\n    \"geometry\": {\n        \"type\": \"Polygon\",\n        \"coordinates\": [\n            [\n                [\n                    -96.02551,\n                    30.09428,\n                    0.0\n                ],\n                [\n                    -96.02524,\n                    30.09953,\n                    0.0\n                ],\n                [\n                    -96.0188,\n                    30.09913,\n                    0.0\n                ],\n                [\n                    -96.01874,\n                    30.0937,\n                    0.0\n                ],\n                [\n                    -96.02551,\n                    30.09428,\n                    0.0\n                ]\n            ]\n        ]\n    },\n    \"properties\": {\n        \"name\": \"Pasture 7\",\n        \"styleUrl\": \"#poly-4F2682-3000-128\",\n        \"styleHash\": \"-50cd947a\",\n        \"styleMapHash\": {\n            \"normal\": \"#poly-4F2682-3000-128-normal\",\n            \"highlight\": \"#poly-4F2682-3000-128-highlight\"\n        },\n        \"description\": \"Future site for\\u00a0\\u201cComprehensive Hi-Tech Agricultural Complex\\u201d\",\n        \"stroke\": \"#4f2682\",\n        \"stroke-opacity\": 1,\n        \"stroke-width\": 3,\n        \"fill\": \"#4f2682\",\n        \"fill-opacity\": 0.5019607843137255\n    }\n}\n"
    ]
}

输出格式错误的结尾

这是我的代码,用于在写入文件之前读取geoJson文件并追加到新变量。

import os
import json
from pathlib import Path
import shutil

files = [os.path.join(sourceoffiles, x) for x in 
os.listdir(destinationfiles) if x.endswith(".json")]
newest = max(files , key = os.path.getctime)

recentfile = open(newest)
recentfilecontent = recentfile.read()
#print(recentfilecontent)

geojson_file = {
"type": "FeatureCollection",
"features": [

]
};
geojson_file['features'].append(recentfilecontent)

with open("newarea.js" , "w") as newarea:
    json.dump(geojson_file, newarea, indent=4)

这是我的预期输出:

{
"type": "FeatureCollection",
"features": [
{
   "type": "Feature",
   "geometry": {
       "type": "Polygon",
       "coordinates": [
           [
               [
                   -95.97203,
                   30.094557,
                   0
               ],
               [
                   -95.970563,
                   30.094285,
                   0
               ],
               [
                   -95.969004,
                   30.094035,
                   0
               ],
               [
                   -95.967678,
                   30.093836,
                   0
               ],
               [
                   -95.967681,
                   30.095099,
                   0
               ],
               [
                   -95.967687,
                   30.096482,
                   0
               ],
               [
                   -95.967699,
                   30.097665,
                   0
               ],
               [
                   -95.970578,
                   30.097908,
                   0
               ],
               [
                  -95.97203,
                  30.094557,
                  0
               ]
           ]
       ]
   },
   "properties": {
       "name": "Area 1",
       "styleUrl": "#poly-FF0004-3000-128-nodesc",
       "styleHash": "-188cd7bd",
       "styleMapHash": {
           "normal": "#poly-FF0004-3000-128-nodesc-normal",
           "highlight": "#poly-FF0004-3000-128-nodesc-highlight"
       },
       "stroke": "#ff0004",
       "stroke-opacity": 1,
       "stroke-width": 3,
       "fill": "#ff0004",
       "fill-opacity": 0.5019607843137255
   }
},
{
   "type": "Feature",
   "geometry": {
       "type": "Polygon",
       "coordinates": [
           [
               [
                   -95.979281,
                   30.0946,
                   0
               ],
               [
                   -95.979115,
                   30.095331,
                   0
               ],
               [
                   -95.980096,
                   30.095487,
                   0
               ],
               [
                   -95.980258,
                   30.094751,
                   0
               ],
               [
                   -95.979281,
                   30.0946,
                   0
               ]
           ]
       ]
   },
   "properties": {
       "name": "Area 2",
       "styleUrl": "#poly-0000FF-3000-128-nodesc",
       "styleHash": "-6fb6f6b9",
       "styleMapHash": {
           "normal": "#poly-0000FF-3000-128-nodesc-normal",
           "highlight": "#poly-0000FF-3000-128-nodesc-highlight"
       },
       "stroke": "#0000ff",
       "stroke-opacity": 1,
       "stroke-width": 3,
       "fill": "#0000ff",
       "fill-opacity": 0.5019607843137255
   }
}
]
};
python json geojson
1个回答
0
投票

您读取的文件包含python视为字符串的json编码数据。当您将其写入geojson_filefeatures数组时,您正在写入一个字符串。在将结果dict加载到geojson_file之前,您需要解析json:

geojson_file['features'].append(json.loads(recentfilecontent))

换行符消失,成为(无意义的)json空格的一部分,该空格当前表示为包含换行符的json编码字符串。

© www.soinside.com 2019 - 2024. All rights reserved.