I am trying to convert shapes that I serialized into GeoJSON string format back into GEOGRAPHY with Google BigQuery GIS.
SELECT ST_GEOGFROMGEOJSON( BlockGroupShape ) AS tmp from dataset.blockgroup_shape
It was successful for ~90% of the shapes, however, it seems to be failing (only?) on the multipolygons with this error (I have ~10k of these failed ones...):
ST_GeogFromGeoJSON failed: Polygon should have only one shell, holes must be inside the shell. Polygon hole 1 is outside of the shell
Example geography:
{ "type": "MultiPolygon", "coordinates": [ [ [ [ -81.790922, 26.156442 ], [ -81.790924, 26.156640 ], [ -81.791073, 26.156640 ], [ -81.791079, 26.156954 ], [ -81.791092, 26.157640 ], [ -81.790950, 26.157703 ], [ -81.790950, 26.157840 ], [ -81.790958, 26.158269 ], [ -81.790960, 26.158425 ], [ -81.790985, 26.159858 ], [ -81.790987, 26.160142 ], [ -81.790996, 26.160414 ], [ -81.791002, 26.160560 ], [ -81.791009, 26.161166 ], [ -81.791013, 26.161445 ], [ -81.791040, 26.161454 ], [ -81.791072, 26.161486 ], [ -81.791090, 26.161527 ], [ -81.791096, 26.161577 ], [ -81.791096, 26.161640 ], [ -81.791100, 26.161898 ], [ -81.791167, 26.162077 ], [ -81.791185, 26.162806 ], [ -81.791189, 26.163029 ], [ -81.791040, 26.163018 ], [ -81.791043, 26.163137 ], [ -81.791051, 26.163485 ], [ -81.791055, 26.163674 ], [ -81.791060, 26.163890 ], [ -81.791064, 26.164114 ], [ -81.791069, 26.164454 ], [ -81.791075, 26.164800 ], [ -81.790692, 26.164778 ], [ -81.790699, 26.165209 ], [ -81.791081, 26.165198 ], [ -81.791090, 26.165451 ], [ -81.791092, 26.165647 ], [ -81.791094, 26.165881 ], [ -81.791107, 26.166456 ], [ -81.791116, 26.166579 ], [ -81.791122, 26.166668 ], [ -81.791133, 26.166812 ], [ -81.791149, 26.167013 ], [ -81.791191, 26.167464 ], [ -81.791223, 26.167751 ], [ -81.791240, 26.167985 ], [ -81.791272, 26.168303 ], [ -81.791302, 26.168589 ], [ -81.791313, 26.168742 ], [ -81.791337, 26.169281 ], [ -81.791354, 26.169732 ], [ -81.791361, 26.170141 ], [ -81.791365, 26.170405 ], [ -81.791373, 26.170804 ], [ -81.791379, 26.171023 ], [ -81.791381, 26.171228 ], [ -81.791388, 26.171595 ], [ -81.791396, 26.171767 ], [ -81.791404, 26.171880 ], [ -81.791407, 26.172017 ], [ -81.791407, 26.172162 ], [ -81.791407, 26.172477 ], [ -81.791407, 26.172675 ], [ -81.791409, 26.172844 ], [ -81.791421, 26.173196 ], [ -81.791423, 26.173257 ], [ -81.791429, 26.173342 ], [ -81.791447, 26.173512 ], [ -81.791449, 26.173661 ], [ -81.791451, 26.173749 ], [ -81.791459, 26.174266 ], [ -81.791453, 26.175174 ], [ -81.791451, 26.175379 ], [ -81.791449, 26.175731 ], [ -81.791331, 26.175731 ], [ -81.790510, 26.175725 ], [ -81.790544, 26.176437 ], [ -81.790555, 26.177320 ], [ -81.790893, 26.177314 ], [ -81.791450, 26.177310 ], [ -81.791458, 26.177473 ], [ -81.791460, 26.177524 ], [ -81.791465, 26.177903 ], [ -81.791471, 26.178571 ], [ -81.791489, 26.180027 ], [ -81.791491, 26.180180 ], [ -81.791496, 26.180371 ], [ -81.791498, 26.180472 ], [ -81.791504, 26.180892 ], [ -81.791504, 26.181074 ], [ -81.791506, 26.181561 ], [ -81.791517, 26.182158 ], [ -81.791678, 26.182153 ], [ -81.791690, 26.182621 ], [ -81.791705, 26.183187 ], [ -81.791736, 26.184391 ], [ -81.791748, 26.184895 ], [ -81.791762, 26.185437 ], [ -81.791765, 26.185572 ], [ -81.791782, 26.186238 ], [ -81.791804, 26.187134 ], [ -81.791809, 26.187409 ], [ -81.791828, 26.188171 ], [ -81.791836, 26.188478 ], [ -81.791846, 26.188807 ], [ -81.791850, 26.188946 ], [ -81.791856, 26.189186 ], [ -81.791859, 26.189298 ], [ -81.791862, 26.189393 ], [ -81.791866, 26.189540 ], [ -81.791949, 26.192615 ], [ -81.791954, 26.193090 ], [ -81.791961, 26.193329 ], [ -81.792014, 26.195537 ], [ -81.792020, 26.195617 ], [ -81.792025, 26.195783 ], [ -81.792031, 26.196048 ], [ -81.792046, 26.196625 ], [ -81.792148, 26.196628 ], [ -81.792234, 26.196670 ], [ -81.792153, 26.196708 ], [ -81.792048, 26.196710 ], [ -81.791891, 26.196710 ], [ -81.791898, 26.197247 ], [ -81.791903, 26.197681 ], [ -81.791907, 26.197968 ], [ -81.791913, 26.198385 ], [ -81.791915, 26.198523 ], [ -81.791917, 26.198734 ], [ -81.791938, 26.200014 ], [ -81.791938, 26.200078 ], [ -81.791941, 26.200317 ], [ -81.791949, 26.200911 ], [ -81.791995, 26.200978 ], [ -81.792015, 26.201007 ], [ -81.792034, 26.201041 ], [ -81.792049, 26.201100 ], [ -81.792051, 26.201484 ], [ -81.792119, 26.201677 ], [ -81.792120, 26.201744 ], [ -81.792128, 26.202208 ], [ -81.792129, 26.202250 ], [ -81.792158, 26.204339 ], [ -81.792161, 26.204556 ], [ -81.792165, 26.204782 ], [ -81.792172, 26.205331 ], [ -81.792175, 26.205526 ], [ -81.792178, 26.205772 ], [ -81.792182, 26.206009 ], [ -81.792218, 26.208468 ], [ -81.792042, 26.208603 ], [ -81.792081, 26.208610 ], [ -81.792109, 26.208622 ], [ -81.792130, 26.208640 ], [ -81.792148, 26.208673 ], [ -81.792152, 26.208717 ], [ -81.792152, 26.208833 ], [ -81.792165, 26.208963 ], [ -81.792179, 26.209015 ], [ -81.792224, 26.209159 ], [ -81.792228, 26.209224 ], [ -81.792242, 26.209467 ], [ -81.792257, 26.209732 ], [ -81.792276, 26.210112 ], [ -81.792300, 26.210609 ], [ -81.792301, 26.210727 ], [ -81.792303, 26.210901 ], [ -81.792308, 26.211295 ], [ -81.792209, 26.211292 ], [ -81.792133, 26.211294 ], [ -81.791723, 26.211275 ], [ -81.791412, 26.211272 ], [ -81.791028, 26.211276 ], [ -81.790524, 26.211280 ], [ -81.790406, 26.211278 ], [ -81.790254, 26.211275 ], [ -81.790057, 26.211275 ], [ -81.789715, 26.211273 ], [ -81.789525, 26.211273 ], [ -81.789438, 26.211270 ], [ -81.788712, 26.211268 ], [ -81.788414, 26.211265 ], [ -81.788293, 26.211264 ], [ -81.788214, 26.211264 ], [ -81.787656, 26.211272 ], [ -81.787240, 26.211276 ], [ -81.785905, 26.211303 ], [ -81.785811, 26.211338 ], [ -81.785692, 26.211361 ], [ -81.785534, 26.211372 ], [ -81.785164, 26.211380 ], [ -81.785099, 26.211389 ], [ -81.785045, 26.211416 ], [ -81.784999, 26.211453 ], [ -81.784348, 26.211464 ], [ -81.783256, 26.211482 ], [ -81.782915, 26.211482 ], [ -81.781403, 26.211511 ], [ -81.780802, 26.211519 ], [ -81.780426, 26.211522 ], [ -81.780449, 26.211450 ], [ -81.780454, 26.211377 ], [ -81.780453, 26.210966 ], [ -81.780460, 26.210750 ], [ -81.780555, 26.210736 ], [ -81.780951, 26.210615 ], [ -81.781092, 26.210566 ], [ -81.781277, 26.210508 ], [ -81.781538, 26.210444 ], [ -81.781761, 26.210416 ], [ -81.782043, 26.210371 ], [ -81.782333, 26.210344 ], [ -81.782547, 26.210348 ], [ -81.782570, 26.210546 ], [ -81.782570, 26.210718 ], [ -81.782600, 26.210794 ], [ -81.782669, 26.210863 ], [ -81.782730, 26.210892 ], [ -81.782799, 26.210905 ], [ -81.782990, 26.210903 ], [ -81.783127, 26.210888 ], [ -81.783180, 26.210869 ], [ -81.783257, 26.210773 ], [ -81.783706, 26.210692 ], [ -81.783735, 26.210746 ], [ -81.783808, 26.210805 ], [ -81.783921, 26.210827 ], [ -81.783959, 26.210850 ], [ -81.784005, 26.210985 ], [ -81.783996, 26.211083 ], [ -81.784010, 26.211129 ], [ -81.784036, 26.211140 ], [ -81.784096, 26.211133 ], [ -81.784125, 26.211113 ], [ -81.784173, 26.211102 ], [ -81.784308, 26.211109 ], [ -81.784364, 26.211102 ], [ -81.784384, 26.211069 ], [ -81.784378, 26.211017 ], [ -81.784371, 26.210960 ], [ -81.784365, 26.210771 ], [ -81.784332, 26.210441 ], [ -81.784348, 26.210264 ], [ -81.784322, 26.209702 ], [ -81.784298, 26.209183 ], [ -81.784273, 26.208643 ], [ -81.784231, 26.207710 ], [ -81.784253, 26.207703 ], [ -81.784298, 26.207652 ], [ -81.784337, 26.207530 ], [ -81.784322, 26.207305 ], [ -81.784306, 26.207147 ], [ -81.784247, 26.207065 ], [ -81.784238, 26.206671 ], [ -81.784217, 26.205869 ], [ -81.784199, 26.205381 ], [ -81.784184, 26.204548 ], [ -81.784167, 26.203985 ], [ -81.784150, 26.203565 ], [ -81.784116, 26.202845 ], [ -81.783755, 26.202895 ], [ -81.784010, 26.202183 ], [ -81.784088, 26.202179 ], [ -81.784079, 26.201910 ], [ -81.784065, 26.201771 ], [ -81.784019, 26.201548 ], [ -81.784022, 26.201109 ], [ -81.784048, 26.200960 ], [ -81.784060, 26.200891 ], [ -81.784055, 26.200720 ], [ -81.784051, 26.200575 ], [ -81.784028, 26.200436 ], [ -81.783986, 26.200273 ], [ -81.783946, 26.200018 ], [ -81.783936, 26.199507 ], [ -81.783936, 26.199281 ], [ -81.783936, 26.198574 ], [ -81.783919, 26.198468 ], [ -81.783886, 26.198363 ], [ -81.783802, 26.198243 ], [ -81.783735, 26.198092 ], [ -81.783718, 26.197942 ], [ -81.783684, 26.197656 ], [ -81.783718, 26.196737 ], [ -81.783751, 26.196572 ], [ -81.783802, 26.196481 ], [ -81.783819, 26.196451 ], [ -81.783847, 26.196255 ], [ -81.783839, 26.195897 ], [ -81.783824, 26.195500 ], [ -81.783792, 26.194905 ], [ -81.783767, 26.194353 ], [ -81.783762, 26.193506 ], [ -81.783741, 26.193308 ], [ -81.783711, 26.193152 ], [ -81.783667, 26.193029 ], [ -81.783638, 26.192985 ], [ -81.783489, 26.192753 ], [ -81.783163, 26.192341 ], [ -81.783124, 26.192281 ], [ -81.783083, 26.192172 ], [ -81.783075, 26.192112 ], [ -81.783006, 26.192014 ], [ -81.782943, 26.192021 ], [ -81.782888, 26.192014 ], [ -81.782804, 26.191975 ], [ -81.782717, 26.191886 ], [ -81.782707, 26.191828 ], [ -81.782736, 26.191826 ], [ -81.782763, 26.191751 ], [ -81.782698, 26.191576 ], [ -81.782700, 26.191540 ], [ -81.782746, 26.191523 ], [ -81.782787, 26.191520 ], [ -81.782914, 26.191472 ], [ -81.782940, 26.191265 ], [ -81.782940, 26.191190 ], [ -81.782932, 26.191162 ], [ -81.782921, 26.191126 ], [ -81.782869, 26.191009 ], [ -81.782853, 26.190932 ], [ -81.782821, 26.190894 ], [ -81.782805, 26.190778 ], [ -81.782783, 26.190682 ], [ -81.782746, 26.190625 ], [ -81.782711, 26.190552 ], [ -81.782752, 26.190507 ], [ -81.782808, 26.190465 ], [ -81.782944, 26.190315 ], [ -81.782974, 26.190263 ], [ -81.782993, 26.190182 ], [ -81.783000, 26.190002 ], [ -81.783023, 26.189904 ], [ -81.783068, 26.189795 ], [ -81.783162, 26.189630 ], [ -81.783305, 26.189442 ], [ -81.783504, 26.189243 ], [ -81.783575, 26.189108 ], [ -81.783598, 26.188773 ], [ -81.783598, 26.188487 ], [ -81.783587, 26.188289 ], [ -81.783531, 26.187343 ], [ -81.783507, 26.186025 ], [ -81.783463, 26.183615 ], [ -81.783439, 26.183554 ], [ -81.783337, 26.183420 ], [ -81.783292, 26.183342 ], [ -81.783255, 26.183240 ], [ -81.783243, 26.183143 ], [ -81.783243, 26.183049 ], [ -81.783255, 26.182951 ], [ -81.783288, 26.182829 ], [ -81.783360, 26.182551 ], [ -81.783406, 26.182284 ], [ -81.783435, 26.181994 ], [ -81.783420, 26.181008 ], [ -81.783405, 26.180029 ], [ -81.783379, 26.179197 ], [ -81.783409, 26.178789 ], [ -81.783402, 26.178188 ], [ -81.783331, 26.177290 ], [ -81.783352, 26.176979 ], [ -81.783305, 26.176673 ], [ -81.783305, 26.176538 ], [ -81.783341, 26.175959 ], [ -81.783339, 26.175806 ], [ -81.783342, 26.175571 ], [ -81.783333, 26.175091 ], [ -81.783342, 26.174785 ], [ -81.783320, 26.174484 ], [ -81.783320, 26.174305 ], [ -81.783333, 26.174265 ], [ -81.783351, 26.174230 ], [ -81.783436, 26.174188 ], [ -81.783448, 26.174182 ], [ -81.783510, 26.174166 ], [ -81.783590, 26.174126 ], [ -81.783722, 26.174011 ], [ -81.783966, 26.173829 ], [ -81.784105, 26.173762 ], [ -81.784271, 26.173728 ], [ -81.784314, 26.173704 ], [ -81.784416, 26.173615 ], [ -81.784505, 26.173443 ], [ -81.784608, 26.173204 ], [ -81.785277, 26.171912 ], [ -81.785622, 26.171245 ], [ -81.785721, 26.171051 ], [ -81.786154, 26.170196 ], [ -81.786408, 26.168904 ], [ -81.786487, 26.168502 ], [ -81.786589, 26.168133 ], [ -81.786671, 26.167521 ], [ -81.786697, 26.167225 ], [ -81.786738, 26.166765 ], [ -81.786774, 26.166583 ], [ -81.786860, 26.166416 ], [ -81.786894, 26.166370 ], [ -81.784411, 26.161942 ], [ -81.784415, 26.161916 ], [ -81.784355, 26.161897 ], [ -81.784178, 26.161735 ], [ -81.783974, 26.161513 ], [ -81.783955, 26.161237 ], [ -81.784091, 26.161010 ], [ -81.784119, 26.160962 ], [ -81.784263, 26.160741 ], [ -81.784279, 26.160532 ], [ -81.784281, 26.160511 ], [ -81.783916, 26.160332 ], [ -81.783864, 26.160264 ], [ -81.783803, 26.160140 ], [ -81.783856, 26.160028 ], [ -81.783964, 26.159983 ], [ -81.784167, 26.159458 ], [ -81.784203, 26.159366 ], [ -81.784325, 26.159147 ], [ -81.784564, 26.158271 ], [ -81.784598, 26.157992 ], [ -81.784608, 26.157909 ], [ -81.784559, 26.157871 ], [ -81.784422, 26.157656 ], [ -81.784367, 26.157545 ], [ -81.784347, 26.157374 ], [ -81.784370, 26.157166 ], [ -81.784437, 26.157010 ], [ -81.784619, 26.156766 ], [ -81.784719, 26.156632 ], [ -81.784773, 26.156582 ], [ -81.784788, 26.156483 ], [ -81.784802, 26.156487 ], [ -81.784875, 26.156505 ], [ -81.784994, 26.156483 ], [ -81.785517, 26.156543 ], [ -81.785766, 26.156538 ], [ -81.786909, 26.156513 ], [ -81.787420, 26.156502 ], [ -81.789877, 26.156505 ], [ -81.790131, 26.156505 ], [ -81.790194, 26.156501 ], [ -81.790234, 26.156477 ], [ -81.790270, 26.156451 ], [ -81.790922, 26.156442 ] ], [ [ -81.776795, 26.200088 ], [ -81.778140, 26.200009 ], [ -81.778373, 26.199976 ], [ -81.778606, 26.199921 ], [ -81.778837, 26.199857 ], [ -81.779199, 26.199752 ], [ -81.779545, 26.199695 ], [ -81.779891, 26.199657 ], [ -81.780599, 26.199666 ], [ -81.781463, 26.199788 ], [ -81.781762, 26.199902 ], [ -81.781274, 26.199803 ], [ -81.780450, 26.199720 ], [ -81.779785, 26.199703 ], [ -81.779541, 26.199725 ], [ -81.779321, 26.199769 ], [ -81.779053, 26.199841 ], [ -81.778839, 26.199918 ], [ -81.778577, 26.199989 ], [ -81.778138, 26.200072 ], [ -81.777820, 26.200094 ], [ -81.777387, 26.200091 ], [ -81.776795, 26.200088 ] ], [ [ -81.783946, 26.200018 ], [ -81.783782, 26.200054 ], [ -81.783598, 26.200095 ], [ -81.783516, 26.200099 ], [ -81.783409, 26.200106 ], [ -81.783196, 26.200133 ], [ -81.782647, 26.200117 ], [ -81.782444, 26.200073 ], [ -81.783946, 26.200018 ] ] ] ] }
It is that "tail" that is in the Golf Club that is killing it...
The question is, how do I load these multipolygons into BigQuery at scale? (Editing them one by one is not an option)
I took a look at the example geojsoin, it is an interesting one.
I does have three loops, but instead of describing them as multipolygon with three polygons (this is what multipolygons are for - when you have shape consisting of disjoint polygons, it should be described as multipolygon with a list of polygon shapes), it describes it as multipolygon with a single polygon, containing three loops. But loops in a polygon are interpreted as (shell, hole1, hole2, ..). So this is not a valid polygon - the additional loops in the polygon should be holes, but here they are outside of the shell.
What you want
Multipolygon / 3 Polygons / Each with 1 loop
What you have
Multipolygon / 1 polygon / With 3 loops
Or, in geojson, this is what you have:
"coordinates": [ /*start-of-multipolygon*/
[ /*start-of-polygon*/
[ /*first-loop*/ [..,..], .. [..,..] ],
[ /*second-loop*/ [..,..], .. [..,..] ],
[ /*third-loop*/ [..,..], .. [..,..] ]
]
]
What you need is:
"coordinates": [ /*start-of-multipolygon*/
[ /*start-of-polygon*/
[ /*first-loop*/ [..,..], .. [..,..] ]
],
[ /*start-of-polygon*/
[ /*second-loop*/ [..,..], .. [..,..] ]
],
[ /*start-of-polygon*/
[ /*third-loop*/ [..,..], .. [..,..] ]
]
]
For this example it is easy to "fix" the geojson by converting to the proper shape, this fixes the example string:
select st_geogfromgeojson(
REPLACE('<example geojson>', '] ], [ [', '] ] ], [ [ ['));
Of course, the fix is fragile and would be incorrect if the polygon has actual holes - it will convert them to independent shells, and they will disappear.