【问题标题】:Error in importing geojson polygon into RethinkDB将 geojson 多边形导入 RethinkDB 时出错
【发布时间】:2025-11-24 23:20:05
【问题描述】:

我关注geojson polygon JSON,我想将其导入 rethinkDB。我尝试使用以下r.geojson 方法导入(参考Building an earthquake map with RethinkDB and GeoJSON):

r.db("strongloop").table("region").insert(
r.http("91231cd2.ngrok.io/data/geojson/MP14_REGION_WEB_PL_FLAT.json")("features")
.merge(function(zone) {
  return {
    zone: r.geojson(zone("geometry"))
  }
}))

这种方法给了我以下错误:

RqlRuntimeError: Invalid LinearRing.  Are there antipodal or duplicate vertices? Is it self-intersecting? in: r.db("strongloop").table("region").insert(r.http("91231cd2.ngrok.io/data/geojson/MP14_REGION_WEB_PL_FLAT.json")("features").merge(function(var_63) { return {zone: r.geojson(var_63("geometry"))}; }))

我怀疑这是因为 geojson 来自扁平的多面体(使用 geojson.io Meta feature 完成,因为 RethinkDB 不支持多面体) - 但从视觉上看,多边形是我所期望的。我还尝试使用r.polygon 方法如下:

r.db("strongloop").table("region").insert(
r.http("91231cd2.ngrok.io/data/geojson/MP14_REGION_WEB_PL_FLAT.json")("features")
 .merge(function(zone) {
  return {
    zone: r.polygon(zone("geometry")("coordinates"))
  }
}))

但 RethinkDB 希望我给出点数

 RqlCompileError: Expected 3 or more arguments but found 1 in: r.db("strongloop").table("region").insert(r.http("91231cd2.ngrok.io/data/geojson/MP14_REGION_WEB_PL_FLAT.json")("features").merge(function(var_64) { return {zone: r.polygon(var_64("geometry")("coordinates"))}; }))

在使用上面的代码传递给r.polygon 之前,我无法弄清楚如何提取几何坐标数组。

我应该如何解决这个问题?有没有更好的办法?

