Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating BoundingBox for GeoJSON data

I'm drawing GeoJSON polygons on my map using the Android Map Utils that Google provides (compile 'com.google.maps.android:android-maps-utils:0.4'). However, the GeoJSON I get does not include a bounding box, so sometimes the map is way off. Here's an example of the polygon stuff I get back:

{"type":"Polygon","coordinates":[[[-88.138825,30.719378],[-88.138927,30.719386],[-88.139181,30.719409],[-88.139433,30.719429],[-88.139438,30.719429],[-88.139633,30.719446],[-88.139848,30.719461],[-88.140194,30.719487],[-88.140316,30.719495],[-88.140488,30.719505],[-88.140857,30.719522],[-88.141193,30.719544],[-88.141495,30.719559],[-88.141733,30.719571],[-88.142232,30.719596],[-88.142378,30.719602],[-88.142709,30.719616],[-88.143286,30.719639],[-88.143726,30.719657],[-88.14423,30.719677],[-88.144496,30.71969],[-88.144954,30.719712],[-88.146015,30.719757],[-88.146594,30.719782],[-88.146871,30.719794],[-88.147776,30.719835],[-88.148589,30.719868],[-88.148847,30.719876],[-88.149387,30.719903],[-88.150273,30.719944],[-88.151101,30.719981],[-88.151919,30.720016],[-88.152748,30.720054],[-88.153862,30.720098],[-88.154916,30.72014],[-88.156111,30.720194],[-88.157177,30.720241],[-88.157649,30.720266],[-88.158093,30.720292],[-88.158425,30.720314],[-88.158771,30.720349],[-88.15907,30.72038],[-88.159393,30.720415],[-88.160031,30.720494],[-88.160662,30.72057],[-88.161215,30.720637],[-88.161742,30.720696],[-88.162071,30.720736],[-88.16195,30.720897],[-88.161855,30.721036],[-88.161782,30.721148],[-88.161692,30.721293],[-88.161655,30.721359],[-88.161616,30.72142],[-88.161584,30.721479],[-88.161543,30.721555],[-88.161508,30.721632],[-88.161476,30.721714],[-88.161457,30.721782],[-88.161442,30.721843],[-88.161432,30.721883],[-88.161429,30.721898],[-88.161416,30.721954],[-88.161402,30.722014],[-88.161392,30.722065],[-88.161384,30.722116],[-88.161375,30.722182],[-88.16137,30.722242],[-88.161367,30.722323],[-88.161369,30.722429],[-88.161373,30.722566],[-88.161376,30.722761],[-88.161375,30.723117],[-88.161375,30.723257],[-88.16137,30.72343],[-88.161361,30.723634],[-88.16136,30.723863],[-88.161359,30.724048],[-88.161364,30.724241],[-88.161365,30.724525],[-88.16137,30.724805],[-88.161371,30.724934],[-88.161371,30.725025],[-88.161374,30.725131],[-88.161375,30.725532],[-88.161374,30.725576],[-88.161368,30.725633],[-88.161362,30.725683],[-88.16136,30.725726],[-88.161361,30.725776],[-88.161362,30.725839],[-88.161366,30.725911],[-88.161379,30.726238],[-88.161378,30.726573],[-88.161377,30.726897],[-88.161366,30.727084],[-88.161366,30.727149],[-88.161371,30.727222],[-88.161379,30.72733],[-88.161379,30.727437],[-88.161378,30.727548],[-88.161375,30.727627],[-88.161357,30.727818],[-88.161351,30.727923],[-88.161352,30.728014],[-88.161363,30.728186],[-88.16137,30.728395],[-88.161369,30.72872],[-88.161372,30.728957],[-88.161372,30.729211],[-88.161379,30.729351],[-88.161386,30.72945],[-88.161393,30.729556],[-88.161396,30.729655],[-88.161394,30.729824],[-88.161403,30.730129],[-88.161405,30.730268],[-88.161405,30.730457],[-88.161405,30.730763],[-88.161407,30.730923],[-88.161399,30.731075],[-88.161392,30.731214],[-88.161388,30.731399],[-88.161394,30.731602],[-88.1614,30.731923],[-88.161402,30.732116],[-88.161395,30.732547],[-88.161405,30.732797],[-88.161418,30.733154],[-88.161417,30.733291],[-88.161394,30.733911],[-88.161366,30.734341],[-88.161317,30.734959],[-88.161275,30.735447],[-88.161241,30.735907],[-88.161219,30.736167],[-88.161189,30.736516],[-88.161175,30.736742],[-88.161137,30.737199],[-88.161115,30.737806],[-88.161113,30.738298],[-88.161098,30.738838],[-88.161087,30.739417],[-88.161091,30.73989],[-88.161094,30.740051],[-88.161103,30.740272],[-88.161115,30.74039],[-88.161129,30.740519],[-88.161137,30.740623],[-88.161141,30.740794],[-88.161139,30.741026],[-88.16114,30.741196],[-88.161139,30.741207],[-88.160888,30.741207],[-88.160501,30.741239],[-88.160319,30.741246],[-88.160169,30.741256],[-88.160092,30.741259],[-88.160012,30.741266],[-88.15993,30.741275],[-88.159829,30.741294],[-88.159698,30.741321],[-88.159558,30.741336],[-88.159442,30.741339],[-88.159311,30.741336],[-88.159192,30.741321],[-88.159042,30.74129],[-88.15892,30.741243],[-88.158603,30.74109],[-88.157116,30.7401],[-88.156761,30.739874],[-88.156503,30.739646],[-88.156348,30.739506],[-88.156203,30.739375],[-88.155937,30.739118],[-88.155858,30.739047],[-88.155775,30.738987],[-88.155698,30.738943],[-88.155132,30.738695],[-88.154993,30.738633],[-88.154833,30.738573],[-88.154655,30.738522],[-88.153851,30.738351],[-88.153642,30.738305],[-88.153508,30.738289],[-88.153379,30.738265],[-88.153268,30.738247],[-88.153164,30.738224],[-88.153104,30.738193],[-88.153035,30.738138],[-88.152764,30.737898],[-88.152599,30.737736],[-88.152537,30.73767],[-88.152488,30.737632],[-88.152424,30.737614],[-88.152354,30.737621],[-88.152231,30.73763],[-88.151638,30.7377],[-88.151547,30.737709],[-88.151478,30.737711],[-88.151423,30.737709],[-88.151369,30.737685],[-88.150774,30.737301],[-88.150566,30.737133],[-88.150513,30.737064],[-88.150293,30.736767],[-88.150214,30.736663],[-88.150156,30.73658],[-88.150095,30.736495],[-88.150037,30.736417],[-88.15,30.736359],[-88.149954,30.736326],[-88.149898,30.736294],[-88.149803,30.736258],[-88.149727,30.736238],[-88.14966,30.736233],[-88.14956,30.736236],[-88.149547,30.73634],[-88.149498,30.736348],[-88.149468,30.736352],[-88.149436,30.73635],[-88.149407,30.736338],[-88.14937,30.736311],[-88.149318,30.736274],[-88.14929,30.736263],[-88.149257,30.736257],[-88.149196,30.736254],[-88.149123,30.736255],[-88.149061,30.736263],[-88.148859,30.736315],[-88.148731,30.736359],[-88.148678,30.736383],[-88.148622,30.736419],[-88.148565,30.736465],[-88.148503,30.736535],[-88.148415,30.736665],[-88.148374,30.736741],[-88.148346,30.73681],[-88.148313,30.736877],[-88.148247,30.736981],[-88.148121,30.737155],[-88.148033,30.737254],[-88.14795,30.737343],[-88.147721,30.737555],[-88.147602,30.737666],[-88.147521,30.737727],[-88.147463,30.737774],[-88.147416,30.737801],[-88.147367,30.737821],[-88.147322,30.737829],[-88.147279,30.737832],[-88.147238,30.737826],[-88.147191,30.737816],[-88.147026,30.737784],[-88.146803,30.73773],[-88.146348,30.737553],[-88.146272,30.737511],[-88.146226,30.737483],[-88.146193,30.737473],[-88.146167,30.737474],[-88.146125,30.73749],[-88.145879,30.737622],[-88.145622,30.737802],[-88.145505,30.73789],[-88.145414,30.737988],[-88.145306,30.738118],[-88.145167,30.738276],[-88.144963,30.738541],[-88.144876,30.738652],[-88.144831,30.738696],[-88.14479,30.738717],[-88.144736,30.738731],[-88.144685,30.738728],[-88.144623,30.738715],[-88.14458,30.738713],[-88.144556,30.738726],[-88.144535,30.738753],[-88.144512,30.738792],[-88.144464,30.738838],[-88.144244,30.739018],[-88.143998,30.739253],[-88.143962,30.739305],[-88.143922,30.739366],[-88.143807,30.739526],[-88.143768,30.739569],[-88.143726,30.739605],[-88.143664,30.73964],[-88.14321,30.739782],[-88.143043,30.73985],[-88.142966,30.739892],[-88.142928,30.739925],[-88.142899,30.739965],[-88.142874,30.740014],[-88.142602,30.740543],[-88.142572,30.740602],[-88.142543,30.740646],[-88.142503,30.740694],[-88.142447,30.740732],[-88.141954,30.741003],[-88.141234,30.741423],[-88.141177,30.741444],[-88.141108,30.741462],[-88.141054,30.741471],[-88.141001,30.741468],[-88.14095,30.741466],[-88.140911,30.741462],[-88.140877,30.741462],[-88.140855,30.74147],[-88.14084,30.741487],[-88.140826,30.741517],[-88.140822,30.741548],[-88.14082,30.741635],[-88.140821,30.74167],[-88.140819,30.741694],[-88.140809,30.741714],[-88.140794,30.74173],[-88.140756,30.741739],[-88.140718,30.741746],[-88.140685,30.741745],[-88.140645,30.741735],[-88.140587,30.74172],[-88.140534,30.741703],[-88.140486,30.741683],[-88.140448,30.741667],[-88.140417,30.741666],[-88.140399,30.741673],[-88.140387,30.741691],[-88.140377,30.741716],[-88.140355,30.741772],[-88.140273,30.741933],[-88.140254,30.741987],[-88.140244,30.742031],[-88.140244,30.742063],[-88.140243,30.742087],[-88.140232,30.7421],[-88.140207,30.74211],[-88.140166,30.74211],[-88.140108,30.742097],[-88.139973,30.742066],[-88.139942,30.74206],[-88.139915,30.742066],[-88.139886,30.742079],[-88.139836,30.742119],[-88.138954,30.742806],[-88.13627,30.742502],[-88.136308,30.74465],[-88.136214,30.745348],[-88.136201,30.745379],[-88.136179,30.745403],[-88.136148,30.745425],[-88.136096,30.745434],[-88.136049,30.745436],[-88.135818,30.745406],[-88.135776,30.745408],[-88.13573,30.745412],[-88.13567,30.745425],[-88.135535,30.745464],[-88.135498,30.745462],[-88.135474,30.745455],[-88.135451,30.74544],[-88.13543,30.745413],[-88.135409,30.745397],[-88.135386,30.745391],[-88.135358,30.745395],[-88.135343,30.745408],[-88.135296,30.745461],[-88.135252,30.745505],[-88.13521,30.74554],[-88.135172,30.745567],[-88.135127,30.745607],[-88.135075,30.745657],[-88.13505,30.745692],[-88.135024,30.745738],[-88.135006,30.745784],[-88.134999,30.745832],[-88.135006,30.745881],[-88.135033,30.745944],[-88.135064,30.746002],[-88.135082,30.746037],[-88.135088,30.746067],[-88.135087,30.7461],[-88.135075,30.746132],[-88.135054,30.746173],[-88.13503,30.746208],[-88.135009,30.74624],[-88.134995,30.746273],[-88.134986,30.746288],[-88.134975,30.746301],[-88.134962,30.746307],[-88.134948,30.746309],[-88.134932,30.746306],[-88.134912,30.746297],[-88.134883,30.746283],[-88.134854,30.746268],[-88.134813,30.746248],[-88.134764,30.746218],[-88.134665,30.746148],[-88.134589,30.746089],[-88.13453,30.746043],[-88.134507,30.746028],[-88.134482,30.746014],[-88.134452,30.746004],[-88.134427,30.745998],[-88.134394,30.745997],[-88.13436,30.745997],[-88.134328,30.746],[-88.134291,30.746007],[-88.134248,30.74602],[-88.134203,30.746035],[-88.134159,30.746056],[-88.134102,30.746087],[-88.134048,30.746129],[-88.134002,30.74618],[-88.13396,30.746239],[-88.133947,30.746269],[-88.133938,30.746293],[-88.133934,30.746327],[-88.133932,30.746364],[-88.133933,30.746407],[-88.133934,30.746453],[-88.133934,30.746503],[-88.133936,30.746522],[-88.133944,30.746541],[-88.133958,30.746559],[-88.133974,30.74658],[-88.133984,30.746599],[-88.133987,30.746617],[-88.133986,30.74663],[-88.133982,30.746644],[-88.133972,30.746655],[-88.133952,30.746665],[-88.133927,30.746673],[-88.133868,30.746683],[-88.133782,30.746697],[-88.133746,30.746706],[-88.133721,30.74672],[-88.133704,30.746736],[-88.133691,30.746755],[-88.133683,30.746786],[-88.133679,30.746821],[-88.133678,30.746862],[-88.133683,30.746887],[-88.133694,30.746909],[-88.133715,30.746928],[-88.133755,30.746946],[-88.133791,30.746962],[-88.133813,30.746978],[-88.133833,30.746997],[-88.133843,30.747014],[-88.13385,30.747031],[-88.133852,30.747048],[-88.133849,30.747062],[-88.133836,30.747073],[-88.133814,30.747079],[-88.133791,30.747081],[-88.133746,30.747084],[-88.133707,30.747091],[-88.133669,30.747101],[-88.133647,30.747106],[-88.133628,30.747114],[-88.133617,30.747124],[-88.133611,30.747141],[-88.133608,30.747156],[-88.133608,30.747178],[-88.133607,30.747197],[-88.133604,30.747217],[-88.133593,30.747233],[-88.133571,30.747252],[-88.133548,30.747262],[-88.133522,30.747272],[-88.133498,30.747282],[-88.13347,30.747299],[-88.133456,30.747313],[-88.133449,30.747328],[-88.133449,30.747346],[-88.133453,30.747362],[-88.133464,30.747374],[-88.133476,30.747386],[-88.133491,30.747401],[-88.133506,30.747417],[-88.133517,30.747432],[-88.133525,30.747447],[-88.133529,30.747462],[-88.133527,30.747477],[-88.13352,30.747489],[-88.133503,30.747498],[-88.13348,30.747506],[-88.13345,30.74751],[-88.133427,30.747509],[-88.133399,30.747503],[-88.133372,30.747496],[-88.133351,30.747494],[-88.133332,30.747499],[-88.133314,30.74751],[-88.133301,30.747526],[-88.133293,30.74754],[-88.133286,30.747556],[-88.133284,30.747574],[-88.133291,30.747588],[-88.133304,30.747603],[-88.133322,30.747616],[-88.133348,30.747629],[-88.133371,30.747642],[-88.133386,30.747657],[-88.1334,30.747673],[-88.133412,30.747691],[-88.133431,30.74772],[-88.133439,30.747737],[-88.133443,30.747755],[-88.13344,30.747771],[-88.133431,30.747783],[-88.133417,30.747797],[-88.1334,30.747805],[-88.13338,30.747809],[-88.133355,30.747808],[-88.133336,30.747808],[-88.133317,30.747812],[-88.133298,30.747818],[-88.133287,30.747826],[-88.133274,30.747841],[-88.133262,30.747858],[-88.133256,30.747877],[-88.133254,30.747895],[-88.133252,30.747924],[-88.133255,30.747949],[-88.133255,30.747974],[-88.133252,30.747997],[-88.133235,30.748034],[-88.133161,30.748169],[-88.133074,30.748355],[-88.133054,30.748379],[-88.13303,30.748402],[-88.133008,30.748431],[-88.132998,30.748448],[-88.132995,30.748465],[-88.132998,30.748482],[-88.133009,30.748495],[-88.133025,30.74851],[-88.133043,30.748522],[-88.133281,30.748692],[-88.133292,30.748704],[-88.133301,30.748718],[-88.133305,30.748732],[-88.133307,30.748753],[-88.133305,30.748775],[-88.133268,30.748966],[-88.133026,30.74896],[-88.132821,30.748958],[-88.132613,30.748957],[-88.132587,30.748957],[-88.132415,30.748956],[-88.132216,30.748957],[-88.131968,30.748957],[-88.131798,30.748957],[-88.13151,30.748954],[-88.131175,30.748955],[-88.130916,30.748954],[-88.130557,30.748947],[-88.130256,30.748935],[-88.129954,30.748931],[-88.129681,30.74893],[-88.129429,30.748925],[-88.129164,30.748924],[-88.128922,30.748923],[-88.12873,30.748925],[-88.128528,30.748931],[-88.128394,30.748941],[-88.128247,30.748951],[-88.128121,30.748968],[-88.127974,30.748994],[-88.127815,30.74903],[-88.127699,30.749062],[-88.127582,30.749098],[-88.127452,30.749143],[-88.127352,30.749184],[-88.127239,30.749233],[-88.127153,30.749273],[-88.127052,30.749329],[-88.126969,30.749378],[-88.126867,30.749446],[-88.126642,30.749584],[-88.126565,30.749631],[-88.126501,30.74967],[-88.126465,30.749635],[-88.126393,30.749565],[-88.12631,30.749473],[-88.126284,30.749443],[-88.126025,30.749132],[-88.125765,30.748829],[-88.1255,30.748507],[-88.125263,30.74823],[-88.125081,30.748001],[-88.124845,30.747717],[-88.124636,30.747471],[-88.124415,30.747191],[-88.124196,30.746919],[-88.124007,30.746693],[-88.123794,30.746433],[-88.123611,30.746229],[-88.123424,30.74604],[-88.123198,30.745811],[-88.122975,30.745596],[-88.122777,30.745397],[-88.122578,30.745196],[-88.122378,30.744998],[-88.122214,30.744832],[-88.1221,30.744722],[-88.12204,30.744673],[-88.122015,30.744642],[-88.122143,30.744548],[-88.122426,30.744346],[-88.122829,30.744058],[-88.123216,30.743779],[-88.123609,30.743497],[-88.1243,30.743009],[-88.124694,30.742722],[-88.125184,30.742375],[-88.125495,30.742154],[-88.125749,30.741984],[-88.125812,30.741944],[-88.125853,30.741923],[-88.125916,30.741888],[-88.12598,30.741854],[-88.126057,30.741816],[-88.126132,30.74178],[-88.126229,30.74174],[-88.126365,30.741689],[-88.126533,30.741637],[-88.126669,30.741601],[-88.126819,30.741569],[-88.127013,30.741531],[-88.127204,30.741496],[-88.127485,30.741447],[-88.127809,30.741395],[-88.128057,30.741354],[-88.128303,30.741314],[-88.128523,30.741277],[-88.12868,30.741249],[-88.128877,30.741213],[-88.129051,30.74118],[-88.129196,30.741152],[-88.129351,30.741118],[-88.129429,30.7411],[-88.129507,30.741079],[-88.12957,30.741062],[-88.129657,30.741035],[-88.129733,30.74101],[-88.12983,30.740976],[-88.129934,30.740935],[-88.130045,30.740887],[-88.130175,30.740828],[-88.130271,30.740779],[-88.130404,30.740706],[-88.130489,30.740654],[-88.130607,30.740576],[-88.130706,30.740504],[-88.130808,30.740425],[-88.130907,30.740346],[-88.131029,30.74025],[-88.131152,30.740148],[-88.131271,30.740052],[-88.131386,30.739958],[-88.131507,30.73986],[-88.131618,30.739773],[-88.131731,30.739683],[-88.131842,30.739591],[-88.131943,30.739506],[-88.132126,30.73936],[-88.132246,30.739261],[-88.132341,30.739182],[-88.132473,30.739076],[-88.132566,30.738999],[-88.132734,30.738863],[-88.13287,30.738753],[-88.133011,30.738637],[-88.133163,30.738513],[-88.133349,30.738364],[-88.133494,30.738243],[-88.133568,30.73818],[-88.13367,30.738096],[-88.1338,30.737986],[-88.133929,30.737873],[-88.134052,30.737761],[-88.134187,30.737638],[-88.134295,30.73754],[-88.134375,30.737464],[-88.134474,30.737368],[-88.13463,30.737219],[-88.134764,30.737089],[-88.134851,30.736994],[-88.135026,30.736815],[-88.135117,30.73672],[-88.135211,30.736618],[-88.135293,30.736531],[-88.135386,30.736425],[-88.135451,30.736348],[-88.135508,30.736276],[-88.135562,30.736201],[-88.135617,30.736119],[-88.135665,30.736037],[-88.135702,30.735961],[-88.13574,30.735878],[-88.135778,30.735772],[-88.135815,30.735652],[-88.135861,30.735483],[-88.135916,30.735259],[-88.135946,30.73514],[-88.136003,30.734895],[-88.13606,30.734662],[-88.136106,30.734482],[-88.136152,30.734293],[-88.136193,30.734115],[-88.136235,30.73391],[-88.136264,30.733754],[-88.136282,30.733621],[-88.136304,30.733441],[-88.136313,30.733312],[-88.136321,30.733103],[-88.136323,30.732956],[-88.136319,30.732683],[-88.136309,30.732458],[-88.136301,30.732264],[-88.13629,30.731981],[-88.136283,30.731762],[-88.136275,30.731519],[-88.136262,30.731154],[-88.136251,30.730828],[-88.136247,30.730594],[-88.13624,30.730347],[-88.136232,30.730142],[-88.13623,30.73002],[-88.136224,30.729878],[-88.13622,30.729688],[-88.136211,30.729404],[-88.136205,30.729143],[-88.136197,30.728916],[-88.13619,30.728696],[-88.136177,30.728474],[-88.136167,30.728194],[-88.136158,30.727943],[-88.136151,30.727669],[-88.136147,30.727423],[-88.136146,30.727202],[-88.136149,30.727029],[-88.136151,30.726858],[-88.136157,30.726651],[-88.136161,30.726376],[-88.136163,30.726128],[-88.136164,30.725844],[-88.136167,30.725572],[-88.136164,30.725318],[-88.136159,30.725085],[-88.13614,30.724638],[-88.136118,30.724255],[-88.136095,30.723822],[-88.136083,30.723563],[-88.136071,30.723161],[-88.136059,30.722664],[-88.136056,30.722224],[-88.136062,30.721896],[-88.136078,30.721565],[-88.136093,30.72123],[-88.136111,30.720908],[-88.13612,30.720527],[-88.136121,30.720172],[-88.136113,30.719843],[-88.136106,30.719487],[-88.136099,30.719248],[-88.136101,30.718929],[-88.136104,30.718792],[-88.136117,30.718637],[-88.136126,30.718554],[-88.13615,30.718471],[-88.136194,30.718375],[-88.136261,30.718437],[-88.136446,30.718573],[-88.136579,30.718667],[-88.13671,30.718751],[-88.136846,30.718833],[-88.136979,30.718902],[-88.137137,30.718979],[-88.137278,30.719042],[-88.137451,30.719109],[-88.137627,30.719169],[-88.137789,30.719215],[-88.13794,30.719255],[-88.138076,30.719286],[-88.138215,30.719312],[-88.138222,30.719313],[-88.138393,30.719336],[-88.138544,30.719353],[-88.138718,30.719371],[-88.138825,30.719378]]]}

