DbGeography - select polygons/linestrings contained inside polygon - sql-server

I have a case where I want to load map objects located inside users viewport.
This is how I create users viewport rectangle:
DbGeography viewport_rectangle = DbGeography.FromText(string.Format("POLYGON(({0} {1}, {0} {2}, {3} {2}, {3} {1}, {0} {1}))", lon_min, lat_min, lat_max, lon_max));
Then I want to select all objects (Points, PolyLines, Polygons, located inside that rectangle):
var objects = db.mapobjects.Where(x => !x.LocationGeographic.Intersects(viewport_rectangle));
Everything until here works fine. The problem occours if PolyLine/Polygon is not entirely contained inside the viewport polygon. In this case it's ignored and I get "no objects" instead of objects, where some points/edges are out of the viewport.
Is there any alternative to "Intersects"? I want to select objects contained in the viewport rectangle, regardless if they are inside at all, or if only one small part of them is inside of the viewport rectangle.
viewport_rectangle = {SRID=4326;POLYGON ((15.693584159016611
46.532346466357438, 15.693584159016611 46.532770863495614, 15.695530101656916 46.532770863495614, 15.695530101656916 46.532346466357438, 15.693584159016611 46.532346466357438))}
Object which is only partially located inside viewport_rectangle and should be returned as a result:
LINESTRING (15.694189164787527 46.532622094224166, 15.694309193640944
46.532614944062828, 15.694392677396532 46.5326121762582, 15.694401059299702 46.532662919320614, 15.694536175578829 46.532621632923423, 15.694564338773485 46.532659690218026, 15.694584455341097 46.532614944062828, 15.694570373743769 46.532578039989573, 15.694489236921068 46.53258611275777, 15.694502312690016 46.532539290685662, 15.694723930209872 46.53252614359414, 15.69474438205361 46.532575041532539, 15.694786962121723 46.532516225610692, 15.694763492792843 46.532481858630774, 15.694699790328738 46.532507922181281, 15.694884862750767 46.532493852478581, 15.694849658757446 46.53254505695287)
One part of LINQ generated query:
SELECT
[Filter1].[ObjectId] AS [ObjectId],
[Filter1].[LocationGeographic] AS [LocationGeographic],
FROM (SELECT [Extent1].[ObjectId] AS [ObjectId], [Extent1].[LocationGeographic] AS [LocationGeographic]
FROM [dbo].[mapobjects] AS [Extent1]
WHERE (([Filter1].[LocationGeographic].STIntersects(#p__linq__0)) <> cast(1
as bit))
) AS [Project1]
Edited:
the correct order of viewport_rectangle should be:
DbGeography viewport_rectangle = DbGeography.FromText(string.Format("POLYGON(({0} {1}, {2} {1}, {2} {3}, {0} {3}, {0} {1}))", lon_min, lat_min, lon_max, lat_max));

You appear to have a ring orientation problem with your polygon. The order in which you specify your points matters. The polygon, as you've defined it, is the entire globe minus a very small square (presumably, your desired viewport). How did I determine this?
declare #line geography = geography::STGeomFromText('LINESTRING (15.694189164787527 46.532622094224166, 15.694309193640944 46.532614944062828, 15.694392677396532 46.5326121762582, 15.694401059299702 46.532662919320614, 15.694536175578829 46.532621632923423, 15.694564338773485 46.532659690218026, 15.694584455341097 46.532614944062828, 15.694570373743769 46.532578039989573, 15.694489236921068 46.53258611275777, 15.694502312690016 46.532539290685662, 15.694723930209872 46.53252614359414, 15.69474438205361 46.532575041532539, 15.694786962121723 46.532516225610692, 15.694763492792843 46.532481858630774, 15.694699790328738 46.532507922181281, 15.694884862750767 46.532493852478581, 15.694849658757446 46.53254505695287)', 4236),
#poly geography = geography::STGeomFromText('POLYGON ((15.693584159016611 46.532346466357438, 15.693584159016611 46.532770863495614, 15.695530101656916 46.532770863495614, 15.695530101656916 46.532346466357438, 15.693584159016611 46.532346466357438))', 4236);
select #poly.EnvelopeAngle(); --returns 180
select #poly.ReorientObject().STIntersects(#line); --returns 1
Best you read up on the EnvelopeAngle() method yourself. But I'll say this - I use it as a quick heuristic to detect the ring orientation problem you have here. Invariably, if a polygon has this problem, the envelope angle will be 180 (which is almost never what you intended).
I've also given away the punchline on how to fix it in the code above; calling ReorientObject() on the polygon changes clockwise to counterclockwise (and vice versa).
Finally, it looks like your line string was fully contained within your viewport; I tested with STContains(). Which explains why you were getting false before when what you thought was your viewport was everything but the viewport!

Related

Map does not fill polygons in Google data studio filled map

I'm trying to follow this tutorial to include a filled map in google studio. I've simplified my data (in Google BigQuery) so it is just simple polygons and an ID string:
WKT,origin
"POLYGON ((-122.257923 37.842606,-122.257728 37.842819,-122.257617 37.842895,-122.257418 37.843096,-122.257007 37.843602,-122.256205 37.844686,-122.255084 37.846069,-122.254239 37.847136,-122.25245 37.849386,-122.252354 37.849468,-122.252449 37.850342,-122.252503 37.850826,-122.252413 37.85083,-122.252333 37.850834,-122.251893 37.850979,-122.251729 37.851022,-122.251516 37.851095,-122.251273 37.85117,-122.251083 37.85123,-122.251008 37.851184,-122.25096 37.851159,-122.250883 37.851118,-122.250809 37.851216,-122.250681 37.851385,-122.250532 37.851379,-122.250473 37.851387,-122.250328 37.851393,-122.250185 37.851406,-122.250037 37.851416,-122.250025 37.851387,-122.250003 37.851346,-122.249981 37.851294,-122.249629 37.851411,-122.249473 37.851448,-122.249155 37.851466,-122.248757 37.851488,-122.248217 37.85152,-122.247757 37.851552,-122.247501 37.851571,-122.247433 37.851616,-122.24738 37.851648,-122.247354 37.851647,-122.247253 37.851576,-122.247131 37.851586,-122.246928 37.851599,-122.246886 37.851602,-122.246618 37.851619,-122.246588 37.851575,-122.246109 37.851603,-122.246122 37.851665,-122.246015 37.851664,-122.245779 37.851677,-122.245537 37.851691,-122.245119 37.851715,-122.245034 37.851716,-122.245023 37.851791,-122.245012 37.851813,-122.245 37.851834,-122.244994 37.851847,-122.244949 37.851883,-122.244931 37.851899,-122.244836 37.851842,-122.244823 37.851816,-122.244781 37.851733,-122.244467 37.851751,-122.243836 37.851786,-122.243703 37.851794,-122.243661 37.851796,-122.243647 37.8518,-122.243633 37.851803,-122.243613 37.851806,-122.243599 37.851807,-122.24349 37.851814,-122.243396 37.851819,-122.243279 37.851826,-122.243229 37.851829,-122.243095 37.851837,-122.243012 37.851842,-122.242887 37.85185,-122.242675 37.851863,-122.242526 37.851871,-122.24246 37.851875,-122.242429 37.851877,-122.24229 37.851885,-122.242259 37.851887,-122.242157 37.851893,-122.242122 37.851895,-122.242059 37.851899,-122.241914 37.851909,-122.24191 37.851814,-122.241938 37.851654,-122.241946 37.851629,-122.241956 37.8516,-122.241971 37.851535,-122.241991 37.851447,-122.242008 37.851396,-122.242019 37.851373,-122.24204 37.851335,-122.242245 37.851041,-122.242315 37.85096,-122.242427 37.850833,-122.242666 37.850756,-122.243383 37.850529,-122.243623 37.850453,-122.243707 37.850413,-122.243959 37.850296,-122.244044 37.850257,-122.243884 37.849996,-122.243621 37.849565,-122.243524 37.849445,-122.243382 37.849318,-122.243341 37.849265,-122.243331 37.849252,-122.243309 37.849205,-122.243306 37.849151,-122.243323 37.849072,-122.243381 37.848989,-122.243519 37.848782,-122.243716 37.848513,-122.245505 37.847515,-122.246198 37.847128,-122.247241 37.846599,-122.247313 37.846563,-122.249088 37.845839,-122.251516 37.844877,-122.251781 37.844777,-122.251857 37.844746,-122.252871 37.844354,-122.254366 37.843773,-122.254958 37.84356,-122.256866 37.842935,-122.257078 37.842867,-122.257155 37.842843,-122.257325 37.84279,-122.257444 37.842752,-122.257644 37.84269,-122.257778 37.842654,-122.257804 37.842645,-122.257923 37.842606))","06001400200"
"POLYGON ((-122.265631 37.837636,-122.265565 37.837847,-122.26551 37.838027,-122.26534 37.838461,-122.265224 37.83866,-122.265144 37.838796,-122.264958 37.839092,-122.264769 37.839366,-122.264589 37.839565,-122.264525 37.839632,-122.264508 37.839649,-122.264483 37.839674,-122.264446 37.839711,-122.26442 37.839737,-122.264383 37.839774,-122.264164 37.839997,-122.263929 37.840196,-122.263679 37.8404,-122.263437 37.840561,-122.263141 37.840743,-122.262857 37.840902,-122.262811 37.840926,-122.262541 37.841064,-122.26186 37.841353,-122.261778 37.841391,-122.261704 37.841415,-122.261366 37.841537,-122.261244 37.841581,-122.260888 37.841694,-122.260384 37.841833,-122.259891 37.841987,-122.258449 37.84244,-122.258415 37.842451,-122.257923 37.842606,-122.257804 37.842645,-122.257778 37.842654,-122.257644 37.84269,-122.257444 37.842752,-122.257325 37.84279,-122.257155 37.842843,-122.257078 37.842867,-122.256866 37.842935,-122.254958 37.84356,-122.254366 37.843773,-122.252871 37.844354,-122.251857 37.844746,-122.251781 37.844777,-122.251516 37.844877,-122.249088 37.845839,-122.247313 37.846563,-122.247241 37.846599,-122.246198 37.847128,-122.245505 37.847515,-122.243716 37.848513,-122.243825 37.848361,-122.24392 37.848229,-122.244188 37.847913,-122.244285 37.847733,-122.24458 37.847194,-122.244678 37.847015,-122.244695 37.846982,-122.244732 37.846915,-122.24475 37.846883,-122.244768 37.846851,-122.244984 37.846435,-122.245396 37.845798,-122.246083 37.844667,-122.246108 37.844629,-122.246185 37.844515,-122.246211 37.844478,-122.24632 37.844301,-122.246648 37.843774,-122.246758 37.843598,-122.246826 37.843489,-122.247031 37.843163,-122.2471 37.843055,-122.24749 37.842427,-122.24813 37.841392,-122.249195 37.839711,-122.249461 37.839267,-122.24973 37.83884,-122.25054 37.837561,-122.25081 37.837135,-122.250836 37.837094,-122.250915 37.836973,-122.250942 37.836933,-122.25102 37.836804,-122.251254 37.836423,-122.251339 37.836291,-122.251413 37.836175,-122.251636 37.835827,-122.251711 37.835712,-122.251851 37.835478,-122.25217 37.834946,-122.252272 37.834778,-122.252282 37.834763,-122.25241 37.834543,-122.252606 37.834686,-122.252852 37.834865,-122.253038 37.834975,-122.253234 37.835054,-122.25346 37.835146,-122.253624 37.835208,-122.254118 37.835397,-122.254283 37.83546,-122.254509 37.835549,-122.255187 37.835819,-122.255414 37.835909,-122.255555 37.835964,-122.255632 37.835994,-122.256284 37.836247,-122.256509 37.836335,-122.256713 37.836408,-122.256852 37.836454,-122.256971 37.836493,-122.257348 37.836584,-122.25764 37.836637,-122.257919 37.83668,-122.258079 37.836705,-122.25828 37.836729,-122.258604 37.836783,-122.259516 37.836935,-122.259577 37.836944,-122.259902 37.836994,-122.259924 37.836997,-122.25999 37.837007,-122.260012 37.837011,-122.260182 37.837044,-122.260221 37.837052,-122.260698 37.837113,-122.260721 37.837116,-122.260872 37.837124,-122.26117 37.837157,-122.26163 37.83721,-122.262064 37.837267,-122.262259 37.837293,-122.262361 37.837314,-122.262305 37.837859,-122.262286 37.838027,-122.263234 37.837903,-122.263356 37.837901,-122.263759 37.837953,-122.263879 37.837961,-122.263994 37.837955,-122.264113 37.837917,-122.264169 37.83787,-122.264269 37.83786,-122.264321 37.837826,-122.264383 37.837806,-122.264584 37.837778,-122.264743 37.837761,-122.264829 37.837746,-122.265026 37.8377,-122.265127 37.837687,-122.265228 37.837681,-122.265408 37.837668,-122.265436 37.837666,-122.265452 37.837663,-122.265586 37.837642,-122.265631 37.837636))","06001400300"
"POLYGON ((-122.261826 37.841623,-122.261805 37.84179,-122.26176 37.842045,-122.261686 37.842467,-122.261632 37.842811,-122.261593 37.843068,-122.261566 37.843265,-122.261486 37.843858,-122.26146 37.844056,-122.261448 37.844103,-122.261414 37.844247,-122.261404 37.844295,-122.261382 37.844441,-122.261317 37.84488,-122.261296 37.845027,-122.261275 37.845181,-122.261214 37.845646,-122.261195 37.845801,-122.261179 37.84594,-122.261175 37.845981,-122.261131 37.846359,-122.261115 37.846499,-122.261095 37.846645,-122.261037 37.847086,-122.261019 37.847233,-122.260995 37.847379,-122.260925 37.847818,-122.260903 37.847965,-122.260883 37.84811,-122.260824 37.848548,-122.260805 37.848694,-122.260782 37.848845,-122.260714 37.849298,-122.260692 37.84945,-122.260673 37.84959,-122.260617 37.85001,-122.260599 37.850151,-122.260587 37.850223,-122.260571 37.85033,-122.260495 37.850869,-122.26047 37.851049,-122.260441 37.851245,-122.260356 37.851836,-122.260329 37.852033,-122.26031 37.852174,-122.260264 37.852535,-122.260258 37.852569,-122.260253 37.8526,-122.260232 37.852742,-122.260223 37.852793,-122.260125 37.852806,-122.259972 37.852826,-122.259753 37.852854,-122.259787 37.853011,-122.258367 37.853196,-122.258338 37.853041,-122.258209 37.853052,-122.258134 37.853061,-122.25804 37.853074,-122.257902 37.853092,-122.257772 37.85311,-122.257508 37.853145,-122.257329 37.853168,-122.257251 37.853176,-122.257274 37.853298,-122.257058 37.85333,-122.256948 37.853347,-122.256412 37.853419,-122.256197 37.853449,-122.256098 37.853462,-122.255802 37.853501,-122.255704 37.853515,-122.255587 37.85353,-122.255239 37.853577,-122.255123 37.853593,-122.255059 37.853604,-122.25487 37.853637,-122.254807 37.853649,-122.254644 37.853668,-122.254157 37.853726,-122.253995 37.853746,-122.253986 37.853702,-122.253968 37.853611,-122.253833 37.85362,-122.253679 37.853641,-122.253593 37.853654,-122.253446 37.853672,-122.253468 37.853763,-122.253483 37.853811,-122.253346 37.85383,-122.253329 37.853743,-122.253318 37.853688,-122.253265 37.853695,-122.253241 37.853475,-122.253309 37.853466,-122.253333 37.853466,-122.253078 37.851958,-122.253056 37.851769,-122.252995 37.851238,-122.25298 37.851165,-122.252966 37.851111,-122.25296 37.851072,-122.25294 37.850928,-122.252919 37.850772,-122.252552 37.850821,-122.252503 37.850826,-122.252449 37.850342,-122.252354 37.849468,-122.25245 37.849386,-122.254239 37.847136,-122.255084 37.846069,-122.256205 37.844686,-122.257007 37.843602,-122.257418 37.843096,-122.257617 37.842895,-122.257728 37.842819,-122.257923 37.842606,-122.258415 37.842451,-122.258449 37.84244,-122.259891 37.841987,-122.260384 37.841833,-122.260888 37.841694,-122.261244 37.841581,-122.261366 37.841537,-122.261704 37.841415,-122.261778 37.841391,-122.26186 37.841353,-122.261826 37.841623))","06001400400"
"POLYGON ((-122.269507 37.848582,-122.26937 37.8486,-122.269237 37.848617,-122.269104 37.848634,-122.269043 37.848641,-122.269049 37.848671,-122.269051 37.848681,-122.268978 37.84869,-122.268612 37.848737,-122.268411 37.848765,-122.268448 37.84912,-122.268457 37.849173,-122.268477 37.849293,-122.268567 37.849812,-122.268597 37.849986,-122.268608 37.850072,-122.268644 37.850332,-122.268657 37.850419,-122.268683 37.850549,-122.268763 37.85094,-122.26879 37.851071,-122.268811 37.851178,-122.268877 37.8515,-122.268899 37.851608,-122.268909 37.851652,-122.268652 37.851683,-122.268436 37.851729,-122.26821 37.851776,-122.267952 37.851822,-122.26785 37.851796,-122.267778 37.851796,-122.267766 37.851798,-122.267613 37.851819,-122.267241 37.851867,-122.267067 37.85189,-122.267011 37.851895,-122.267014 37.851911,-122.266521 37.851964,-122.266498 37.851967,-122.26641 37.851977,-122.266128 37.852009,-122.266066 37.852016,-122.265268 37.852122,-122.263951 37.852296,-122.263924 37.852167,-122.263835 37.852203,-122.263608 37.852296,-122.26362 37.85234,-122.263644 37.852422,-122.263594 37.852511,-122.263569 37.852547,-122.263489 37.852548,-122.263429 37.85255,-122.262367 37.852681,-122.262286 37.852691,-122.26225 37.852696,-122.262245 37.852657,-122.262241 37.852629,-122.262238 37.852606,-122.262234 37.852585,-122.262221 37.852527,-122.262149 37.852537,-122.261829 37.85258,-122.261511 37.852623,-122.261024 37.852691,-122.260826 37.852715,-122.260548 37.85275,-122.260223 37.852793,-122.260232 37.852742,-122.260253 37.8526,-122.260258 37.852569,-122.260264 37.852535,-122.26031 37.852174,-122.260329 37.852033,-122.260356 37.851836,-122.260441 37.851245,-122.26047 37.851049,-122.260495 37.850869,-122.260571 37.85033,-122.260587 37.850223,-122.260599 37.850151,-122.260617 37.85001,-122.260673 37.84959,-122.260692 37.84945,-122.260714 37.849298,-122.260782 37.848845,-122.260805 37.848694,-122.260824 37.848548,-122.260883 37.84811,-122.260903 37.847965,-122.260925 37.847818,-122.260995 37.847379,-122.261019 37.847233,-122.261037 37.847086,-122.261095 37.846645,-122.261115 37.846499,-122.261131 37.846359,-122.261175 37.845981,-122.261179 37.84594,-122.261195 37.845801,-122.261214 37.845646,-122.261275 37.845181,-122.261296 37.845027,-122.261382 37.845028,-122.26164 37.845031,-122.261726 37.845032,-122.262408 37.84493,-122.264456 37.844627,-122.265041 37.844541,-122.26514 37.84453,-122.265326 37.844501,-122.265884 37.844415,-122.266071 37.844387,-122.266257 37.844367,-122.266817 37.844306,-122.267004 37.844287,-122.267217 37.844256,-122.267856 37.844165,-122.26807 37.844136,-122.268092 37.844225,-122.26816 37.844495,-122.268183 37.844585,-122.268305 37.845251,-122.268369 37.845474,-122.268411 37.845598,-122.268533 37.846183,-122.268574 37.846374,-122.268609 37.846562,-122.268681 37.846902,-122.268763 37.847261,-122.269134 37.847213,-122.269226 37.847201,-122.269279 37.847479,-122.269388 37.847999,-122.269409 37.848107,-122.26942 37.848162,-122.269435 37.84823,-122.269497 37.848532,-122.269507 37.848582))","06001400500"
"POLYGON ((-122.26807 37.844136,-122.267856 37.844165,-122.267217 37.844256,-122.267004 37.844287,-122.266817 37.844306,-122.266257 37.844367,-122.266071 37.844387,-122.265884 37.844415,-122.265326 37.844501,-122.26514 37.84453,-122.265041 37.844541,-122.264456 37.844627,-122.262408 37.84493,-122.261726 37.845032,-122.26164 37.845031,-122.261382 37.845028,-122.261296 37.845027,-122.261317 37.84488,-122.261382 37.844441,-122.261404 37.844295,-122.261414 37.844247,-122.261448 37.844103,-122.26146 37.844056,-122.261486 37.843858,-122.261566 37.843265,-122.261593 37.843068,-122.261632 37.842811,-122.261686 37.842467,-122.26176 37.842045,-122.261805 37.84179,-122.261826 37.841623,-122.26186 37.841353,-122.262541 37.841064,-122.262811 37.840926,-122.262857 37.840902,-122.263141 37.840743,-122.263437 37.840561,-122.263679 37.8404,-122.263929 37.840196,-122.264164 37.839997,-122.264383 37.839774,-122.26442 37.839737,-122.264446 37.839711,-122.264483 37.839674,-122.264508 37.839649,-122.264525 37.839632,-122.264589 37.839565,-122.264769 37.839366,-122.264958 37.839092,-122.265144 37.838796,-122.265224 37.83866,-122.26534 37.838461,-122.26551 37.838027,-122.265565 37.837847,-122.265631 37.837636,-122.265835 37.837606,-122.26612 37.837565,-122.266448 37.837511,-122.266589 37.837489,-122.266653 37.837482,-122.266684 37.837496,-122.266712 37.837514,-122.266724 37.83753,-122.266742 37.837591,-122.266748 37.837623,-122.266861 37.838165,-122.266905 37.838345,-122.266938 37.838522,-122.26704 37.839056,-122.267074 37.839234,-122.26711 37.839417,-122.26722 37.839968,-122.267241 37.840068,-122.267257 37.840152,-122.267285 37.840304,-122.267372 37.84076,-122.267402 37.840912,-122.267434 37.841064,-122.26753 37.84152,-122.267562 37.841672,-122.267587 37.841792,-122.267665 37.842152,-122.267691 37.842273,-122.267728 37.842462,-122.267841 37.843029,-122.267879 37.843219,-122.267898 37.843313,-122.267955 37.843596,-122.267974 37.843691,-122.267993 37.84378,-122.26805 37.844047,-122.26807 37.844136))","06001400600"
"POLYGON ((-122.278078 37.847326,-122.277885 37.847509,-122.277766 37.847525,-122.277703 37.847533,-122.277603 37.847546,-122.277329 37.847582,-122.276746 37.847664,-122.276188 37.847734,-122.276082 37.847222,-122.275896 37.846329,-122.275843 37.846073,-122.275819 37.84596,-122.2758 37.845867,-122.275379 37.845924,-122.274912 37.845981,-122.274435 37.846042,-122.274095 37.846086,-122.27377 37.846128,-122.273323 37.846186,-122.273117 37.846214,-122.273027 37.846226,-122.272646 37.846278,-122.272566 37.846289,-122.272471 37.846302,-122.271763 37.846395,-122.271329 37.846451,-122.271236 37.846465,-122.271085 37.846485,-122.271004 37.846496,-122.270315 37.846586,-122.2702 37.846601,-122.270076 37.846618,-122.270041 37.846622,-122.269926 37.846637,-122.269789 37.846655,-122.269656 37.846673,-122.269631 37.846676,-122.269525 37.84669,-122.269495 37.846694,-122.269409 37.846705,-122.269382 37.846708,-122.269285 37.846721,-122.269144 37.84674,-122.269181 37.846927,-122.269226 37.847201,-122.269134 37.847213,-122.268763 37.847261,-122.268681 37.846902,-122.268609 37.846562,-122.268574 37.846374,-122.268533 37.846183,-122.268411 37.845598,-122.268369 37.845474,-122.268305 37.845251,-122.268183 37.844585,-122.26816 37.844495,-122.268092 37.844225,-122.26807 37.844136,-122.26805 37.844047,-122.267993 37.84378,-122.267974 37.843691,-122.267955 37.843596,-122.267898 37.843313,-122.267879 37.843219,-122.267841 37.843029,-122.267728 37.842462,-122.267691 37.842273,-122.267665 37.842152,-122.267587 37.841792,-122.267562 37.841672,-122.26753 37.84152,-122.267434 37.841064,-122.267402 37.840912,-122.267372 37.84076,-122.267285 37.840304,-122.267257 37.840152,-122.267241 37.840068,-122.26722 37.839968,-122.26711 37.839417,-122.267074 37.839234,-122.26704 37.839056,-122.266938 37.838522,-122.266905 37.838345,-122.266861 37.838165,-122.266748 37.837623,-122.266742 37.837591,-122.266724 37.83753,-122.266712 37.837514,-122.266684 37.837496,-122.266653 37.837482,-122.267064 37.83744,-122.267385 37.837408,-122.267935 37.837346,-122.268296 37.837307,-122.268327 37.837304,-122.268438 37.837279,-122.268459 37.837275,-122.268703 37.837232,-122.268784 37.837214,-122.268886 37.837192,-122.269029 37.837169,-122.269112 37.837156,-122.269151 37.837149,-122.269234 37.837136,-122.269268 37.837121,-122.269305 37.837105,-122.26968 37.837055,-122.270804 37.836906,-122.27118 37.836857,-122.271516 37.836805,-122.271628 37.836792,-122.272236 37.836722,-122.272961 37.836623,-122.272977 37.836621,-122.273356 37.836582,-122.273428 37.836578,-122.27367 37.836544,-122.274398 37.836441,-122.274641 37.836408,-122.275638 37.836447,-122.275665 37.836462,-122.275785 37.836531,-122.275987 37.836646,-122.276009 37.836659,-122.27604 37.836677,-122.276076 37.836705,-122.27622 37.836818,-122.276142 37.837029,-122.276105 37.837131,-122.276059 37.837259,-122.275924 37.837643,-122.275879 37.837772,-122.275846 37.837849,-122.275749 37.838082,-122.275718 37.83816,-122.27611 37.838323,-122.276121 37.838376,-122.276155 37.838535,-122.276167 37.838589,-122.276195 37.838724,-122.276282 37.83913,-122.276311 37.839266,-122.276334 37.839382,-122.276406 37.839733,-122.27643 37.83985,-122.27646 37.840015,-122.27655 37.84051,-122.27658 37.840676,-122.276605 37.840828,-122.276683 37.841284,-122.276709 37.841436,-122.276736 37.84157,-122.276819 37.841976,-122.276847 37.842111,-122.276873 37.84225,-122.276952 37.842668,-122.276979 37.842808,-122.276999 37.842924,-122.277006 37.842963,-122.277012 37.843085,-122.277031 37.843276,-122.277032 37.843286,-122.277026 37.843394,-122.27701 37.84359,-122.277088 37.843654,-122.277322 37.843846,-122.2774 37.84391,-122.277437 37.844084,-122.277548 37.844607,-122.277586 37.844782,-122.277599 37.844865,-122.277641 37.845115,-122.277655 37.845199,-122.277676 37.845376,-122.277742 37.845907,-122.277764 37.846084,-122.277981 37.846972,-122.278078 37.847326))","06001400700"
"POLYGON ((-122.288744 37.850007,-122.288487 37.850058,-122.28838 37.850079,-122.28678 37.850398,-122.286247 37.850505,-122.286221 37.850424,-122.28616 37.850236,-122.286143 37.850184,-122.286132 37.850149,-122.286006 37.850173,-122.285812 37.850207,-122.285689 37.85023,-122.285586 37.850251,-122.284984 37.850367,-122.284638 37.850435,-122.28452 37.850458,-122.2845 37.850391,-122.284331 37.850424,-122.284352 37.85049,-122.284054 37.850548,-122.283834 37.850591,-122.283571 37.850642,-122.283501 37.850441,-122.283464 37.850329,-122.283459 37.850307,-122.28344 37.85022,-122.283346 37.849956,-122.283383 37.849948,-122.283354 37.84985,-122.283326 37.849757,-122.283245 37.849489,-122.283216 37.849407,-122.283197 37.849347,-122.283089 37.849372,-122.282997 37.849102,-122.282964 37.849008,-122.282714 37.849063,-122.282705 37.849036,-122.282619 37.849057,-122.282588 37.849062,-122.282597 37.849088,-122.282463 37.849118,-122.282332 37.848743,-122.282244 37.848492,-122.282178 37.848307,-122.28199 37.848348,-122.281875 37.847829,-122.281863 37.847776,-122.282052 37.847751,-122.281977 37.847416,-122.281973 37.847376,-122.281966 37.847311,-122.281957 37.847245,-122.281899 37.846975,-122.281804 37.846988,-122.281792 37.846989,-122.281702 37.847001,-122.281502 37.847028,-122.281275 37.847057,-122.280708 37.847133,-122.280298 37.847188,-122.280024 37.847224,-122.279888 37.847242,-122.279883 37.84722,-122.279747 37.847239,-122.279751 37.84726,-122.279521 37.84729,-122.279371 37.84731,-122.27889 37.847375,-122.278542 37.847421,-122.278136 37.847475,-122.277885 37.847509,-122.278078 37.847326,-122.277981 37.846972,-122.277764 37.846084,-122.277742 37.845907,-122.277676 37.845376,-122.277655 37.845199,-122.277641 37.845115,-122.277599 37.844865,-122.277586 37.844782,-122.277548 37.844607,-122.277437 37.844084,-122.2774 37.84391,-122.277322 37.843846,-122.277088 37.843654,-122.27701 37.84359,-122.277335 37.843462,-122.277761 37.843295,-122.278301 37.843056,-122.278621 37.842915,-122.278695 37.842883,-122.278919 37.842788,-122.278994 37.842757,-122.279447 37.842565,-122.280807 37.841991,-122.281011 37.841906,-122.28126 37.8418,-122.281645 37.841635,-122.282285 37.841362,-122.2828 37.841141,-122.283062 37.841029,-122.283186 37.840978,-122.283999 37.840624,-122.283957 37.840487,-122.28407 37.840411,-122.284753 37.840133,-122.285576 37.839778,-122.285604 37.839871,-122.28569 37.840153,-122.285698 37.840179,-122.285721 37.840247,-122.285835 37.840597,-122.286179 37.84165,-122.286294 37.842001,-122.286378 37.842269,-122.28663 37.843076,-122.286714 37.843345,-122.286759 37.84348,-122.286897 37.843885,-122.286943 37.84402,-122.286996 37.844202,-122.287157 37.844751,-122.287211 37.844934,-122.287268 37.845101,-122.287441 37.845604,-122.287499 37.845772,-122.287543 37.845917,-122.287677 37.846353,-122.287722 37.846499,-122.28775 37.84658,-122.287837 37.846825,-122.287845 37.846847,-122.287866 37.846907,-122.287805 37.846924,-122.287835 37.847016,-122.287809 37.847024,-122.287839 37.84712,-122.287867 37.847112,-122.287916 37.847261,-122.287936 37.847398,-122.287954 37.847521,-122.288026 37.847725,-122.288128 37.848014,-122.288401 37.847952,-122.288534 37.848362,-122.288272 37.84842,-122.28826 37.848423,-122.288292 37.848549,-122.288315 37.84864,-122.288369 37.848809,-122.288456 37.849085,-122.288477 37.849159,-122.288521 37.849299,-122.288529 37.849324,-122.288539 37.849356,-122.288604 37.849561,-122.288618 37.84961,-122.288631 37.84965,-122.288715 37.849917,-122.288744 37.850007))","06001400800"
"POLYGON ((-122.285576 37.839778,-122.284753 37.840133,-122.28407 37.840411,-122.283957 37.840487,-122.283999 37.840624,-122.283186 37.840978,-122.283062 37.841029,-122.2828 37.841141,-122.282285 37.841362,-122.281645 37.841635,-122.28126 37.8418,-122.281011 37.841906,-122.280807 37.841991,-122.279447 37.842565,-122.278994 37.842757,-122.278919 37.842788,-122.278695 37.842883,-122.278621 37.842915,-122.278301 37.843056,-122.277761 37.843295,-122.277335 37.843462,-122.27701 37.84359,-122.277026 37.843394,-122.277032 37.843286,-122.277031 37.843276,-122.277012 37.843085,-122.277006 37.842963,-122.276999 37.842924,-122.276979 37.842808,-122.276952 37.842668,-122.276873 37.84225,-122.276847 37.842111,-122.276819 37.841976,-122.276736 37.84157,-122.276709 37.841436,-122.276683 37.841284,-122.276605 37.840828,-122.27658 37.840676,-122.27655 37.84051,-122.27646 37.840015,-122.27643 37.83985,-122.276406 37.839733,-122.276334 37.839382,-122.276311 37.839266,-122.276282 37.83913,-122.276195 37.838724,-122.276167 37.838589,-122.276155 37.838535,-122.276121 37.838376,-122.27611 37.838323,-122.275718 37.83816,-122.275749 37.838082,-122.275846 37.837849,-122.275879 37.837772,-122.275924 37.837643,-122.276059 37.837259,-122.276105 37.837131,-122.276142 37.837029,-122.27622 37.836818,-122.276257 37.836848,-122.27638 37.83693,-122.27656 37.836897,-122.276794 37.836853,-122.27694 37.83687,-122.277115 37.837003,-122.277135 37.837077,-122.277409 37.837029,-122.278062 37.83691,-122.27807 37.836927,-122.278111 37.836933,-122.278148 37.836945,-122.278187 37.836964,-122.27821 37.836985,-122.278232 37.837001,-122.278244 37.83702,-122.278312 37.837069,-122.278384 37.837113,-122.278438 37.837131,-122.278526 37.837167,-122.278546 37.837175,-122.278592 37.83719,-122.27865 37.837207,-122.278678 37.837221,-122.27871 37.837232,-122.278736 37.837234,-122.278779 37.837239,-122.278816 37.837241,-122.278953 37.837233,-122.278939 37.837178,-122.278963 37.83718,-122.279294 37.83712,-122.27941 37.837102,-122.279547 37.837075,-122.279928 37.837006,-122.280308 37.836938,-122.281098 37.836791,-122.281331 37.836752,-122.281425 37.836736,-122.281525 37.836719,-122.281649 37.836692,-122.28171 37.836884,-122.28172 37.836917,-122.281775 37.837109,-122.281807 37.837206,-122.281963 37.837181,-122.282024 37.83717,-122.282131 37.837152,-122.284 37.836803,-122.284085 37.836787,-122.284197 37.836766,-122.284343 37.836739,-122.284429 37.836724,-122.284453 37.836722,-122.284467 37.836721,-122.284494 37.83672,-122.284611 37.837096,-122.28471 37.837394,-122.284798 37.837683,-122.284915 37.838031,-122.285012 37.83801,-122.285053 37.838138,-122.285124 37.838363,-122.285463 37.839424,-122.285576 37.839778))","06001400900"
"POLYGON ((-122.278671 37.827178,-122.278593 37.827164,-122.278528 37.827146,-122.278481 37.827133,-122.27812 37.827033,-122.277956 37.826993,-122.277909 37.827156,-122.277868 37.82732,-122.277977 37.827337,-122.278074 37.827666,-122.278206 37.828111,-122.278226 37.828177,-122.278299 37.828394,-122.27832 37.828454,-122.278343 37.828522,-122.278366 37.828592,-122.278381 37.828635,-122.278426 37.82876,-122.278441 37.828803,-122.278463 37.828864,-122.278369 37.829117,-122.278251 37.829429,-122.278215 37.82954,-122.278061 37.829934,-122.2779 37.830366,-122.277606 37.831136,-122.277556 37.831276,-122.277343 37.831835,-122.277415 37.831849,-122.277842 37.831915,-122.277977 37.831942,-122.277842 37.832304,-122.277821 37.832304,-122.277731 37.832292,-122.277684 37.832286,-122.27754 37.832268,-122.27743 37.832251,-122.277275 37.832227,-122.277204 37.832216,-122.277168 37.832297,-122.276943 37.832898,-122.276766 37.833361,-122.276679 37.833595,-122.27641 37.834279,-122.276146 37.83496,-122.275896 37.835661,-122.275607 37.83643,-122.275638 37.836447,-122.274641 37.836408,-122.274398 37.836441,-122.27367 37.836544,-122.273428 37.836578,-122.273356 37.836582,-122.272977 37.836621,-122.272961 37.836623,-122.272236 37.836722,-122.271628 37.836792,-122.271516 37.836805,-122.27118 37.836857,-122.270804 37.836906,-122.26968 37.837055,-122.269305 37.837105,-122.269268 37.837121,-122.269234 37.837136,-122.269151 37.837149,-122.269112 37.837156,-122.269029 37.837169,-122.268886 37.837192,-122.268784 37.837214,-122.268703 37.837232,-122.268459 37.837275,-122.268438 37.837279,-122.268327 37.837304,-122.268296 37.837307,-122.267935 37.837346,-122.267385 37.837408,-122.267064 37.83744,-122.266653 37.837482,-122.266589 37.837489,-122.266448 37.837511,-122.26612 37.837565,-122.265835 37.837606,-122.265631 37.837636,-122.265647 37.837578,-122.265755 37.837034,-122.266115 37.835227,-122.266118 37.835217,-122.266236 37.834625,-122.266258 37.834514,-122.266299 37.834312,-122.266327 37.834181,-122.266351 37.834071,-122.266379 37.833937,-122.266465 37.833538,-122.266494 37.833405,-122.266514 37.83331,-122.266578 37.833025,-122.266599 37.832931,-122.266628 37.832774,-122.266718 37.832303,-122.266748 37.832147,-122.266777 37.831985,-122.266865 37.8315,-122.266895 37.831339,-122.266939 37.831193,-122.266941 37.831181,-122.267006 37.830878,-122.267043 37.830698,-122.267077 37.830538,-122.267251 37.829656,-122.267277 37.82953,-122.267396 37.828952,-122.267419 37.828852,-122.267456 37.82869,-122.267555 37.828261,-122.267566 37.828205,-122.267599 37.828043,-122.267643 37.827829,-122.267828 37.827044,-122.267913 37.826942,-122.26824 37.826555,-122.2683 37.826485,-122.268368 37.826304,-122.268452 37.826078,-122.268507 37.825931,-122.268541 37.825796,-122.268541 37.825569,-122.268519 37.825298,-122.268411 37.825041,-122.268476 37.824813,-122.268546 37.824532,-122.268563 37.824462,-122.26883 37.824531,-122.269048 37.824571,-122.269348 37.824631,-122.269686 37.824696,-122.27274 37.825464,-122.273365 37.825637,-122.274402 37.825924,-122.274453 37.825935,-122.275061 37.826059,-122.275111 37.826176,-122.275129 37.826104,-122.275136 37.82608,-122.278488 37.826848,-122.278611 37.826878,-122.278638 37.826959,-122.27865 37.826993,-122.27872 37.827188,-122.278671 37.827178))","06001401000"
However, when I try to include this in the report, the map is completely filled. The tutorial (the map of Washington state underneath) works perfectly.
In this image, you can zoom in to see the polygons and they look fine. However, the "fill" does not fill the polygons, but instead the entire map. I've tried changing the CRS, as well as going between multipolygons and simple polygons. I've also played with what goes in the location field. But no matter what, the first record covers the map and is identified on mouseover.
The report is here.
Edit: Using BigQuery Geo Viz, I have found that BigQuery somehow adds a section of coordinates that covers the entire globe:
{ "origin": "06001401000", "WKT": "{ \"type\": \"Polygon\", \"coordinates\": [ [ [180, 90], [-180, 90], [-180, -90], [180, -90], [180, 90] ], [ [-122.278671, 37.827178], [-122.278593, 37.827164], [-122.278528, 37.827146], [-122.278481, 37.827133], [-122.27812, 37.827033], ...
However, I cannot see this in the raw data.
The issue seems to be somewhere in the upload step. Big Query allows for upload of .csv files with WKT, but will not automatically detect WKT (as explained here). If the geo field is manually specified, it seems a set of coordinates that represent the planar map are added. Thus the "fill" field fills the entire map. These coordinates are not visible when querying the data in BigQuery.
A workaround is to let BigQuery convert it after upload:
SELECT ST_GEOGFROM(WKT) as tract_geo,
origin
FROM mydata

Increase speed creation for masked xarray file

I am currently trying to crop a retangular xarray file to the shape of a country using a mask grid. Below you can find my current solution (with simpler and smaller arrays). The code works and I get the desired mask based on 1s and 0s. The problem lies on the fact that the code when run on a real country shape (larger and more complex) takes over 30 minutes to run. Since I am using very basic operations here like nested for loops, I also tried different alternatives like a list approach. However, when timing the process, it did not improve on the code below. I wonder if there is a faster way to obtain this mask (vectorization?) or if I should approach the problem in a different way (tried exploring xarray's properties, but have not found anything that tackles this issue yet).
Code below:
import geopandas as gpd
from shapely.geometry import Polygon, Point
import pandas as pd
import numpy as np
import xarray as xr
df = pd.read_csv('Brazil_borders.csv',index_col=0)
lats = np.array([-20, -5, -5, -20,])
lons = np.array([-60, -60, -30, -30])
lats2 = np.array([-10.25, -10.75, -11.25, -11.75, -12.25, -12.75, -13.25, -13.75,
-14.25, -14.75, -15.25, -15.75, -16.25, -16.75, -17.25, -17.75,
-18.25, -18.75, -19.25, -19.75, -20.25, -20.75, -21.25, -21.75,
-22.25, -22.75, -23.25, -23.75, -24.25, -24.75, -25.25, -25.75,
-26.25, -26.75, -27.25, -27.75, -28.25, -28.75, -29.25, -29.75,
-30.25, -30.75, -31.25, -31.75, -32.25, -32.75])
lons2 = np.array([-61.75, -61.25, -60.75, -60.25, -59.75, -59.25, -58.75, -58.25,
-57.75, -57.25, -56.75, -56.25, -55.75, -55.25, -54.75, -54.25,
-53.75, -53.25, -52.75, -52.25, -51.75, -51.25, -50.75, -50.25,
-49.75, -49.25, -48.75, -48.25, -47.75, -47.25, -46.75, -46.25,
-45.75, -45.25, -44.75, -44.25])
points = []
for i in range(len(lats)):
_= [lats[i],lons[i]]
points.append(_)
poly_proj = Polygon(points)
mask = np.zeros((len(lats2),len(lons2))) # Mask with the dataset's shape and size.
for i in range(len(lats2)): # Iteration to verify if a given coordinate is within the polygon's area
for j in range(len(lons2)):
grid_point = Point(lats2[i], lons2[j])
if grid_point.within(poly_proj):
mask[i][j] = 1
bool_final = mask
bool_final
The alternative based on list approach, but with even worse processing time (according to timeit):
lats = np.array([-20, -5, -5, -20,])
lons = np.array([-60, -60, -30, -30])
lats2 = np.array([-10.25, -10.75, -11.25, -11.75, -12.25, -12.75, -13.25, -13.75,
-14.25, -14.75, -15.25, -15.75, -16.25, -16.75, -17.25, -17.75,
-18.25, -18.75, -19.25, -19.75, -20.25, -20.75, -21.25, -21.75,
-22.25, -22.75, -23.25, -23.75, -24.25, -24.75, -25.25, -25.75,
-26.25, -26.75, -27.25, -27.75, -28.25, -28.75, -29.25, -29.75,
-30.25, -30.75, -31.25, -31.75, -32.25, -32.75])
lons2 = np.array([-61.75, -61.25, -60.75, -60.25, -59.75, -59.25, -58.75, -58.25,
-57.75, -57.25, -56.75, -56.25, -55.75, -55.25, -54.75, -54.25,
-53.75, -53.25, -52.75, -52.25, -51.75, -51.25, -50.75, -50.25,
-49.75, -49.25, -48.75, -48.25, -47.75, -47.25, -46.75, -46.25,
-45.75, -45.25, -44.75, -44.25])
points = []
for i in range(len(lats)):
_= [lats[i],lons[i]]
points.append(_)
poly_proj = Polygon(points)
grid_point = [Point(lats2[i],lons2[j]) for i in range(len(lats2)) for j in range(len(lons2))]
mask = [1 if grid_point[i].within(poly_proj) else 0 for i in range(len(grid_point))]
bool_final2 = np.reshape(mask,(((len(lats2)),(len(lons2)))))
Thank you in advance!
Based on this answer from snowman2, I created this simple function that provides a much faster solution by using geopandas and rioxarray. Instead of using a list of latitudes and longitudes, one has to use a shapefile with the desired shape to be masked (Instructions for GeoDataFrame creation from list of coordinates).
import xarray as xr
import geopandas as gpd
import rioxarray
from shapely.geometry import mapping
def mask_shape_border (DS,shape_shp): #Inputs are the dataset to be cropped and the address of the mask file (.shp )
if 'lat' in DS: #Some datasets use lat/lon, others latitude/longitude
DS.rio.set_spatial_dims(x_dim="lon", y_dim="lat", inplace=True)
elif 'latitude' in DS:
DS.rio.set_spatial_dims(x_dim="longitude", y_dim="latitude", inplace=True)
else:
print("Error: check latitude and longitude variable names.")
DS.rio.write_crs("epsg:4326", inplace=True)
mask = gpd.read_file(shape_shp, crs="epsg:4326")
DS_clipped = DS.rio.clip(mask.geometry.apply(mapping), mask.crs, drop=False)
return(DS_clipped)

using lookup tables to plot a ggplot and table

I'm creating a shiny app and i'm letting the user choose what data that should be displayed in a plot and a table. This choice is done through 3 different input variables that contain 14, 4 and two choices respectivly.
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
selectInput(inputId = "DataSource", label = "Data source", choices =
c("Restoration plots", "all semi natural grasslands")),
selectInput(inputId = "Variabel", label = "Variable", choices =
choicesVariables)),
#choicesVariables definition is omitted here, because it's very long but it
#contains 14 string values
selectInput(inputId = "Factor", label = "Factor", choices = c("Company
type", "Region and type of application", "Approved or not approved
applications", "Age group" ))
),
dashboardBody(
plotOutput("thePlot"),
tableOutput("theTable")
))
This adds up to 73 choices (yes, i know the math doesn't add up there, but some choices are invalid). I would like to do this using a lookup table so a created one with every valid combination of choices like this:
rad1<-c(rep("Company type",20), rep("Region and type of application",20),
rep("Approved or not approved applications", 13), rep("Age group", 20))
rad2<-choicesVariable[c(1:14,1,4,5,9,10,11, 1:14,1,4,5,9,10,11, 1:7,9:14,
1:14,1,4,5,9,10,11)]
rad3<-c(rep("Restoration plots",14),rep("all semi natural grasslands",6),
rep("Restoration plots",14), rep("all semi natural grasslands",6),
rep("Restoration plots",27), rep("all semi natural grasslands",6))
rad4<-1:73
letaLista<-data.frame(rad1,rad2,rad3, rad4)
colnames(letaLista) <- c("Factor", "Variabel", "rest_alla", "id")
Now its easy to use subset to only get the choice that the user made. But how do i use this information to plot the plot and table without using a 73 line long ifelse statment?
I tried to create some sort of multidimensional array that could hold all the tables (and one for the plots) but i couldn't make it work. My experience with these kind of arrays is limited and this might be a simple issue, but any hints would be helpful!
My dataset that is the foundation for the plots and table consists of dataframe with 23 variables, factors and numerical. The plots and tabels are then created using the following code for all 73 combinations
s_A1 <- summarySE(Samlad_info, measurevar="Dist_brukcentrum",
groupvars="Companytype")
s_A1 <- s_A1[2:6,]
p_A1=ggplot(s_A1, aes(x=Companytype,
y=Dist_brukcentrum))+geom_bar(position=position_dodge(), stat="identity") +
geom_errorbar(aes(ymin=Dist_brukcentrum-se,
ymax=Dist_brukcentrum+se),width=.2,position=position_dodge(.9))+
scale_y_continuous(name = "") + scale_x_discrete(name = "")
where summarySE is the following function, burrowed from cookbook for R
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=TRUE,
conf.interval=.95, .drop=TRUE) {
# New version of length which can handle NA's: if na.rm==T, don't count them
length2 <- function (x, na.rm=FALSE) {
if (na.rm) sum(!is.na(x))
else length(x)
}
# This does the summary. For each group's data frame, return a vector with
# N, mean, and sd
datac <- ddply(data, groupvars, .drop=.drop,
.fun = function(xx, col) {
c(N = length2(xx[[col]], na.rm=na.rm),
mean = mean (xx[[col]], na.rm=na.rm),
sd = sd (xx[[col]], na.rm=na.rm)
)
},
measurevar
)
# Rename the "mean" column
datac <- rename(datac, c("mean" = measurevar))
datac$se <- datac$sd / sqrt(datac$N) # Calculate standard error of the mean
# Confidence interval multiplier for standard error
# Calculate t-statistic for confidence interval:
# e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
ciMult <- qt(conf.interval/2 + .5, datac$N-1)
datac$ci <- datac$se * ciMult
return(datac)
}
The code in it's entirety is a bit to large but i hope this may clarify what i'm trying to do.
Well, thanks to florian's comment i think i might have found a solution my self. I'll present it here but leave the question open as there is probably far neater ways of doing it.
I rigged up the plots (that was created as lists by ggplot) into a list
plotList <- list(p_A1, p_A2, p_A3...)
tableList <- list(s_A1, s_A2, s_A3...)
I then used subset on my lookup table to get the matching id of the list to select the right plot and table.
output$thePlot <-renderPlot({
plotValue<-subset(letaLista, letaLista$Factor==input$Factor &
letaLista$Variabel== input$Variabel & letaLista$rest_alla==input$DataSource)
plotList[as.integer(plotValue[1,4])]
})
output$theTable <-renderTable({
plotValue<-subset(letaLista, letaLista$Factor==input$Factor &
letaLista$Variabel== input$Variabel & letaLista$rest_alla==input$DataSource)
skriva <- tableList[as.integer(plotValue[4])]
print(skriva)
})

SQL Server : geography data type: GeomFromGml

I have troubles to load data into a geography data type:
DECLARE #g geography;
DECLARE #x xml;
SET #x = '<gml:MultiSurface gml:id="HVO.20584" srsName="urn:ogc:def:crs:EPSG::5514" srsDimension="2" xmlns:gml="http://www.opengis.net/gml/3.2">
<gml:surfaceMember>
<gml:Polygon gml:id="HVO.20584.1">
<gml:exterior>
<gml:LinearRing>
<gml:posList>-747267.00 -1056204.00 -747226.00 -1056232.00 -747239.00 -1056253.00 -747251.00 -1056278.00 -747267.00 -1056315.00 -747291.00 -1056365.00 -747337.00 -1056470.00 -747311.00 -1056474.00 -747244.00 -1056475.00 -747155.00 -1056467.00 -747077.00 -1056501.00 -747112.00 -1057366.00 -747263.00 -1057857.00 -747263.20 -1057857.05 -747263.30 -1057856.60 -747272.50 -1057858.50 -747276.10 -1057850.21 -747285.47 -1057849.89 -747295.34 -1057849.55 -747308.73 -1057848.34 -747320.38 -1057847.28 -747346.97 -1057857.70 -747358.66 -1057860.00 -747374.12 -1057860.55 -747386.06 -1057860.19 -747407.98 -1057859.54 -747444.60 -1057874.26 -747447.75 -1057879.52 -747454.57 -1057890.92 -747674.02 -1057865.46 -747851.54 -1057841.44 -747855.78 -1057838.67 -747871.68 -1057851.47 -747902.20 -1057870.55 -747916.06 -1057881.32 -747934.07 -1057891.87 -747960.03 -1057915.53 -747985.14 -1057954.40 -748001.13 -1057977.02 -748021.22 -1058003.59 -748088.58 -1058092.68 -748104.73 -1058095.49 -748166.00 -1058032.60 -748193.50 -1058002.40 -748214.40 -1057982.20 -748216.40 -1057988.60 -748293.20 -1058053.20 -748297.14 -1058050.03 -748302.55 -1058045.67 -748306.53 -1058042.47 -748311.89 -1058038.15 -748316.50 -1058034.44 -748320.83 -1058030.95 -748330.04 -1058023.54 -748332.15 -1058021.83 -748337.10 -1058017.85 -748341.39 -1058014.39 -748353.30 -1058004.80 -748347.67 -1057996.70 -748338.28 -1057981.31 -748335.70 -1057977.29 -748332.12 -1057971.71 -748330.30 -1057968.92 -748325.55 -1057961.32 -748317.05 -1057947.55 -748310.89 -1057935.02 -748307.10 -1057924.52 -748302.70 -1057912.35 -748298.54 -1057900.12 -748295.48 -1057891.13 -748286.20 -1057862.27 -748278.37 -1057838.25 -748276.55 -1057834.04 -748267.49 -1057808.20 -748264.92 -1057798.33 -748262.21 -1057787.93 -748254.57 -1057761.59 -748251.89 -1057752.13 -748247.59 -1057736.93 -748240.11 -1057716.13 -748237.47 -1057685.67 -748234.93 -1057657.42 -748234.51 -1057652.51 -748234.98 -1057625.54 -748234.51 -1057622.46 -748231.40 -1057602.21 -748233.13 -1057596.29 -748234.13 -1057592.87 -748237.61 -1057582.60 -748238.48 -1057580.02 -748249.72 -1057571.70 -748250.10 -1057571.42 -748252.39 -1057569.73 -748256.83 -1057563.57 -748258.55 -1057540.99 -748250.24 -1057535.67 -748250.94 -1057526.41 -748243.01 -1057522.35 -748236.51 -1057522.75 -748234.73 -1057522.86 -748230.54 -1057518.95 -748211.65 -1057501.36 -748199.48 -1057493.59 -748189.98 -1057489.26 -748179.90 -1057484.66 -748168.54 -1057470.65 -748168.29 -1057470.34 -748165.98 -1057467.49 -748157.92 -1057458.13 -748147.31 -1057450.99 -748146.04 -1057423.54 -748145.40 -1057410.42 -748144.91 -1057400.47 -748144.08 -1057372.43 -748142.62 -1057348.91 -748142.48 -1057347.16 -748141.05 -1057329.37 -748142.26 -1057321.94 -748146.21 -1057304.13 -748148.14 -1057295.42 -748146.30 -1057289.44 -748144.73 -1057284.34 -748141.64 -1057275.31 -748144.63 -1057272.64 -748154.01 -1057247.80 -748151.29 -1057246.55 -748127.50 -1057235.56 -748110.70 -1057228.23 -748082.39 -1057214.75 -748057.48 -1057203.76 -748041.82 -1057196.01 -748029.14 -1057189.74 -748010.10 -1057178.36 -747979.25 -1057161.25 -747957.25 -1057147.63 -747943.14 -1057138.66 -747937.35 -1057131.26 -747926.97 -1057121.60 -747909.48 -1057109.13 -747904.01 -1057106.36 -747895.25 -1057106.99 -747873.33 -1057094.64 -747869.74 -1057093.00 -747851.77 -1057082.31 -747869.79 -1057044.54 -747836.92 -1057032.09 -747795.65 -1057016.73 -747793.70 -1057009.93 -747787.82 -1056992.87 -747781.28 -1056974.44 -747776.81 -1056960.35 -747768.14 -1056933.57 -747762.16 -1056911.71 -747757.80 -1056895.21 -747753.30 -1056877.50 -747749.17 -1056859.18 -747744.25 -1056838.69 -747741.79 -1056821.91 -747739.99 -1056803.90 -747737.12 -1056781.92 -747735.82 -1056761.48 -747735.01 -1056740.42 -747736.57 -1056718.66 -747738.55 -1056694.68 -747741.17 -1056669.07 -747744.02 -1056634.01 -747745.17 -1056623.46 -747748.09 -1056596.69 -747755.37 -1056564.46 -747759.01 -1056547.20 -747761.29 -1056536.38 -747761.56 -1056535.10 -747772.54 -1056491.85 -747774.05 -1056485.90 -747786.35 -1056435.41 -747786.99 -1056433.22 -747796.41 -1056400.93 -747799.01 -1056392.00 -747799.01 -1056392.00 -747799.00 -1056392.00 -747723.00 -1056390.00 -747615.00 -1056374.00 -747596.00 -1056372.00 -747564.00 -1056375.00 -747539.00 -1056382.00 -747513.00 -1056389.00 -747487.00 -1056344.00 -747472.00 -1056316.00 -747458.00 -1056290.00 -747455.00 -1056283.00 -747445.00 -1056265.00 -747433.00 -1056243.00 -747424.00 -1056225.00 -747420.00 -1056218.00 -747412.00 -1056205.00 -747399.00 -1056188.00 -747388.00 -1056173.00 -747383.00 -1056167.00 -747371.00 -1056154.00 -747370.00 -1056153.00 -747362.00 -1056145.00 -747356.00 -1056141.00 -747356.00 -1056140.00 -747334.00 -1056160.00 -747267.00 -1056204.00</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</gml:surfaceMember>
</gml:MultiSurface>';
SET #g = geography::GeomFromGml(#x, 4326);
I get this exception:
A .NET Framework error occurred during execution of user-defined
routine or aggregate "geography": System.FormatException: 24129: The
given XML instance is not valid because the top-level tag is
gml:MultiSurface. The top-level element of the input Geographic Markup
Language (GML) must contain a Point, LineString, Polygon, MultiPoint,
MultiGeometry, MultiCurve, MultiSurface, Arc, ArcString,
CompositeCurve, PolygonPatch or FullGlobe (geography Data Type only)
object.
What do I need to change in the #x variable to get it working?
Thanks
Note, the answer here refers to the question of dealing with the coordinates above, that are not geodesic, as raised in the comments, and not with the original question of parsing the XML, which was answered in the comments.
After a bit of digging, it seems that the Czech Republic's projection is now known as http://spatialreference.org/ref/sr-org/czech-s-jtsk-gis-esri102067/. If you look at that page you will see that the coordinates are in meters, and are negative, just as in your XML above. This WKT representation gives you information about the datum, spheroid and other characteristics of the projection.
SQL Server does not currently support projecting from one coordinate system to another, so you have two options.
1). Load it as is, but as a geometry not a geography datatype, so your last line above would become
SET #g = geometry::GeomFromGml(#x, 5514);
where 9819 appears to be the SRID code in the above link (although it is all a bit unclear). This actually doesn't matter that much, as you can't convert from one spatial reference system to another, but you will be able to perform any geometry functions on your geometries, such as STIntersects, STContains, STBuffer, etc.
2). Convert it to lat/lon (4326) outside of SQL Server and then import as a geography data type, if that it what you really want. There is as tool called gdaltransform which does this, eg,
gdaltransform -s_srs "+proj=krovak +lat_0=49.5 +lon_0=24.83333333333333 +alpha=0 +k=0.9999 +x_0=0 +y_0=0 +ellps=bessel +towgs84=570.8,85.7,462.8,4.998,1.587,5.261,3.56 +units=m +no_defs" -t_srs EPSG:4326
-747267.00 -1056204.00
24.8307791587916 50.4239544736181 32.0127759873867
14.3847700978484 49.9644982150517 45.4088854994625
where you enter the first line, with the projection info, hit Return, then enter the coordinates and then you get the answer. From this you can see that it is giving a lat/lon 14.38, 49.96, which is in Prague, so definitely the Czech Republic.
This approach isn't very scalable, so you would probably want to look at something like Proj4j which is a Java library for doing coordinate transforms.

