V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
buxingdebeiju
V2EX  ›  程序员

geojson 遇到的诡异问题

  •  1
     
  •   buxingdebeiju · 2018-12-19 20:31:18 +08:00 · 1476 次点击
    这是一个创建于 2168 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我用 nodejs-v8.15.0 和 tilestrata-2.1.2 搭建了一个地图服务,现在在调试 geojson 的输出,遇到一个诡异的问题。 我的后端代码如下:

    query=`SELECT ST_ASGEOJSON(ST_INTERSECTION( ST_TRANSFORM(ST_SETSRID(way, 900913),4326) , ST_SETSRID(ST_MAKEENVELOPE(121.5966796875,31.12819929911195,121.61865234375,31.109388560814956), 4326) ) ) AS geometry, tags AS properties FROM planet_osm_roads WHERE ST_INTERSECTS( ST_TRANSFORM(ST_SETSRID(way, 900913),4326), ST_SETSRID(ST_MAKEENVELOPE(121.5966796875,31.12819929911195,121.61865234375,31.109388560814956), 4326) ) AND highway <> ''`;
    
    pgPool.query(query, function(err, result) {
        if (err) {
            console.log(query, err.message, err.stack)
            var err = new Error('An error occurred');
            err.statusCode = 500;
            return callback(err);
        }
          
        var outputText = '{"type":"FeatureCollection","features":[' +
            result.rows.map(function(row) {
              if (row.geometry) {
                var featureString = '{"type":"Feature","geometry":' + row.geometry;
                if (row.properties != ''){
                    var rowpro = row.properties;
                    rowpro = rowpro.replace(/=>/g,':');
                    featureString = featureString + ',"properties":' + '{'+  rowpro + '}}';
                }
                else{
                    featureString = featureString + ',"properties":""}';
                }
                delete row;
                return featureString;
              }
        }).join(',') +']}';
        
        //在此处的 outputText 得到的数据是完整的,总共 4372 个字符,也是可以正常解析的
        callback(null, outputText, {'Content-Type': 'application/json'});
    });
    

    然后在浏览器中输入 http://192.168.56.180:8767/geojson/14/13726/6700/road.json 你会发现 head 中的 Content-Length 是 4372,但 RAW DATA 中只显示了 4324 个字符,后面的消失了,导致报错:SyntaxError: JSON.parse: unterminated string at line 1 column 4325 of the JSON data

    并不是所有的语句都会被截断,但有些就会这样,如果我把 row.properties 这部分代码去除则不会出错,但是加了 row.properties 这部分代码后生成的 outputText 是正确的,一到浏览器那就可能出错,我找不出规律

    附:数据来源是 openstreetmap 下载的 shanghai 的 pbf,此问题在我这是可以复现的,浏览器是 chrome-71.0.3578.80 ( 64 位)和 firefox-63.0.3 (64 位),数据是用 osm2pgsql 导入的。

    请各位大虾指点迷津,我已经没方向了

    7 条回复    2018-12-19 21:13:38 +08:00
    des
        1
    des  
       2018-12-19 20:45:25 +08:00 via Android
    老哥,能别用拼接的么,很明显是拼 properties 的时候出错了。
    比如里边有引号之类的
    des
        2
    des  
       2018-12-19 20:47:10 +08:00 via Android
    肉眼难看出错误,你把 outputText 复制出
    来找个 json 格式化工具就很明显了
    des
        3
    des  
       2018-12-19 20:48:21 +08:00 via Android
    都 8102 年了,还有拼接 json 的
    kzfile
        4
    kzfile  
       2018-12-19 20:53:21 +08:00
    要我直接就在 postgis 里用 row_to_json 返回 featurecollection 了.
    buxingdebeiju
        5
    buxingdebeiju  
    OP
       2018-12-19 20:57:44 +08:00
    感谢回复,我将 outputText 复制出来验证过的,格式肯定没错,现在的问题是浏览器端会截断
    kzfile
        6
    kzfile  
       2018-12-19 21:06:06 +08:00
    你试过只加适量的吗?osm 数据谁知道有啥乱七八糟的字符
    buxingdebeiju
        7
    buxingdebeiju  
    OP
       2018-12-19 21:13:38 +08:00
    得到的 outputText 内容如下:
    {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6174958,31.1155741996536],[121.6175779,31.1154147996536],[121.6180951,31.1147908996536],[121.61865234375,31.1141186666828]]},"properties":{"name:en":"Kangxin Hwy", "int_name":"Kangxin Hwy"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.598580173598,31.128199299112],[121.5993113,31.1260036996529],[121.5994785,31.125470599653],[121.5996444,31.124930499653],[121.5998067,31.124386899653],[121.5999638,31.1238317996531],[121.6001239,31.1232462996531],[121.6001665,31.1230842996531]]},"properties":{"name:en":"Hufeng Expressway", "name:zh":"沪奉高速", "int_name":"Hufeng Gaosu", "name:zh-hant":"滬奉高速", "name:zh_pinyin":"Hùfèng Gāosù"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6002983,31.1231090996531],[121.6002555,31.1232720996531],[121.6000951,31.1238585996531],[121.5999377,31.124414799653],[121.5997751,31.124959499653],[121.5996089,31.125500299653],[121.5994414,31.1260344996529],[121.598720540595,31.128199299112]]},"properties":{"name:en":"Hufeng Expressway", "name:zh":"沪奉高速", "int_name":"Hufeng Gaosu", "name:zh-hant":"滬奉高速", "name:zh_pinyin":"Hùfèng Gāosù"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6004449,31.1176192996535],[121.6012693,31.1177876996535],[121.6015535,31.1178457996535],[121.6017564,31.1178871996534]]},"properties":{"name:en":"Zhoudeng Hwy", "int_name":"Zhoudeng Hwy"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6009608,31.1193780996534],[121.6011358,31.1184991996534],[121.60121,31.1181036996534],[121.6012693,31.1177876996535]]},"properties":""},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6015535,31.1178457996535],[121.6014975,31.1181438996534],[121.6014232,31.1185398996534],[121.6012754,31.1192820996534]]},"properties":""},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6007809,31.1200430996533],[121.6008066,31.1199276996533],[121.600875,31.1196021996533],[121.6009608,31.1193780996534]]},"properties":""},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6012754,31.1192820996534],[121.6012376,31.1196639996533],[121.6011359,31.1201282996533]]},"properties":""},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6003394,31.1219540996532],[121.6004978,31.1212930996532],[121.6006523,31.1206222996533],[121.6007809,31.1200430996533]]},"properties":""},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6011359,31.1201282996533],[121.6010129,31.1206820996533],[121.6008578,31.1213551996532],[121.6006991,31.1220172996532]]},"properties":""},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6001665,31.1230842996531],[121.6002363,31.1224859996532],[121.6002839,31.1222195996532],[121.6003394,31.1219540996532]]},"properties":{"destination:street":"周邓公路"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6006991,31.1220172996532],[121.600623,31.1222798996532],[121.6005278,31.1225404996532],[121.6002983,31.1231090996531]]},"properties":""},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6173272,31.1159013996536],[121.6174958,31.1155741996536]]},"properties":{"name:en":"Kangxin Hwy", "int_name":"Kangxin Hwy"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6017564,31.1178871996534],[121.6024349,31.1180257996534],[121.608019,31.1191666996534],[121.6153795,31.1206101996533],[121.6179191,31.1211082996532],[121.61865234375,31.1212281916358]]},"properties":{"name:en":"Zhoudeng Hwy", "int_name":"Zhoudeng Hwy"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6139156,31.125504199653],[121.6141751,31.124636599653],[121.6148268,31.1224580996532],[121.6153795,31.1206101996533],[121.6165758,31.1173601996535],[121.6173272,31.1159013996536]]},"properties":{"name:en":"Kangxin Hwy", "int_name":"Kangxin Hwy"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.6137068,31.1262022996529],[121.6139156,31.125504199653]]},"properties":{"name:en":"Kangxin Hwy", "int_name":"Kangxin Hwy"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[121.613109423394,31.128199299112],[121.6137068,31.1262022996529]]},"properties":{"name:en":"Kangxin Hwy", "int_name":"Kangxin Hwy"}}]}
    我用在线工具测试过的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4068 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 05:16 · PVG 13:16 · LAX 21:16 · JFK 00:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.