And I'm just adding it to the map by doing:

            geoJsonObject = new JSONObject(response);
            GeoJsonLayer layer = new GeoJsonLayer(getMap(), geoJsonObject);
            layer.addLayerToMap();

However, because there are no "bounds" supplied in the KVP data, so any time I try to do, layer.getBoundingBox(), I always get a return value of null. Is there a way to generate the bounding box myself once I know the coordinates?

like image 369
RyanInBinary Avatar asked May 27 '16 15:05

RyanInBinary


People also ask

What is BBOX in GeoJSON?

A GeoJson object MAY have a member named "bbox" to include information on the coordinate range for its Geometries, Features, or FeatureCollections. This class simplifies the build process for creating a bounding box and working with them when deserialized.

What type of geometry is the GeoJSON data?

GeoJSON is a format for encoding a variety of geographic data structures. GeoJSON supports the following geometry types: Point , LineString , Polygon , MultiPoint , MultiLineString , and MultiPolygon . Geometric objects with additional properties are Feature objects.

What are GeoJSON coordinates?

GeoJSON describes an order for coordinates: they should go, in order: [longitude, latitude, elevation] This order can be surprising. Historically, the order of coordinates is usually “latitude, longitude”, and many people will assume that this is the case universally.

What is GeoJSON object?

A GeoJSON object may represent a region of space (a Geometry), a spatially bounded entity (a Feature), or a list of Features (a FeatureCollection). GeoJSON supports the following geometry types: Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection.


1 Answers

Looking at the sources of GeoJsonLayer.java and GeoJsonParser.java it's obvious that there's no code to calculate the bounding box. Even the parseFeatureCollection() method just parses the bounding box if it's in the GeoJSON data.

However the bounding box is a LatLngBounds object and the class has a nested Builder class which can be used to build a LatLngBounds object:

// Get the bounding box builder.
LatLngBounds.Builder builder = LatLngBounds.builder();

// Get the coordinates
for (GeoJsonFeature feature : layer.getFeatures()) {
    if (feature.hasGeometry()) {
        GeoJsonGeometry geometry = feature.getGeometry();

        List<? extends List<LatLng>> lists =
                ((GeoJsonPolygon) geometry).getCoordinates();

        // Feed the coordinates to the builder.
        for (List<LatLng> list : lists) {
            for (LatLng latLng : list) {
                builder.include(latLng);
            }
        }
    }
}

// Assign the builder's return value to a bounding box.
LatLngBounds bb = builder.build();

So, if you have only GeoJsonPolygons to draw it's not that much hassle to get a bounding box.

For a more complete solution you would have to build a more elaborate mechanism to extract the coordinates from any GeoJSON geometry object type. They are all implementations of the GeoJsonGeometry interface. The first thing is to differentiate between GeoJsonGeometryCollection and all the rest:

private LatLngBounds getLatLngBoundingBox(GeoJsonLayer layer) {

    List<LatLng> coordinates = new ArrayList<>();

    for (GeoJsonFeature feature : layer.getFeatures()) {

        if (feature.hasGeometry()) {
            GeoJsonGeometry geometry = feature.getGeometry();

            if (geometry.getType().equals("GeometryCollection")) {
                List<GeoJsonGeometry> geometries =
                        ((GeoJsonGeometryCollection)geometry).getGeometries();

                for (GeoJsonGeometry geom : geometries) {
                    coordinates.addAll(getCoordinatesFromGeometry(geom));
                }
            }
            else {
                coordinates.addAll(getCoordinatesFromGeometry(geometry));
            }
        }
    }

    // Get the bounding box builder.
    LatLngBounds.Builder builder = LatLngBounds.builder();

    // Feed the coordinates to the builder.
    for (LatLng latLng : coordinates) {
        builder.include(latLng);
    }

    LatLngBounds boundingBoxFromBuilder = builder.build();

    //return boundingBox;
    return boundingBoxFromBuilder;
}

The actual code (may have bugs) for extracting the coordinates would be something like:

private List<LatLng> getCoordinatesFromGeometry(GeoJsonGeometry geometry) {

    List<LatLng> coordinates = new ArrayList<>();

    // GeoJSON geometry types:
    // http://geojson.org/geojson-spec.html#geometry-objects

    switch (geometry.getType()) {
        case "Point":
            coordinates.add(((GeoJsonPoint) geometry).getCoordinates());
            break;
        case "MultiPoint":
            List<GeoJsonPoint> points = ((GeoJsonMultiPoint) geometry).getPoints();
            for (GeoJsonPoint point : points) {
                coordinates.add(point.getCoordinates());
            }
            break;
        case "LineString":
            coordinates.addAll(((GeoJsonLineString) geometry).getCoordinates());
            break;
        case "MultiLineString":
            List<GeoJsonLineString> lines =
                    ((GeoJsonMultiLineString) geometry).getLineStrings();
            for (GeoJsonLineString line : lines) {
                coordinates.addAll(line.getCoordinates());
            }
            break;
        case "Polygon":
            List<? extends List<LatLng>> lists =
                    ((GeoJsonPolygon) geometry).getCoordinates();
            for (List<LatLng> list : lists) {
                coordinates.addAll(list);
            }
            break;
        case "MultiPolygon":
            List<GeoJsonPolygon> polygons =
                    ((GeoJsonMultiPolygon) geometry).getPolygons();
            for (GeoJsonPolygon polygon : polygons) {
                for (List<LatLng> list : polygon.getCoordinates()) {
                    coordinates.addAll(list);
                }
            }
            break;
    }

    return coordinates;
}

You would then use the code like:

LatLngBounds bb = getLatLngBoundingBox(layer);
like image 171
Markus Kauppinen Avatar answered Sep 28 '22 05:09

Markus Kauppinen