Why is NSLayoutAttributeCenterX an 'Invalid pairing' with NSLayoutAttributeWidth

I am trying to have the auto layout manager adjust the center point of a view, based on the width of the super view. I don't understand why that is an 'Invalid Pairing' of the attributes (as told by the crash and NSInvalidArgumentException)
UIView *ac;
NSLayoutConstraint *cXloc = [NSLayoutConstraint constraintWithItem:ac
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:ac.superview
attribute:NSLayoutAttributeWidth
multiplier:.1
constant:x*ac.superview.frame.size.width*.2];
[ac.superview addConstraint:cXloc];
Could someone explain why this is an 'Invalid paring' and how I should approach this?
Thanks
It is the limitation of the current implementation of Auto Layout. However, you can easily work around it since all the constrains are linear and NSLayoutAttributes are correlated. For example, say, the constraint you want is:
subview.centerX = m * superview.width + c;
You can express it as a relationship between tow centerXs:
// Since width == 2 * centerX
subview.centerX = m * 2 * superview.centerX + c;
If you relate ac's AttributeCenterX to its superview's AttributeCenterX, AttributeLeading, or AttributeTrailing, you should be able to express your desired constraint using the multiplier and constraint. Keep in mind that the constant is evaluated only when the constraint is created, and your example's constant wouldn't update as ac.superview's width changes.
If you can express in words how you'd like ac positioned relative to its superview, we can suggest a constraint.
Edit
Here's an example with 5 NSButtons. They themselves and the space between them expand so that the spaces are 30% as wide as the buttons, all the buttons have the same width, and all the spaces have the same width. Creating 4 invisible NSViews just for spacing is pretty cumbersome, especially considering you've got it working outside of autolayout. But in case you're curious:
// Assuming these NSViews and NSButtons exist,
//NSView* superview ;
//NSButton *buttonOne, *buttonTwo, *buttonThree, *buttonFour, *buttonFive ;
[superView removeConstraints:superView.constraints] ;
// Create empty NSViews to fill the space between the 5 buttons.
NSView* spaceOne = [NSView new] ;
NSView* spaceTwo = [NSView new] ;
NSView* spaceThree = [NSView new] ;
NSView* spaceFour = [NSView new] ;
spaceOne.translatesAutoresizingMaskIntoConstraints = NO ;
spaceTwo.translatesAutoresizingMaskIntoConstraints = NO ;
spaceThree.translatesAutoresizingMaskIntoConstraints = NO ;
spaceFour.translatesAutoresizingMaskIntoConstraints = NO ;
[superView addSubview:spaceOne] ;
[superView addSubview:spaceTwo] ;
[superView addSubview:spaceThree] ;
[superView addSubview:spaceFour] ;
NSDictionary* views = NSDictionaryOfVariableBindings(superView,buttonOne,buttonTwo,buttonThree,buttonFour,buttonFive,spaceOne,spaceTwo,spaceThree,spaceFour) ;
// Vertically align buttonOne to its superview however you like.
[superView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-[buttonOne]" options:0 metrics:nil views:views ] ] ;
// Make the "space" NSViews' widths equal and >= 10. Make the buttons' widths equal.
[superView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:#"H:|[buttonOne][spaceOne(>=10)][buttonTwo(==buttonOne)][spaceTwo(==spaceOne)][buttonThree(==buttonOne)][spaceThree(==spaceOne)][buttonFour(==buttonOne)][spaceFour(==spaceOne)][buttonFive(==buttonOne)]|" options: NSLayoutFormatAlignAllCenterY metrics:nil views:views ] ] ;
// Make the "space" NSViews' widths 30% of the NSButtons' widths.
[superView addConstraint: [NSLayoutConstraint constraintWithItem: spaceOne
attribute: NSLayoutAttributeWidth
relatedBy: NSLayoutRelationEqual
toItem: buttonOne
attribute: NSLayoutAttributeWidth
multiplier: 0.3
constant: 0 ] ] ;
Based on an0's answer, and assuming you have an NSArray containing your buttons, the following should space the buttons equally within the superview:
NSUInteger currentButton = 1;
for (UIButton *button in self.buttons)
{
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:2.0 * (CGFloat) currentButton / (CGFloat) (self.buttons.count + 1) constant:0.0]];
currentButton++;
}
If you're looking to have your programmatically generated views to fit its superview's width. You can use the constraint pairing of NSLayoutAttributeLeading and attribute:NSLayoutAttributeCenterX
You have to make the proper calculation in order to get the right multiplier. Calculation involves the total count of the views to be laid out and the index of the current view.
//Caculate constraint multiplier from parentView CenterX.
//This sets the width of the button relative to parentView.
// A value of 2 = Full width.
CGFloat multiplier = 2/(arr.count/counter);
[parentView addConstraint:[NSLayoutConstraint constraintWithItem:btn
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual toItem:parentView
attribute:NSLayoutAttributeCenterX multiplier:multiplier constant:0]];
This will distribute the views width to fill its superview.

Resources