【问题讨论】:

    标签: rethinkdb


    【解决方案1】:

    检查网址

    如果您自己执行http 请求会发生什么?我尝试了它并在该资源上获得了 404。我会先运行该命令并确保它有效:

    r.http("91231cd2.ngrok.io/data/geojson/MP14_REGION_WEB_PL_FLAT.json")
    

    试试r.args

    r.polygon 需要 3 个参数,但您传递的是一个。您可以尝试使用 r.args 在函数中传播这些参数(类似于 Function.apply,如果您是 JavaScript 专家)

    r.db("strongloop").table("region").insert(
      r.http("91231cd2.ngrok.io/data/geojson/MP14_REGION_WEB_PL_FLAT.json")  ("features")
       .merge(function(zone) {
        return {
          zone: r.polygon(r.args(zone("geometry")("coordinates")))
        }
      }))
    

    无效的多边形

    您的某个多边形似乎无效。如果您使用您发布的其他答案的输入并使用以下功能,它将起作用:

    r.expr(ARRAY_FROM_OTHER_ANSWER_WITH_POLYGONS).do(function (arr) {
      // Remove middle element
      return arr.slice(0, 2).add(arr.slice(3, 5))
      // Map all elements to polygons
      // Make sure you pass an array of LON/LATs into `r.polygon`
      .map(function (row) {
        return r.polygon(r.args(row('geometry')('coordinates')(0)));
      });
    })
    

    或许你可以一个一个地插入,然后捕获失败的。

    以下查询失败:

    r.json(r.http('https://gist.githubusercontent.com/tekoadjaib/9e0f0729c050b69b283f/raw/6950765ed4a931b9b208e69a3c39b3114be5c7e3/map.geojson'))('features')
      .map(function (row) {
        return r.polygon(r.args(row('geometry')('coordinates')(0)))
      })
    

    虽然这个(切片前 17 个元素)没有。

    r.json(r.http('https://gist.githubusercontent.com/tekoadjaib/9e0f0729c050b69b283f/raw/6950765ed4a931b9b208e69a3c39b3114be5c7e3/map.geojson'))('features')
      .slice(0, 17)
      .map(function (row) {
        return r.polygon(r.args(row('geometry')('coordinates')(0)))
      })
    

    【讨论】:

    • 嗨 Jorge,r.polygon 仍然拒绝 r.args 方法:RqlRuntimeError: Expected 3 or more arguments but found 1 in: r.db("strongloop").table("region").insert(r.http("6h822726.ngrok.io/data/geojson/MP14_PLNG_AREA_WEB_PL_FLAT.json")("features").merge(function(var_56) { return {zone: r.polygon(r.args(var_56("geometry")("coordinates")))}; }))1 - r.http 工作正常。我使用ngrok 创建到我的本地主机的隧道并远程提供geojson,因此URL 是随机和临时的
    • 您能否发布(在您的答案中)您将插入r.polygon 的数据的结果。谢谢!
    • 你是对的,问题出在导入的数据上。 r.geojson(zone("geometry")) 和 `zone: r.polygon(r.args(zone("geometry")("coordinates")(0)))` 都有效。我无法验证导致错误的点,但错误消息似乎与gis.stackexchange.com/questions/153573/… 相似。为了解决这个问题,我使用mapshaper.org 来简化多边形,然后手动删除带有geometry: null 的多边形——如果也可以使用 rql 完成这样的过滤/预处理,那就太好了
    【解决方案2】:

    在 RethinkDB Data Explorer 上运行此命令

    r.http("6f892736.ngrok.io/data/geojson/MP14_REGION_WEB_PL_FLAT.json") ("features")
    

    我收到了(删除了一些以适应 SO 限制)

     [
    {
        "geometry": {
            "coordinates": [
                [
                    [
                        103.84874965353661,
                        1.363027350968694
                    ],
                    [
                        103.84924291873233,
                        1.362752820720889
                    ],
                    [
                        103.84935645049902,
                        1.362682483934137
                    ],
                    [
                        103.84973091592526,
                        1.362406260504349
                    ],
                    [
                        103.84992386166968,
                        1.362263929416379
                    ],
                    [
                        103.85137091514449,
                        1.361102941278631
                    ],
                    [
                        103.85194295836556,
                        1.360652909851773
                    ],
                    [
                        103.8522672818684,
                        1.360326981650605
                    ],
                    [
                        103.82714577966834,
                        1.241938910658531
                    ],
                    [
                        103.82714429349235,
                        1.241945678019855
                    ]
    
                ]
            ],
            "type": "Polygon"
        },
        "properties": {
            "FMEL_UPD_D": "2014/12/05",
            "INC_CRC": "F6D4903B6C0B72F8",
            "OBJECTID": 1,
            "REGION_C": "CR",
            "REGION_N": "CENTRAL REGION",
            "SHAPE_Area": 136405631.404,
            "SHAPE_Leng": 131065.464453,
            "X_ADDR": 27836.5573,
            "Y_ADDR": 31929.9186
        },
        "type": "Feature"
    },
    {
        "geometry": {
            "coordinates": [
                [
                    [
                        103.82233409716747,
                        1.247288081083765
                    ],
                    [
                        103.82235533213682,
                        1.247267117084279
                    ],
                    [
                        103.82236522405806,
                        1.247257351790808
                    ],
                    [
                        103.82239090120945,
                        1.24726873242952
                    ],
                    [
                        103.82242101348092,
                        1.247282079176339
                    ],
                    [
                        103.82244356466394,
                        1.247292075218428
                    ],
                    [
                        103.8224842992999,
                        1.247310126516275
                    ],
    
                    [
                        103.82229812450149,
                        1.247323592638841
                    ],
                    [
                        103.82233409716747,
                        1.247288081083765
                    ]
                ]
            ],
            "type": "Polygon"
        },
        "properties": {
            "FMEL_UPD_D": "2014/12/05",
            "INC_CRC": "F6D4903B6C0B72F8",
            "OBJECTID": 1,
            "REGION_C": "CR",
            "REGION_N": "CENTRAL REGION",
            "SHAPE_Area": 136405631.404,
            "SHAPE_Leng": 131065.464453,
            "X_ADDR": 27836.5573,
            "Y_ADDR": 31929.9186
        },
        "type": "Feature"
    },
    {
        "geometry": {
            "coordinates": [
                [
                    [
                        103.81270485670082,
                        1.253738883481215
                    ],
                    [
                        103.81270819925057,
                        1.253736038375202
                    ],
                    [
                        103.81271747666578,
                        1.25372269002029
                    ],
                    [
                        103.81272796270179,
                        1.253696608214719
                    ],
                    [
                        103.81272550089241,
                        1.25367617858827
                    ],
                    [
                        103.81271663253291,
                        1.253660777180855
                    ],
                    [
                        103.81268489667742,
                        1.253637281524639
                    ],
                    [
                        103.81271738240808,
                        1.253596580832204
                    ],
                    [
                        103.81272993138482,
                        1.25358085854482
                    ],
                    [
                        103.81277884770714,
                        1.253527980705921
                    ],
                    [
                        103.81280844732579,
                        1.253491732178708
                    ],
                    [
                        103.81284783649771,
                        1.253444824881476
                    ],
                    [
                        103.81289710415396,
                        1.253390129269173
                    ],
    
                    [
                        103.81268859863616,
                        1.253749463519217
                    ],
                    [
                        103.81269418659909,
                        1.253747966839359
                    ],
                    [
                        103.81269933160983,
                        1.253743587040128
                    ],
                    [
                        103.81270485670082,
                        1.253738883481215
                    ]
                ]
            ],
            "type": "Polygon"
        },
        "properties": {
            "FMEL_UPD_D": "2014/12/05",
            "INC_CRC": "F6D4903B6C0B72F8",
            "OBJECTID": 1,
            "REGION_C": "CR",
            "REGION_N": "CENTRAL REGION",
            "SHAPE_Area": 136405631.404,
            "SHAPE_Leng": 131065.464453,
            "X_ADDR": 27836.5573,
            "Y_ADDR": 31929.9186
        },
        "type": "Feature"
    },
    {
        "geometry": {
            "coordinates": [
                [
                    [
                        103.81422064298785,
                        1.252570070111331
                    ],
                    [
                        103.81420878069888,
                        1.252558437176131
                    ],
                    [
                        103.8142083125649,
                        1.252558523991746
                    ],
                    [
                        103.81420773751474,
                        1.252557413427196
                    ],
                    [
                        103.81419024508641,
                        1.252540259301697
                    ],
                    [
                        103.81418525918728,
                        1.252535369363019
                    ],
                    [
                        103.81417827319876,
                        1.252525659143691
                    ],
                    [
                        103.81417483815903,
                        1.252520884975136
                    ],
                    [
                        103.81417401960785,
                        1.25251974727783
                    ],
                    [
                        103.8141688845694,
                        1.252512609990775
                    ],
                    [
                        103.81416372167686,
                        1.252505434720186
                    ],
                    [
                        103.81415922015667,
                        1.252490646509147
                    ],
                    [
                        103.81415911592985,
                        1.252490302849641
                    ],
                    [
                        103.81415883649417,
                        1.252489384917013
                    ],
                    [
                        103.81415845013613,
                        1.252488116994314
                    ],
                    [
                        103.8141580341274,
                        1.252486750495618
                    ],
                    [
                        103.8141581823891,
                        1.252486131910979
                    ],
                    [
                        103.81415836569447,
                        1.252485366819468
                    ],
                    [
                        103.81415859033339,
                        1.252484429898899
                    ],
                    [
                        103.81415863076748,
                        1.252484387394026
                    ],
                    [
                        103.81415862268139,
                        1.252484294244369
                    ],
                    [
                        103.81416084480966,
                        1.252475025422944
                    ],
                    [
                        103.81416187994584,
                        1.25247070708728
                    ],
                    [
                        103.81416579397985,
                        1.252464651487459
                    ],
                    [
                        103.81417344413717,
                        1.25245281611786
                    ],
                    [
                        103.81417638235824,
                        1.25244826989617
                    ],
                    [
                        103.81418792868888,
                        1.252415206393471
                    ],
                    [
                        103.81423209597757,
                        1.252398253489821
                    ],
                    [
                        103.8142345678362,
                        1.252397304828964
                    ],
                    [
                        103.81423924468973,
                        1.252395510602936
                    ],
                    [
                        103.8142416716218,
                        1.252394579124685
                    ],
                    [
                        103.81424197622378,
                        1.252394461559454
                    ],
                    [
                        103.81424641316956,
                        1.252392758672508
                    ],
                    [
                        103.81427168974326,
                        1.252383056828959
                    ],
                    [
                        103.81429695645653,
                        1.252370091131469
                    ],
                    [
                        103.81430622571014,
                        1.252365335143192
                    ],
                    [
                        103.81430795807904,
                        1.252364492287584
                    ],
                    [
                        103.81430790237042,
                        1.252364474199889
                    ],
                    [
                        103.8143179156083,
                        1.252359335670564
                    ],
                    [
                        103.81433202075975,
                        1.252352098139711
                    ],
                    [
                        103.81434014977647,
                        1.252347926362841
                    ],
                    [
                        103.81435231859189,
                        1.252341681810603
                    ],
                    [
                        103.81435985727103,
                        1.252338114145249
                    ],
                    [
                        103.81437420681739,
                        1.25233151509769
                    ],
                    [
                        103.81439499523958,
                        1.252321955205564
                    ],
                    [
                        103.81439606179633,
                        1.252321465047431
                    ],
                    [
                        103.81439624419711,
                        1.252321532876141
                    ],
                    [
                        103.81440192381508,
                        1.25231876917771
                    ],
                    [
                        103.81441291824507,
                        1.252313712952395
                    ],
                    [
                        103.81442976570837,
                        1.252305965378984
                    ],
                    [
                        103.81444317808666,
                        1.252299726261537
                    ],
                    [
                        103.81445277979405,
                        1.252295146626335
                    ],
                    [
                        103.81447443170696,
                        1.252284820742596
                    ],
                    [
                        103.8144961555022,
                        1.252274461397674
                    ],
                    [
                        103.81450288101018,
                        1.252271253663335
                    ],
                    [
                        103.81450281451966,
                        1.252271173174363
                    ],
                    [
                        103.81452463535528,
                        1.252260878944195
                    ],
                    [
                        103.81458959107665,
                        1.25223203649656
                    ],
                    [
                        103.81461125376586,
                        1.252222416920872
                    ],
                    [
                        103.81464389561143,
                        1.252207399268555
                    ],
                    [
                        103.81465169486319,
                        1.252203810803814
                    ],
                    [
                        103.81469601222396,
                        1.252183421305906
                    ],
                    [
                        103.81472344798394,
                        1.252170002532816
                    ],
                    [
                        103.81478302601083,
                        1.252140862505733
                    ],
                    [
                        103.81484031637163,
                        1.252113379257888
                    ],
                    [
                        103.81490026635718,
                        1.25208846442443
                    ],
                    [
                        103.81491445851239,
                        1.252102786050432
                    ],
                    [
                        103.81491771565472,
                        1.252106072536219
                    ],
                    [
                        103.81491908860134,
                        1.25210674629792
                    ],
                    [
                        103.81493128425387,
                        1.252117179139818
                    ],
                    [
                        103.8149446623662,
                        1.2521281301912
                    ],
                    [
                        103.81496569689162,
                        1.252131490055921
                    ],
                    [
                        103.81497133065815,
                        1.25213238993891
                    ],
                    [
                        103.81498839193847,
                        1.252128956184855
                    ],
                    [
                        103.81500279627888,
                        1.252124305136556
                    ],
                    [
                        103.8150235712083,
                        1.252116328782531
                    ],
                    [
                        103.81502467283144,
                        1.252112382141001
                    ],
                    [
                        103.8150250430265,
                        1.252112184992011
                    ],
                    [
                        103.81502909456997,
                        1.252100774646097
                    ],
                    [
                        103.81503503665392,
                        1.252084043029215
                    ],
                    [
                        103.81503408874394,
                        1.252078647580404
                    ],
                    [
                        103.81503511578735,
                        1.25207496772605
                    ],
                    [
                        103.81503518952596,
                        1.252066416954603
                    ],
                    [
                        103.81502759086356,
                        1.252038774077729
                    ],
                    [
                        103.81502703827032,
                        1.252038532608371
                    ],
                    [
                        103.81502617751417,
                        1.252033633656605
                    ],
                    [
                        103.81501061513713,
                        1.252014397701631
                    ],
                    [
                        103.81497715415199,
                        1.251984291152084
                    ],
                    [
                        103.81496625504562,
                        1.251978257151349
                    ],
                    [
                        103.8149622584071,
                        1.251976044141786
                    ],
                    [
                        103.8149536020188,
                        1.251971250945737
                    ],
                    [
                        103.8149359747586,
                        1.25196149181759
                    ],
                    [
                        103.81491979321531,
                        1.251953915836878
                    ],
                    [
                        103.8149053934479,
                        1.251947173693286
                    ],
                    [
                        103.81487481213755,
                        1.251932856472991
                    ],
                    [
                        103.81486042674678,
                        1.251926121564181
                    ],
                    [
                        103.8148226958023,
                        1.251918231615303
                    ],
                    [
                        103.8148209230086,
                        1.251917860813026
                    ],
                    [
                        103.81482003795891,
                        1.251917765848395
                    ],
                    [
                        103.8148186775897,
                        1.251917391431592
                    ],
                    [
                        103.81480645223182,
                        1.251914834704565
                    ],
                    [
                        103.81478684994318,
                        1.251914215075008
                    ],
                    [
                        103.81478406270905,
                        1.25191412733175
                    ],
                    [
                        103.81476586212422,
                        1.251913551121548
                    ],
                    [
                        103.81475217215036,
                        1.251913118737449
                    ],
                    [
                        103.81473641375817,
                        1.251913570807558
                    ],
                    [
                        103.81469760451252,
                        1.25191528611085
                    ],
                    [
                        103.81464911173373,
                        1.251930252101506
                    ],
                    [
                        103.81464647993616,
                        1.251931415096138
                    ],
                    [
                        103.81464604415146,
                        1.251931198949751
                    ],
                    [
                        103.81463423207276,
                        1.251934844360703
                    ],
                    [
                        103.81462343982588,
                        1.251938175059867
                    ],
                    [
                        103.81461634052772,
                        1.251941862105349
                    ],
                    [
                        103.81459325275814,
                        1.251953853820537
                    ],
                    [
                        103.81457931114169,
                        1.251961094067155
                    ],
                    [
                        103.81457055045844,
                        1.251965643864866
                    ],
                    [
                        103.81450478872569,
                        1.251999797642535
                    ],
                    [
                        103.81446223335465,
                        1.252023820888956
                    ],
                    [
                        103.81441832030102,
                        1.2520486110264
                    ],
                    [
                        103.8143689396795,
                        1.25207648681698
                    ],
                    [
                        103.81429144309426,
                        1.252119919289534
                    ],
                    [
                        103.81421839961543,
                        1.252163028029699
                    ],
                    [
                        103.81418472441868,
                        1.252183801048487
                    ],
                    [
                        103.81416549402833,
                        1.252195662559566
                    ],
                    [
                        103.81411929141971,
                        1.252234061560852
                    ],
                    [
                        103.81407773413764,
                        1.252281223893357
                    ],
                    [
                        103.81405899059976,
                        1.252313446282806
                    ],
                    [
                        103.81403917762307,
                        1.252371587767593
                    ],
                    [
                        103.81403048853925,
                        1.252414490782956
                    ],
                    [
                        103.81403453146623,
                        1.252476150424537
                    ],
                    [
                        103.81404507986444,
                        1.252520744745098
                    ],
                    [
                        103.81406615890175,
                        1.252581120314285
                    ],
                    [
                        103.81410007800378,
                        1.252628997652677
                    ],
                    [
                        103.81413071763996,
                        1.252654609498039
                    ],
                    [
                        103.81415741467501,
                        1.252660750333722
                    ],
                    [
                        103.81415875168324,
                        1.252661057827697
                    ],
                    [
                        103.81415903202367,
                        1.252661122944038
                    ],
                    [
                        103.81416270520218,
                        1.25266196764807
                    ],
                    [
                        103.81416714302718,
                        1.252662988708921
                    ],
                    [
                        103.81416796517983,
                        1.252663120752286
                    ],
                    [
                        103.81420024580622,
                        1.252650676021723
                    ],
                    [
                        103.8142128693197,
                        1.252638786426654
                    ],
                    [
                        103.8142159252361,
                        1.252635907855009
                    ],
                    [
                        103.81421741504606,
                        1.252629051874232
                    ],
                    [
                        103.81422008102156,
                        1.252623340828278
                    ],
                    [
                        103.81422398521352,
                        1.252611535275082
                    ],
                    [
                        103.8142249584107,
                        1.252598998973394
                    ],
                    [
                        103.81422552184074,
                        1.252591729690962
                    ],
                    [
                        103.81422064298785,
                        1.252570070111331
                    ]
                ]
            ],
            "type": "Polygon"
        },
        "properties": {
            "FMEL_UPD_D": "2014/12/05",
            "INC_CRC": "F6D4903B6C0B72F8",
            "OBJECTID": 1,
            "REGION_C": "CR",
            "REGION_N": "CENTRAL REGION",
            "SHAPE_Area": 136405631.404,
            "SHAPE_Leng": 131065.464453,
            "X_ADDR": 27836.5573,
            "Y_ADDR": 31929.9186
        },
        "type": "Feature"
    },
    
    {
        "geometry": {
            "coordinates": [
                [
                    [
                        103.74130389714394,
                        1.159976941119677
                    ],
                    [
                        103.7413428293003,
                        1.159968251449955
                    ],
                    [
                        103.74129985127891,
                        1.159780169341412
                    ],
                    [
                        103.74126091912512,
                        1.159788858106384
                    ],
                    [
                        103.74130389714394,
                        1.159976941119677
                    ]
                ]
            ],
            "type": "Polygon"
        },
        "properties": {
            "FMEL_UPD_D": "2014/12/05",
            "INC_CRC": "11540153B663CA9B",
            "OBJECTID": 5,
            "REGION_C": "WR",
            "REGION_N": "WEST REGION",
            "SHAPE_Area": 257110296.977,
            "SHAPE_Leng": 258264.026231,
            "X_ADDR": 12896.436,
            "Y_ADDR": 33986.5714
        },
        "type": "Feature"
    }
    ]
    

    【讨论】: