how to filter a JSON file? (NYC Census Tracts) - json

I am adapting an existing D3JS map-drawing file to work with a different JSON file. In the file 'ny.json' there are things called "objects" and inside of those there are things called "tracts" so I get how the references work for the current (working) code, which comes from here and looks like:
d3.json("./shapefiles/ny.json", function(error, ny) {
if (error) throw error;
var tracts = ny.objects.tracts;
tracts.geometries = tracts.geometries
.filter(function(d) { return (d.id / 10000 | 0) !== 99; });
svg.append("g")
.selectAll("path")
.data(topojson.feature(ny, tracts).features)
.enter().append("path")
.attr("class", "tract")
.attr("d", path)
However, the new JSON file doesn't have those 'objects' or 'tracts' and I'm trying to figure out how to filter it so that it can be used to draw a map not of New York State (as in the first one) but just of the Bronx county. It's possible that the two files are too different in structure that an easy filter change isn't possible, but I thought I would ask in case there's another solution. I haven't seen the 'meta' features in json before.

I am afraid it is not possible to display only Bronx in the map because census tract ids in ny.json are unique only at borough level. For instance, census tract 014300 appears in Bronx, Brooklyn, Manhattan, and Queens. ny.json stores only census tract ids in ny.objects.tracts.id, and not their FIPS County Codes.
This makes filtering on Borough name impossible: https://bl.ocks.org/memoryfull/ba6121d19f80f85f0beb
Even if the filtering was feasible, it would be suboptimal. Instead, I would create own GeoJSON of Bronx in any GIS software with census tract ids and then call it in your d3 code.

Related

Gets all polygons from a json file

I have a json file, with the coordinates of several polygons. What I want to do is automate the getting the coordinates but saving them separately by polygons. In the file that I currently use, I have 2 polygons:
{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[
[[[0.03990083369766674,41.65117297365802],[0.03986585098450487,41.6511738730363],[0.0398193071116094,41.65118861345209],[0.03978640897789055,41.651211227828185],[0.039785212880236676,41.65121205009294],[0.039761889395081365,41.65124414021116],[0.03975127958141444,41.65127097990489],[0.03973309671778063,41.65131697476312],[0.039706482497244025,41.651425988964064],[0.03969658765996025,41.65146652049745],[0.039683272167788275,41.65150292059053],[0.03968380609505212,41.65151410540278],[0.03968455879000179,41.6515298809837],[0.03969278143750318,41.65153853529924],[0.03969588944738445,41.651541805918015],[0.03971376972429572,41.65156062329174],[0.039752407785648564,41.65158215221135],[0.03979465677110295,41.65160350427274],[0.03988602119749911,41.651632606242316],[0.03992955596697664,41.65164948823451],[0.03993958742929026,41.65165619711021],[0.039968351608118506,41.651675434417314],[0.03999355934521909,41.651701840766634],[0.04000585559790465,41.65173745966629],[0.04000155651951174,41.65177364266802],[0.03998752269819514,41.65181002432095],[0.03993384247251406,41.65191499844431],[0.0399064931590255,41.65198778102845],[0.03987869457550017,41.65209657478536],[0.03987154648804941,41.65213727647139],[0.03986267424301351,41.65216458136081],[0.039847923768933036,41.65220997439829],[0.03980755819723356,41.65227854926681],[0.03978735697119576,41.65231062932676],[0.0397695957173166,41.65234709479867],[0.039757844288369676,41.65238344543852],[0.03975297859328857,41.65242411611149],[0.03975461138812371,41.65245838049552],[0.039754692692589313,41.652460093756645],[0.03976241996958375,41.65249586520693],[0.039775312175620324,41.65253159055675],[0.039792208417040606,41.65257165102716],[0.0398126778639959,41.65260712072893],[0.03984532034965278,41.652678297340344],[0.039888420936520154,41.652749203216246],[0.039899750755712435,41.652784978019326],[0.03990246230155005,41.65282539304425],[0.03990100971764384,41.65284035977143],[0.03989973315371473,41.652853510978275],[0.03989900644266644,41.65286150731441],[0.03988340855812481,41.65289793925871],[0.03982299431103434,41.65300310029855],[0.03980279308499652,41.653035180358444],[0.039796679324458795,41.65304764676378],[0.03978491196989489,41.653071643315826],[0.0397440929372068,41.65317631904341],[0.0397286140756973,41.65321275350229],[0.03970857042944851,41.65324925082547],[0.03966616386420511,41.6533061086308],[0.03966059408921564,41.6533135760688],[0.03962721818698753,41.653337031149846],[0.039608159414420085,41.65335700606448],[0.03959644067489746,41.653369288906106],[0.039574675385634625,41.65340141925745],[0.03955691413175548,41.653437884729385],[0.03954504200339567,41.653474232016464],[0.03952742659464041,41.65350945529025],[0.03952465972823856,41.65351498818487],[0.03951080444147017,41.653542693729264],[0.03949758031204822,41.6535615806728],[0.03948831998487233,41.6535748056404],[0.03938851917243118,41.65370353659338],[0.039367413538987255,41.65375064205381],[0.039358610025561444,41.653770290912405],[0.039341569615398024,41.653808324638504],[0.03932925156976292,41.65384916294957],[0.039294849722525456,41.653976004621455],[0.03927882436089661,41.65403509285139],[0.03927713624549725,41.654056445750996],[0.03927596780812548,41.65407122304717],[0.039281407663608055,41.65410711603502],[0.039310323554892744,41.654178376465495],[0.03936399288409906,41.65425614795873],[0.039382605739391195,41.65428311924833],[0.03939218541709694,41.654297000518994],[0.0394054858218423,41.65431627386832],[0.03942630395599031,41.65434644117781],[0.039450928312595376,41.6544222747749],[0.03945708817360331,41.65445818620299],[0.039456164487818746,41.654516564485675],[0.03945594907289441,41.654530202681144],[0.039453803305554584,41.65455188918051],[0.03945192324456115,41.65457089514709],[0.03943704788011598,41.65460725500702],[0.03941627919919963,41.65464382357635],[0.03934394253232732,41.654732627331185],[0.0393319027658941,41.654747407980096],[0.039311307590383746,41.65477269199252],[0.03926260956824479,41.6548369987957],[0.03925610688737672,41.65484747114613],[0.03921995489865273,41.65490569352545],[0.03921109941742416,41.65492864736862],[0.0392059194009554,41.6549420751783],[0.03919861708647711,41.65497826991475],[0.03919375139139601,41.65501894058772],[0.039193012107492216,41.655039557556165],[0.03919245806365955,41.655055020492085],[0.03919789791914211,41.655090913479995],[0.03921464077172523,41.65512646783898],[0.03923784355746767,41.65515732497917],[0.03926289874392143,41.65517922521707],[0.03928896814071942,41.655192054558825],[0.03933606186649572,41.65518633390957],[0.0394175440284636,41.655148178645845],[0.039442782778607764,41.65513278192702],[0.03945621058828894,41.65512459029256],[0.039527098862206865,41.65506869053877],[0.039586379037730636,41.65501163156773],[0.039623324792732284,41.654976069665025],[0.039725422246779336,41.654896838882365],[0.03976381052740279,41.654868829913084],[0.03980034640731491,41.65485428311925],[0.039848573366467556,41.654835081854316],[0.039941219385935034,41.654810092046716],[0.039994916375423455,41.6547901758054],[0.04003271457002846,41.654776157071524],[0.04003523752303347,41.65477534486507],[0.04007854095196967,41.65476139737734],[0.04011864668465535,41.654737845904435],[0.040155591601466624,41.654709888902886],[0.040182720470888646,41.654686606489065],[0.040188089918380375,41.654681998118434],[0.040242230310573,41.65462206415444],[0.04027888772808837,41.654571541391874],[0.040315545145603725,41.6545210177911],[0.04037410196284068,41.65445642432684],[0.04040103721028788,41.65442415735046],[0.040431813884187574,41.654391899594174],[0.04046347317254383,41.65436398701678],[0.04046785858454457,41.65436054037798],[0.04047288018303395,41.65435659333954],[0.040498973887352574,41.65433608282126],[0.04050050609934366,41.65433434776722],[0.04052434842433622,41.65430734714093],[0.040555966641364495,41.65427153964842],[0.04057773193062733,41.65423940929708],[0.04060973320065338,41.65417113617707],[0.04065305339339694,41.65410239115596],[0.04065650087037687,41.6540996050112],[0.04068771927060004,41.65407437631933],[0.04072866319365282,41.65405084580112],[0.040770455365357124,41.654027156026785],[0.040860539036872326,41.65399777745441],[0.040907804591673896,41.653983054640634],[0.04095089763482795,41.65397290750806],[0.04100058555979046,41.6539760792204],[0.04104998095631486,41.65398824890633],[0.04109435643073061,41.65400515185328],[0.04114391192161512,41.65403052052292],[0.04116956473780064,41.65404365328962],[0.04119017248616651,41.65406561806819],[0.04119417065421764,41.6541015638621],[0.04119530388759398,41.65412534919008],[0.04119588475351839,41.65413754066901],[0.04120276964919386,41.65417338085089],[0.04122281245725229,41.65419983413889],[0.04124421732465728,41.654211061698874],[0.04126361975527523,41.654221238168105],[0.04131172601501501,41.654237967609646],[0.04136353875255816,41.65424896382905],[0.04138485477178423,41.65425348754249],[0.04140638955869612,41.654258057356344],[0.04145406834134867,41.65426576954596],[0.04160685871060841,41.654275199187566],[0.04165954987165629,41.65427835748889],[0.04181220445407655,41.65426977106674],[0.041937059614781114,41.65426650044793],[0.04196128164000998,41.654265865937816],[0.04198126661293308,41.65426063060079],[0.04198913302952844,41.65425856949068],[0.04198995948523041,41.654258353237566],[0.04199706314859171,41.65425649161676],[0.04201822074983839,41.654250948663844],[0.04202009075254742,41.65425045916069],[0.04202343094116037,41.65424958408994],[0.042057054109546785,41.65424077554738],[0.042151707594943494,41.65421124274798],[0.04219497246712282,41.65419209345086],[0.04220322026033522,41.65418708861617],[0.04227241455152121,41.65414509695515],[0.04227500791251706,41.65414385391884],[0.042302370637051494,41.65413073875416],[0.042353914315486796,41.6541248613633],[0.042403152132222184,41.65413261378603],[0.042413530605347784,41.654134562578605],[0.042414217083258446,41.65413469165995],[0.04243573091541116,41.65413873089932],[0.042487086839204234,41.6541483734413],[0.04250871131248541,41.654161268161914],[0.04251341356044492,41.65417922052319],[0.04249180752735182,41.65421576813779],[0.04243910882259064,41.654275740658534],[0.042402066675696765,41.6543058694113],[0.04224077202697229,41.65443706045298],[0.042230817678172446,41.654445157371924],[0.042212416885043194,41.65446012326092],[0.042203314137656336,41.654467526996456],[0.042190945800599206,41.654477586957256],[0.04216446569052133,41.65449912509689],[0.042158375399313885,41.65450340238232],[0.04212451502305359,41.65452718519583],[0.04208356690904897,41.65455080623855],[0.04195181511800379,41.65460378908983],[0.04190542295756477,41.65462303729345],[0.04185803335058887,41.654637846440835],[0.04181464861718705,41.65465699238521],[0.0416459468801884,41.6547380219244],[0.041602715535623766,41.654761673980175],[0.041584732161297336,41.6547697449152],[0.0415343996679425,41.65479233414561],[0.041517508455664424,41.65479991473927],[0.041206645441451396,41.65492519234792],[0.041072091580008956,41.654967805108015],[0.041067007117242045,41.654969415271694],[0.04098074475561051,41.65500777505386],[0.0409415702524227,41.655031294675524],[0.040926787088917914,41.655043202846116],[0.04091264346466323,41.65505459552958],[0.04090137650974849,41.65507725684432],[0.04089677233006193,41.655086517171526],[0.04089737917988772,41.65510847356819],[0.04089776642383733,41.65512247553826],[0.040903428399767176,41.65513149865757],[0.04091724010063649,41.65515350702202],[0.04094500934749897,41.655184301297936],[0.04096908636579433,41.65521968382788],[0.04098133567981932,41.65523302865674],[0.04105383830838432,41.65531201552599],[0.04109895139032312,41.655378379248276],[0.04112916228572502,41.65541355977439],[0.04115775631190874,41.65544364997035],[0.04118758834526468,41.65547504355237],[0.04120909547189447,41.65548727945534],[0.041225389892631165,41.65549654984082],[0.04127064379055165,41.655517888491175],[0.04131000940114316,41.65553934616457],[0.041343394523465304,41.655565511953284],[0.04137345119183965,41.65559618552976],[0.04137836801653314,41.65560232024504],[0.04139821468804566,41.65562708290304],[0.04142181226145102,41.65566441590201],[0.04177131668977836,41.6555764746451],[0.04208765141070791,41.65549688008781],[0.04280846076062087,41.655315512456184],[0.04502639796742188,41.65475740088567],[0.04503264248565688,41.65475582927874],[0.04491913726071341,41.65471054688234],[0.04481832056154731,41.65463496222779],[0.04490633893199795,41.65458509157733],[0.04516969080163186,41.654501499690184],[0.0454428671005587,41.654456392475595],[0.04567295370909296,41.65444145676145],[0.04576299295651871,41.65449938493592],[0.04582947067272489,41.65455530648262],[0.04584204604280269,41.65455214147582],[0.04669402212688459,41.6543377323799],[0.04674658839756776,41.65432450405953],[0.04758604107937462,41.6541131971062],[0.04747183177452276,41.65406386792671],[0.04745190379854463,41.6540552605498],[0.04730123069815222,41.653990180935125],[0.04693486523240001,41.653832617071714],[0.04660293514149995,41.653688814617794],[0.046272875891499275,41.65354614120625],[0.04589455364016018,41.65338232192815],[0.04559773953469034,41.65325319954027],[0.04547678028281212,41.65320111690542],[0.04547537212299538,41.65320051089381],[0.04545764355854056,41.65319287749412],[0.04542093836417426,41.65317707257657],[0.0454185159940133,41.65317602902954],[0.04540317375753365,41.653169423251285],[0.04538960261729971,41.653163579388035],[0.04530522534580381,41.65312724886476],[0.045198634353198666,41.65308073013756],[0.044926965120551894,41.65296216727194],[0.044682048409852286,41.65285887288209],[0.044474430332298885,41.65278022380352],[0.04404397599288677,41.65262487360093],[0.043744324613024264,41.65251593567527],[0.04336616161785493,41.652380391072825],[0.04304298726352973,41.652263371315655],[0.04280758401349688,41.652177968937366],[0.042621828455288904,41.6521115809075],[0.042493676691870486,41.65206577967129],[0.04213919341276249,41.651937684904794],[0.04190932641010441,41.65185728652298],[0.04175594344025513,41.65180033484029],[0.041663035905393116,41.651765838277555],[0.04139794143798598,41.65166988811163],[0.041386050031247734,41.65166558400412],[0.04132406082445745,41.65164317163189],[0.04112134448417094,41.651569877751626],[0.041083383680634716,41.65155590847098],[0.040914122032470836,41.65149362003021],[0.04079188789304423,41.65144863770598],[0.04053546785774635,41.651355575105924],[0.04031697006922782,41.65127609705709],[0.040218658721069014,41.65124693138503],[0.040136785962390065,41.65122264230462],[0.04003391569682453,41.65119818390971],[0.03995920862759403,41.6511804218176],[0.03990083369766674,41.65117297365802]]],
Here starts second polygon
[[[0.04758604107937462,41.6541131971062],[0.04674658839756776,41.65432450405953],[0.04669402212688459,41.6543377323799],[0.04584204604280269,41.65455214147582],[0.04582947067272489,41.65455530648262],[0.04595265951097963,41.65465893279591],[0.04610042744355961,41.65482261628716],[0.04614809197697591,41.654895094608236],[0.045911219379229505,41.654906380003325],[0.045561253108009875,41.65490780408874],[0.045443292901265184,41.65490303143281],[0.04522180361132643,41.654831294910224],[0.04503264248565688,41.65475582927874],[0.04502639796742188,41.65475740088567],[0.04280846076062087,41.655315512456184],[0.04208765141070791,41.65549688008781],[0.04177131668977836,41.6555764746451],[0.04142181226145102,41.65566441590201],[0.0414310650449136,41.65567905489678],[0.04144861088386839,41.65570681408536],[0.041465396484160214,41.655733370470756],[0.04149721083773439,41.655762378563],[0.04149725945277568,41.65576451343384],[0.04150375878088228,41.65605208229957],[0.041496843710353594,41.656059615954575],[0.041444482796310356,41.656116656485466],[0.04141456275296585,41.656135429435096],[0.04136614468640951,41.65614120624315],[0.04132706825149465,41.65614227074488],[0.04128124354593417,41.65612551029031],[0.04119501722648842,41.65607768063336],[0.04116495469078151,41.65606100567422],[0.04112186332400818,41.656039632658064],[0.04108033518226952,41.656018209350464],[0.041008348878970674,41.655995706453695],[0.040987399987125396,41.65598915767234],[0.040937129519857736,41.655972462596594],[0.040889723149074506,41.6559692598712],[0.04078962477905301,41.65596289884451],[0.04076305582079624,41.65595886211969],[0.04074718720076604,41.65595645148423],[0.0407389427603151,41.65595519838962],[0.04072028045179132,41.65594841407679],[0.04069311805475462,41.65593843793502],[0.04065432325180311,41.65591240206584],[0.04061847971713421,41.65588179722096],[0.040587874034069424,41.65585566495989],[0.04056173925842281,41.655836514824585],[0.04055220568118726,41.65582952934605],[0.04054751935884471,41.65582596871337],[0.04051785663994292,41.655803428936224],[0.0404244830712368,41.65571582798458],[0.04030450534027847,41.655592959335024],[0.04026526042909982,41.65557150501439],[0.04022374821297813,41.6555680935796],[0.0401938801374364,41.6555724697715],[0.0401866339817127,41.655573531758705],[0.04014841753011451,41.65559253940168],[0.04012136158325442,41.65562480302526],[0.04012063068125431,41.65562618352479],[0.040102036266150255,41.65566131878862],[0.040089356960466914,41.65569211977004],[0.04005893316471178,41.65576602552929],[0.040051750711455975,41.65580222361845],[0.04004544165256271,41.655842947097426],[0.040032126160390744,41.65587934802872],[0.040015807432133406,41.655915760694654],[0.03997528260426417,41.655979917461735],[0.039961119701631055,41.65598799258771],[0.03995049815329898,41.65599404851312],[0.039942853857150906,41.6559942496788],[0.03985371398798907,41.65599659745004],[0.03980228933258589,41.65600247651727],[0.03975174226249707,41.656012791287935],[0.03970507517561764,41.65602752835093],[0.03966397115820474,41.65604664160588],[0.03963545676010589,41.65606094616271],[0.039617866497061636,41.65608841114647],[0.03961844149565348,41.65616497480735],[0.03961414577002202,41.6562010672846],[0.039597102845287495,41.65623755119668],[0.03956400354588053,41.656265426893725],[0.03952633359440169,41.65629345430321],[0.039458177821080555,41.65634474149539],[0.0394236686854761,41.65637717275709],[0.03940910093689566,41.65639558109393],[0.03939817512546034,41.656409386927464],[0.03938098048826936,41.65644315007364],[0.039379568975691136,41.65644592113097],[0.03936309685859661,41.65647782684732],[0.03934677310119708,41.656514330037794],[0.03932541265788153,41.656568643097216],[0.039303821712215053,41.65662354288986],[0.03925695932698007,41.65669230970397],[0.03923426867555194,41.65671936732721],[0.039229902541929644,41.65672457332758],[0.03919439679797868,41.65678399683367],[0.039194100916779086,41.656824514956014],[0.039190951835569844,41.65686964228718],[0.03919596421396519,41.65689651886129],[0.03922662018845202,41.65692720836341],[0.039295667796323756,41.65696968449845],[0.03930393654429534,41.65697477063759],[0.03930853485664933,41.65697727682681],[0.03934330215488685,41.65699622831099],[0.039396235552950826,41.657026310125076],[0.0395476487754374,41.65711235707178],[0.03966764578477417,41.65719028782115],[0.03969654491225152,41.65721413098436],[0.039736377394877516,41.657246994752256],[0.03979621748156652,41.657291811953],[0.039811843864580615,41.65730351476691],[0.03987315665174465,41.65736489293292],[0.039900202540320305,41.65739576013138],[0.03990380759708926,41.65740158639261],[0.03994403067642632,41.65746659392292],[0.03996293773653732,41.65750211391611],[0.039985982104300384,41.657550193353785],[0.039997026100577475,41.65757323688334],[0.04004428075890426,41.65768459550276],[0.04005657533520909,41.657720304926954],[0.04006502261772887,41.65775609565563],[0.040096527679057574,41.6578678582828],[0.04011371812529672,41.657898920779616],[0.040169337085296936,41.657958161560174],[0.040171425855691785,41.65796038611745],[0.04020865827179899,41.657986472278054],[0.04022491162120769,41.65799241672411],[0.04025448381554984,41.6580032327326],[0.04025571427900875,41.65800344479476],[0.040299878529452,41.65801106729796],[0.04034742403983617,41.658000765100184],[0.04052384467192559,41.65793758817762],[0.040566616688169035,41.65792652238841],[0.04091163679803233,41.65783725930527],[0.0410018461981026,41.65780788324747],[0.0410316874515526,41.657801838218546],[0.04105311914104934,41.65779749723063],[0.04106175166364024,41.6577980143941],[0.04110581197847795,41.65780065553193],[0.04113990704804105,41.65780873820165],[0.04117413706625324,41.65783483693511],[0.041195893973612424,41.65786583656762],[0.04122952468571213,41.65789191937549],[0.041266762130961546,41.65791791417334],[0.04137407564366314,41.657976714065796],[0.041394564368996876,41.65798793994935],[0.041404999839067445,41.65799365808405],[0.04141233987211226,41.657997679721404],[0.04143147156724164,41.65800816296834],[0.04147856696939867,41.65803060300087],[0.0414794789205181,41.65803103718349],[0.04148032633097924,41.65803144119124],[0.0414911649706164,41.658036605282064],[0.041556750014081593,41.65806785553352],[0.04159884477250843,41.658084790331685],[0.04174148968556461,41.65813502391859],[0.04178860185152899,41.658147315142145],[0.041837880739592354,41.65815948147531],[0.04190665761197552,41.65818563888212],[0.04192652272367613,41.65819319433007],[0.04199795917409414,41.6582092632776],[0.04205417995477795,41.658222021373206],[0.04212590725725336,41.65823829819192],[0.04219359029120745,41.658253657192205],[0.04219840653305688,41.658254578363426],[0.042251606475657615,41.65826475147992],[0.04233834912036951,41.658281340105475],[0.042386751261308864,41.658289070735236],[0.042434450998720584,41.65828327464885],[0.04244071647171477,41.658280066056136],[0.042449364919922634,41.658275636220026],[0.04246224203853262,41.658269040500016],[0.042466747311755856,41.65826276832152],[0.0424715836701741,41.658256035138294],[0.042481994832724024,41.65824154115046],[0.04248371396116697,41.65821447849808],[0.04249443776872383,41.65810614490769],[0.04251305816772927,41.658087623415156],[0.04252125650771001,41.6581165426592],[0.042528257073656144,41.658141238262004],[0.04252997201114726,41.65817721506892],[0.042525380404316214,41.65825391619305],[0.04252770805896568,41.6583269493961],[0.042530111988938565,41.65840236305979],[0.042517528236957085,41.65856888801639],[0.04251579402108752,41.65859183515411],[0.042511053216371136,41.658633764789016],[0.04250610789320516,41.65867749988601],[0.04254461184409927,41.65862743226083],[0.04255568350065848,41.658613035503095],[0.042704411161208815,41.65841963983965],[0.04389609658098795,41.656869663241935],[0.043987920335705304,41.65675023201109],[0.044215711140824024,41.656453861308336],[0.0448310778189348,41.65671995154588],[0.04495933435614909,41.65677541041151],[0.04496666852186133,41.656778585476644],[0.04500843722423537,41.656796666919234],[0.045306469220308614,41.65692552527713],[0.04549528584973728,41.65700716334251],[0.045545746586218266,41.657028981437776],[0.04558705093112892,41.65704683992175],[0.04568211848256698,41.65708794393916],[0.04574764988184867,41.65711627728814],[0.04576830792163658,41.65712520904469],[0.04580129155077288,41.65713947001558],[0.0457895258725897,41.65711052897859],[0.04577522969768819,41.65705288411249],[0.04577237314491708,41.65699193174714],[0.04578172818760443,41.65692901801643],[0.045785033172221955,41.65690679256062],[0.04581981304331499,41.65687886908674],[0.045835246642544666,41.656861878968016],[0.04584914970616399,41.6568465727737],[0.04588982037915708,41.65681853027678],[0.045990798849064034,41.65676628251842],[0.04599208128032572,41.656765130006676],[0.04599548600959694,41.65676206893548],[0.046000725537581784,41.656757359143796],[0.04600219404710496,41.65675603899396],[0.046002961829481236,41.65675534916326],[0.04600879479624598,41.656750106282544],[0.04615034335630198,41.656622871499366],[0.0461992794245052,41.656603146365455],[0.04624594567319427,41.656588406787876],[0.0464349617919303,41.65653842214354],[0.046531061155710284,41.65650883318537],[0.04661638809689212,41.65647059158803],[0.04672136557304057,41.65639124848792],[0.04680226351632258,41.6563395639935],[0.04681761497289628,41.656332575162224],[0.046844204885912226,41.65632047001694],[0.04693414019773245,41.65628648894125],[0.04697996238872184,41.65627181725705],[0.047126162067124965,41.656236401199465],[0.04721121408186638,41.65619364846165],[0.0472491547688338,41.65617012883993],[0.04725368099681621,41.65616549113261],[0.04733935830157151,41.65607770745547],[0.04737087928851719,41.65602779405734],[0.0473798764239178,41.65601354733565],[0.0473980157016525,41.65598699178841],[0.04742684945028122,41.655944779683324],[0.04744455622178651,41.65592366063885],[0.04745390204437982,41.655912514383346],[0.04748784204872501,41.65588456827834],[0.04752534939127262,41.655852121091016],[0.047538546698602836,41.6558445078079],[0.04756629666708687,41.65582849753369],[0.04761112476430087,41.655809297107],[0.04765050797689009,41.65578572384109],[0.047725545293125234,41.65573875165293],[0.04775933023225249,41.65570629859829],[0.04778409540483924,41.655674153997694],[0.04780185246776655,41.65563768684939],[0.047806942797866035,41.65561827519866],[0.04781190739941044,41.65561704138247],[0.04781096359705707,41.6556121924512],[0.04783401467034309,41.655517336961935],[0.04786700165224085,41.6554508089543],[0.047859350650569836,41.655290596409316],[0.04779664311463493,41.65510650968815],[0.04767336794277238,41.65501912079867],[0.04745843833064664,41.654971486440076],[0.04732087117483444,41.654951105003136],[0.047169374133311154,41.6549079080244],[0.04712811924163216,41.65490965481308],[0.047008370336644074,41.6549907941552],[0.04684413950706359,41.65506236890701],[0.04678805702779036,41.655060926381395],[0.046692642465540314,41.655019170251876],[0.04661758922368819,41.654941443182764],[0.04660133084513729,41.654887686681775],[0.0466046643282273,41.654814781721825],[0.046499787434922904,41.65457065710103],[0.04650519292460042,41.654534300593866],[0.046667114539719494,41.65451319915135],[0.046984142444082645,41.65453819566446],[0.047065498877495456,41.65455993832262],[0.047178475204317276,41.65470882356297],[0.047246051788094746,41.65478635784831],[0.04724850852406075,41.654814494222535],[0.04725853244266107,41.65481555537153],[0.04725382432736899,41.654811863981166],[0.047251745615258536,41.6548102345391],[0.04728646094569332,41.654799684236934],[0.04731677158574888,41.65479047252478],[0.04736320900846773,41.65477777226437],[0.04740797256502346,41.65476552965586],[0.04742911591703391,41.65476109981975],[0.04747934698935431,41.65475057633972],[0.04749266751066849,41.65474778600395],[0.047493383325242,41.65474763596788],[0.04749877624206401,41.654746506087264],[0.04755576648150482,41.6547345669037],[0.04760358356556794,41.65472464692073],[0.04760559186968755,41.654724230340065],[0.04761344487523703,41.65472404929099],[0.047614034961255484,41.65472403587992],[0.0476151380197786,41.654724009896],[0.04761622850544622,41.65472398475029],[0.04762048735070153,41.654723886682035],[0.04762115119747228,41.6547238715946],[0.0476230496986538,41.65472382800871],[0.04762590708961529,41.654723761791644],[0.0478007703640026,41.65471006492285],[0.04784846255770102,41.65470435684648],[0.04789396204539906,41.65469408901447],[0.04793521693707806,41.65467947935636],[0.04802098644277373,41.654636745058696],[0.04802561912093276,41.65463492953836],[0.048070382677488484,41.654617391243136],[0.04811936484616189,41.654607123411154],[0.04815142730408473,41.65460315290338],[0.04816573772822247,41.65460138013074],[0.048209999208748286,41.654586668213426],[0.0482446240146234,41.6545796123269],[0.048337147658297275,41.654560757234606],[0.04849303849372496,41.654521229015074],[0.04849366881288103,41.6545210689207],[0.04849580955107865,41.65452052661152],[0.048506965864864936,41.65451811178508],[0.04848377313740689,41.65450714406413],[0.04846406644368568,41.65449782674],[0.04835557694989901,41.65444649680012],[0.048056706763458655,41.65431729394601],[0.047935193467747765,41.65426476287928],[0.04789959384647571,41.65424928150321],[0.047679226055248145,41.6541534461694],[0.04758604107937462,41.6541131971062]]]
]}}
saved as poly1.json
Is there any way to save them separately, I would like the final result to be an like this:
array = [coordinates of polygon 1], [coordinates of polygon 2] where in the case of using more polygons than in the example, they will be saved automatically without having to change the code, array = [coordinates of polygon 1], [coordinates of polygon 2],[coordinates of polygon 3] and so on.
So far i have this :
require 'rubygems'
require 'json'
file = File.read('poly1.json')
coordinates = JSON.parse(file)
string=coordinates.to_s[0...-4]
path = string.split"["
path.shift()
path.shift()
path.shift()
This just gets all coordinates from both polygons in path. But what i want is to get polygon 1 coordinates separated from polygon 2 coordinates and in a case where i use a 3 polygon file, to get it separated whit out doing manualy.
Sorry for my bad english, and if you need something else ask in comments.
Your example is quite big and therefore not easy to understand. But from key structure at the beginning of the file
{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[ [[[0.03990083369766674,41.65117297365802], ...
it looks like the coordinates are a simple hash nested below the geometry key. Therefore I would try the following:
require 'rubygems'
require 'json'
file = File.read('poly1.json')
data = JSON.parse(file)
coordinates = data["geometry"]["coordinates"]
coordinates[0] #=> should now return the first coordonate from the array

In Google Earth Engine: Most efficiently reduceRegions over each image in ImageCollection, saving mean as a Feature property?

I have a FeatureCollection made up of many (100-200) polygons ('ftr_polygons'). I also have an ImageCollection made up of monthly median Landsat8 bands and indices ('byMonth'). I want to ReduceRegions and save a median (or mean) spatial average from each polygon in the FeatureCollection. End goal is to export to csv a timeseries of monthly mean bands/indices within each polygons over multiple years (2013-2019).
With the code below, I am able to do this for ~1 year, but any more than that, and I get an error: 'FeatureCollection (Error) Computation timed out’. Is there a better way to do this?
// define the function that will grab median (or mean) spatial reductions for each polygon, for each month
var extractdata = function(medianImage,ftr_polygons) {
var date_start = ee.Date(medianImage.get('system:time_start')).format("YYYY-MM"); // get date as string to append to each property
// spatial MEDIAN
ftr_polygons = medianImage.reduceRegions({ // create feature collection with new properties, bands for each month, uniquely named
collection: ftr_polygons,
reducer: ee.Reducer.median(),
scale: 30,
tileScale: 1}); // tile scale
var ftr_polygons_propnames = ftr_polygons.first().propertyNames(); // get property names first
var ftr_polygons_newnames = ftr_polygons_propnames.replace('NDVI_median',
ee.String('NDVI_median_').cat(date_start)); //replace property names with band+date
ftr_polygons_newnames = ftr_polygons_newnames.replace('EVI_median',
ee.String('EVI_median_').cat(date_start)); //replace property names with band+date
ftr_polygons_newnames = ftr_polygons_newnames.replace('NIRv_median',
ee.String('NIRv_median_').cat(date_start)) ; //replace property names with band+date
ftr_polygons = ftr_polygons.map(function(f) {return f.select(ftr_polygons_propnames,ftr_polygons_newnames)});
return ftr_polygons;
};
// apply the function over ImageCollection byMonth, beginning with feature collection ftr_polygons
var ftr_polygons = ee.FeatureCollection(byMonth.iterate(extractdata,ftr_polygons));
// remove geometry on each feature before printing or exporting
var myproperties=function(feature){
feature=ee.Feature(feature).setGeometry(null);
return feature;
};
var ftr_polygon_export = ftr_polygon.map(myproperties)
print(ftr_polygon_export.limit(1), 'For export w monthly properties');
Maybe this answer: https://stackoverflow.com/a/48412324/12393507 alludes to a better way:
The same approach can be used with reduceRegions() as well, mapping over images and then over regions. However, you will have to map over the resulting features to set dates.
I would appreciate more info on this approach.
Thanks.
For computationally intensive operations that will run for a long time you should always export your results instead of visualizing/printing them.
For more info read through this section of the debugging page in the Earth Engine manual.

D3JS Map Citiy Circles

I'm currently creating a map of germany with d3js to visualize some data. I now added some Cities by using a csv, which are shown as circles in the map. I want some cities to have a different color the more data are used in them. Does anybody have a idea how I can achieve this?
You can declare a range of color like this:
var color = d3.scale.linear()
.domain([d3.min(array), d3.max(array)])
.range(["#fff7f3", "#49006a"]);
then style every circle:
.style("fill", function(d) {
return color(+d.data);
});
But you need to compact your data to just one value per city like so:
d.values.reduce(function(sum, d){
return sum + d.amount;
},0)
Here's an example from Mike's Site: http://bl.ocks.org/mbostock/4060606

MeteorJS and UnderscoreJS: grouping results to plot points on a Google Map

I am working on a small MeteorJS app that plots points on a map based on popular areas for work.
I have this:
Template.list.jobs = function() {
if(Session.get('currentIndustryOnet')) {
jobs = Jobs.find({onet: Session.get('currentIndustryOnet')}).fetch();
// Session.set('jobCount', jobs.count());
var cnt = _.groupBy(jobs, 'address');
console.log(cnt);
return Pagination.collection(jobs);
} else {
jobs = Jobs.find()
Session.set('jobCount', jobs.count());
return Pagination.collection(jobs.fetch());
}
}
The cnt variable returns a properly grouped array (the key of the array is an address like Allentown, PA). I have a collection of Cities which have ever city in the USA along with their LAT/LONGs to plot on a Google Map. So I will take the top 100 from the grouped array, find the lat/long in the Cities collection and plot those points on a map.
I am not familiar with working with a groupedBy method to sort the list based on the length and then pull out the key to use as my search.
I'm not 100% certain about the data structure... but assuming jobs have an address field, and you want them sorted by frequency of occurrence and capped, you could do something like this:
var addresses = _.chain(jobs)
.countBy('address')
.pairs()
.sortBy(function(j) {return -j[1];})
.map(function(j) {return j[0];})
.first(100)
.value();
Note there may be a more clever way to use underscore to arrive at this result. Once you have the capped, sorted list of addresses, you can probably get the lat/long values via a find like:
Cities.find({address: {$in: addresses}}).fetch();

D3 reusable multi-line chart with JSON data

I'm trying to do some re-factoring on my charts to make them re-usable using this as a guide: http://bost.ocks.org/mike/chart/
I'm having problems drawing the lines in my multi-line graph though - specifically passing the data to the x and y values. If I hard code the element names it works, but if I try to use the xValue and yValue objects this does not work. I'm assuming that this is because I'm trying to call a function within the parameter of an other object, but I'm not sure how to get around this. In the exmaple Mike uses d[0] and d[1], but this won't work with JSON data (or I'm not sure how to make it work).
I've posted this JSFiddle so you can see the code. The problem lines are 125 to 131 which in turn is being called from line 165.
var main_line = d3.svg.line()
.interpolate("cardinal")
// Hard coding the elements works
//.x(function(d) { return main_x(d.date); })
//.y(function(d) { return main_y(d.buildFixTime); });
// Passing xValue and yValue does not work
.x(function(d) { return main_x(xValue); })
.y(function(d) { return main_y(yValue); });
http://jsfiddle.net/goodspeedj/fDyLY/
Thank you in advance.
You need to redefine your accessor method within .x() and .y(). The accessor method defines the way that a datum is pulled out of the data that is bound to the selection that you call the line generator on.
Suppose you have a relatively flat data structure such as the following.
data = [{x : 1, y : 2}, {x:1, y:3}, {x:4, y:5}];
You then bind the data to a selection with the following statement
d3.select("body").datum(data).append("path").attr("d",lineGenerator);
Quite a bit is going on underneath this statement. I'll give you a bit more of a walkthrough after showing you a commonly used example.
The important aspect to understand is that similarly to other calls in d3 such as
var exampleRectangles = d3.select("body")
.data(data).enter()
.append("rect")
.attr("width",2)
.attr("height", 3)
.attr("x",function(datum){return datum.x}) // pay attention to this line
.attr("y",0);
d3 is implicitly iterating over each element in your data. For each datum in your data array, in this case there is a total of three datum, you are going to add a rectangle to the dom.
In the line that I tell you to pay attention to you notice that you're defining an anonymous (unnamed) function. What is that datum parameter coming from? It's implicitly being passed to your anonymous function.
So each rectangle has it's own corresponding datum {x : 1, y : 2}, {x:1, y:3}, {x:4, y:5} respectively. Each rectangle's x coordinate is defined by the respective datum.x attribute. Under the sheets, d3 is implicitly looping over the data array that you've defined. A similar approach to the example d3 code could be written as above.
for (var i = 0; i < data.length; i++)
{
d3.select("body").append("rect")
.attr("width",2)
.attr("height", 3)
.attr("x",data[i].x)
.attr("y",0);
}
This follows from the notion of data driven documents (d3). For each item added (a rectangle in the above example a piece of data is tied to it. In the above example you see that there is something kind of similar to your .x() and .y() accessor functions :
.attr("x",function(datum){return datum.x})
This function is telling d3 how to filter over the total datum that's being passed to the .attr() accessor method.
So, you need to determine which data you need to get a hold of to make your .attr("d", lineGenerator)call make sense. The difference between your.datum(data)call and the typical.data(data)call is that instead of parceling the data that's being passed to.data(data)`, the whole array is given as a single piece of data to the line generator function (similar to main_line(data), wherein it will again implicitly loop over the points to construct your path.
So, what you need to do is determine what a single datum will be defined as for your function to operate on.
I'm not going to define that as I don't seem to know quite which information you are operating on, but I would hazard a guess at something like.
.x(xAccessor)
.y(yAccessor)
function xAccessor(datum)
{
return xScale(datum._id.month);
}
function yAccessor(datum)
{
return yScale(datum.buildFixTime);
}
The way you have it set up, xValue and yValue are functions; you have to actually execute them on something to get a value back.
.x(function(d) { return main_x( xValue(d) ); })
.y(function(d) { return main_y( yValue(d) ); });
If you weren't using a scale, you could use
.x(xValue)
.y(yValue);
but only because if you pass in a function d3 executes it for you with the data as a parameter. And that only works for d3 methods that expect functions as possible input -- the scale functions expect data values as input.
I wrote a long piece work for another user last week that you may find useful, explaining methods that accept functions as parameters.