1

So, by definition, if we have polygons A and B, A.difference(B) should return:

1) the same polygon if A does not intersect with B

2) the difference of A and B if they intersect

3) empty geometry if A and B are equal

In case 3) A.difference(B) and B.difference(A) should produce the same results, but this is failing for me.

These are the polygons:

a {'type': 'Polygon', 'coordinates': (((4151073.295823495, 2913157.8323019724), (4151117.0683254446, 2913187.4071865543), (4151124.6724597723, 2913194.0262439107), (4151139.7999071423, 2913207.2669556076), (4151155.3407511846, 2913220.8036157894), (4151171.6241492364, 2913240.0029458604), (4151186.4709844757, 2913259.804181751), (4151211.2833097167, 2913290.661555859), (4151218.810210139, 2913296.1088672774), (4151250.1418168447, 2913341.593073344), (4151266.9612248917, 2913365.040306387), (4151289.2805723376, 2913398.943808798), (4151313.8540199697, 2913450.6997813866), (4151317.7893829998, 2913478.3878766457), (4151322.3057944668, 2913515.637555257), (4151322.8372046226, 2913533.9772354835), (4151319.225544508, 2913572.4118478335), (4151318.6653859527, 2913606.299598947), (4151299.4916320005, 2913634.3499660823), (4151279.963044623, 2913676.9362144554), (4151257.6558665647, 2913712.070311522), (4151217.581151669, 2913765.8777630455), (4151195.023589876, 2913782.6017191466), (4151159.5566063435, 2913802.702376749), (4151095.4975393824, 2913820.9919063714), (4151069.3090835577, 2913823.6780184936), (4151066.9986895644, 2913823.9345682254), (4151067.8749254975, 2913825.101499152), (4151079.3763717297, 2913839.306346547), (4151087.070326084, 2913848.827394531), (4151112.637354185, 2913843.070768149), (4151118.284472024, 2913841.4060348487), (4151135.1335936766, 2913836.0500173173), (4151154.737958126, 2913828.319720326), (4151170.808301693, 2913821.3197671613), (4151184.2944309516, 2913814.401904982), (4151198.7438274594, 2913805.970140542), (4151213.470063, 2913796.0462707193), (4151223.8219909878, 2913787.1265735626), (4151229.594137481, 2913784.285686334), (4151281.4923110898, 2913729.8559282133), (4151308.2509348155, 2913694.889732589), (4151334.638575634, 2913705.054236413), (4151338.894812416, 2913679.516740678), (4151329.0867853304, 2913641.075139272), (4151332.8667826997, 2913631.3752253386), (4151344.0295374114, 2913590.105239949), (4151348.211180454, 2913571.2452367786), (4151354.626946153, 2913542.3635358685), (4151340.726054679, 2913478.9582887553), (4151339.6937881573, 2913431.585444202), (4151311.283828078, 2913369.567570044), (4151304.222320671, 2913359.5935044563), (4151285.3185409377, 2913335.408811127), (4151248.920523185, 2913297.2547562886), (4151209.881627287, 2913258.6902701557), (4151179.878364305, 2913218.2322497997), (4151135.3883144706, 2913176.009316536), (4151108.410413926, 2913154.8010543925), (4151069.1076708515, 2913111.7340410813), (4151065.7344737262, 2913109.8633789085), (4151068.1385514736, 2913113.062761468), (4151074.6334935096, 2913122.9927311377), (4151074.920437797, 2913128.2989790915), (4151074.0901237642, 2913134.073360427), (4151073.636461171, 2913136.3746121493), (4151051.970190195, 2913122.1674691914), (4151050.525896679, 2913121.2244458017), (4151024.116972458, 2913104.015941311), (4150990.625923501, 2913073.6205919855), (4150982.0746572507, 2913059.9858671078), (4150968.39412521, 2913042.6203588275), (4150951.7125657904, 2913015.8938663886), (4150937.907710566, 2912988.087073018), (4150899.115091983, 2912900.627907833), (4150890.9299924965, 2912880.6156004188), (4150875.0874337214, 2912848.423675646), (4150846.916033796, 2912772.3085753997), (4150842.5486273416, 2912773.5599923627), (4150839.380047315, 2912764.0189435096), (4150820.323756362, 2912687.243242995), (4150815.1604581107, 2912659.161884642), (4150811.3140740427, 2912643.4617573605), (4150804.9810044947, 2912628.3969925344), (4150801.229399168, 2912601.5068737348), (4150799.9052290497, 2912577.259027613), (4150800.213425931, 2912565.1351433927), (4150800.487184315, 2912554.4957119375), (4150802.980771587, 2912530.8062982853), (4150806.3730488513, 2912511.291145292), (4150808.2494724654, 2912505.359839729), (4150812.22152953, 2912509.5599519713), (4150814.1480010385, 2912503.936089415), (4150816.569976392, 2912492.177622971), (4150814.662051406, 2912491.9292758536), (4150811.5443930165, 2912491.7812147234), (4150812.382454003, 2912487.5517152827), (4150818.415678176, 2912469.7449625204), (4150825.94334231, 2912451.890688544), (4150835.096135575, 2912432.995840086), (4150846.7800170556, 2912413.5878746584), (4150859.1103562796, 2912395.457318749), (4150873.485349481, 2912378.065268402), (4150891.3280398827, 2912359.079660348), (4150909.6864053262, 2912341.0666281017), (4150930.6413129205, 2912322.044038061), (4150951.8830724414, 2912304.4339679414), (4150959.7681310456, 2912296.7048094682), (4150963.4144920874, 2912293.0659690117), (4150967.645498271, 2912288.728682452), (4150965.143117878, 2912287.572073632), (4150977.1093621454, 2912277.17919187), (4150980.0249707215, 2912274.737905293), (4150974.414561366, 2912275.03979056), (4150979.774488489, 2912269.306901839), (4150987.9082905613, 2912268.2449751366), (4151019.0796000278, 2912234.5590853975), (4151031.0225685374, 2912218.2336673574), (4151048.3827243187, 2912186.3464427046), (4151059.63261466, 2912156.8783656145), (4151065.612582447, 2912098.4669084335), (4151068.4571636715, 2912065.24839702), (4151068.418537189, 2912062.7155656535), (4151068.0849152883, 2912048.143236527), (4151067.36492072, 2912038.0298698735), (4151064.4247114337, 2912018.9632933536), (4151062.1171936393, 2911998.1460503433), (4151058.9234438995, 2911985.2063515354), (4151056.416520606, 2911974.8407057803), (4151043.8388003316, 2911942.3592281765), (4151029.5380583648, 2911918.152741258), (4151014.5485843187, 2911893.906361687), (4150992.16458565, 2911863.343472419), (4150971.7237982084, 2911841.928064542), (4150958.9469216247, 2911830.281817916), (4150962.0031410404, 2911827.1561972885), (4150918.088735088, 2911798.5757059823), (4150884.3002524, 2911779.624471341), (4150857.7063164245, 2911768.355901734), (4150760.314551059, 2911741.6626190473), (4150756.6365854377, 2911740.4198848065), (4150751.573625364, 2911738.8503767108), (4150746.8742117486, 2911737.2693071696), (4150743.1193127306, 2911736.152642333), (4150734.8304309, 2911733.5732702073), (4150724.242607587, 2911728.5948054106), (4150719.9797917316, 2911726.752641424), (4150716.4537289124, 2911725.1960688196), (4150712.983348486, 2911722.8342437986), (4150708.4876530515, 2911720.0105972593), (4150705.0980549837, 2911717.6462065466), (4150700.683140337, 2911714.81999566), (4150697.4667017125, 2911712.8209373215), (4150692.6111379126, 2911708.834434878), (4150691.226461449, 2911711.1035313616), (4150671.7755028848, 2911696.8272545063), (4150651.3238872555, 2911680.2960620667), (4150663.066845515, 2911676.955564934), (4150646.7145385854, 2911647.994439245), (4150646.297412245, 2911650.170940815), (4150644.264295311, 2911665.316418281), (4150642.637256763, 2911667.5932425074), (4150634.7309193485, 2911664.3219723618), (4150629.184334272, 2911657.6380426222), (4150612.202017467, 2911636.9791287202), (4150600.6274819686, 2911621.154390076), (4150595.0400493164, 2911611.87592571), (4150583.9006004985, 2911572.489131962), (4150576.6299584513, 2911576.614466205), (4150576.598848497, 2911574.3286204305), (4150576.3325294717, 2911568.4036879665), (4150569.38853675, 2911570.0463739545), (4150522.8430996155, 2911458.8558281776), (4150519.7429732536, 2911451.537815879), (4150499.4065184216, 2911405.1510123676), (4150485.155624129, 2911378.781073015), (4150479.300465584, 2911370.005702123), (4150468.4829114387, 2911353.848078058), (4150449.9251275654, 2911330.7057825765), (4150442.13852406, 2911322.2392258462), (4150426.6441078633, 2911305.2418268053), (4150416.3446006663, 2911294.0123021086), (4150371.536965873, 2911247.4788894607), (4150369.872677183, 2911245.9867896196), (4150344.9121099375, 2911223.7288437095), (4150338.895697712, 2911218.8525625044), (4150325.0388714382, 2911207.6748324353), (4150304.7424252885, 2911192.314259757), (4150300.3133430374, 2911189.056167824), (4150281.9005082548, 2911175.7370199086), (4150262.225547467, 2911162.1491638864), (4150242.985454899, 2911149.5364058004), (4150231.514514495, 2911142.2383982455), (4150223.2461650497, 2911137.0342378835), (4150215.050709781, 2911131.761514462), (4150216.2697798028, 2911129.715300883), (4150204.552779846, 2911123.862852196), (4150177.759275535, 2911109.102195952), (4150180.2396336016, 2911104.6809140556), (4150186.630487088, 2911096.6511694314), (4150189.2146380167, 2911091.9509632415), (4150186.250933546, 2911090.2853956567), (4150183.3585071624, 2911094.7659664713), (4150161.046846077, 2911096.40520112), (4150138.312302498, 2911081.988347453), (4150117.265588527, 2911071.1024730452), (4150092.914086003, 2911061.867369296), (4150088.54990146, 2911065.838731693), (4150080.457515416, 2911061.8325057565), (4150080.598843239, 2911057.316118988), (4150077.8762279092, 2911055.60597522), (4150070.405031375, 2911050.7777851447), (4150049.3428575606, 2911040.6961476663), (4150041.279136034, 2911036.3148658127), (4150021.159904455, 2911025.339739994), (4150022.8366839625, 2911022.072236586), (4150017.5495220367, 2911019.8306847997), (4149994.318483104, 2911007.6553161666), (4149962.1340828184, 2910988.534791087), (4149956.7922866084, 2910985.8605828923), (4149952.9303701967, 2910982.646448296), (4149905.678750463, 2910940.89396082), (4149897.3115905873, 2910932.014068928), (4149887.505170323, 2910922.376717036), (4149871.201658317, 2910911.8963969713), (4149845.2811972676, 2910888.806073511), (4149812.1779668415, 2910855.932932486), (4149796.2440474005, 2910840.4964849586), (4149789.5052069384, 2910842.257203608), (4149762.5904229274, 2910815.861512387), (4149757.3740497297, 2910813.3089101957), (4149736.9532955005, 2910804.382258416), (4149722.108611504, 2910797.8731224798), (4149700.4780550525, 2910783.917179552), (4149669.962939733, 2910770.3693157136), (4149671.1947978036, 2910772.2458892567), (4149654.953023458, 2910777.895747722), (4149638.537979212, 2910776.752477006), (4149635.0859720455, 2910775.0087969094), (4149608.330512876, 2910744.714929638), (4149597.6305333083, 2910734.9830828365), (4149595.3676279313, 2910733.7576086083), (4149588.6836687326, 2910728.7663233895), (4149564.0985172945, 2910710.4072218826), (4149570.938108824, 2910710.559002315), (4149545.5908008213, 2910687.760740009), (4149472.146343369, 2910565.7588505615), (4149457.028208964, 2910544.1783626284), (4149442.155641139, 2910526.545668819), (4149422.712173151, 2910505.042095066), (4149405.81427257, 2910489.884875394), (4149388.549206326, 2910475.9138040165), (4149370.074339512, 2910463.3413221454), (4149346.423341052, 2910449.513332209), (4149323.839225491, 2910438.741559347), (4149304.757955124, 2910431.318696558), (4149286.85271654, 2910425.279728024), (4149257.2891599485, 2910417.637038025), (4149251.482167468, 2910416.8962529358), (4149250.555301679, 2910418.285738925), (4149250.190681988, 2910420.8315123515), (4149247.8502432783, 2910424.861471087), (4149251.1769862245, 2910430.317333131), (4149255.4393353774, 2910431.728955996), (4149287.237667055, 2910438.0658173887), (4149299.1976864217, 2910441.0699523087), (4149299.8717436763, 2910439.0683012935), (4149309.403063632, 2910443.963304446), (4149348.462777652, 2910460.325904876), (4149357.0896180156, 2910464.5611579665), (4149404.5960135865, 2910499.9984123344), (4149415.0606337492, 2910511.220799175), (4149433.376270228, 2910531.5861688755), (4149460.8819084032, 2910572.794947752), (4149493.789175987, 2910630.1477327547), (4149491.1673279656, 2910631.6532936734), (4149519.755333093, 2910679.008337089), (4149529.3275378514, 2910693.1223406107), (4149532.043600874, 2910691.529852805), (4149560.115768837, 2910731.67828806), (4149590.304225936, 2910756.41306064), (4149620.1481526243, 2910780.230402206), (4149657.2594316565, 2910807.7654043105), (4149661.178792285, 2910803.531610376), (4149677.349400017, 2910804.62096483), (4149683.3913259353, 2910807.7033263985), (4149701.901240729, 2910821.450038088), (4149749.948595552, 2910856.2548239664), (4149794.232130951, 2910897.422877999), (4149794.1176453577, 2910873.1983655524), (4149803.976148514, 2910875.4789073234), (4149839.714336766, 2910920.134592974), (4149850.8963993625, 2910929.78967846), (4149865.982418181, 2910941.421382658), (4149903.1073809853, 2910973.6100110915), (4149928.26737754, 2910996.972129958), (4149940.133058115, 2911005.2439840203), (4149995.009702004, 2911043.853392857), (4150049.0502234614, 2911072.59764788), (4150072.829016034, 2911084.1994904648), (4150076.3687907313, 2911075.866181032), (4150081.322991002, 2911077.8093890343), (4150077.754438905, 2911086.5144529594), (4150112.4792245827, 2911114.4546645507), (4150124.579331914, 2911121.1760787647), (4150131.6579117156, 2911108.3415656164), (4150156.798870802, 2911122.125315469), (4150212.4569774135, 2911157.3710165555), (4150213.8893219745, 2911172.093693274), (4150224.825555737, 2911189.4220412043), (4150249.589551816, 2911206.679397572), (4150286.6906454754, 2911235.6573921326), (4150352.503830345, 2911270.9450739212), (4150373.855523534, 2911282.754877506), (4150408.5286637135, 2911316.8825723305), (4150449.628369814, 2911362.5455328412), (4150482.213558437, 2911410.087039304), (4150499.9393228097, 2911441.5380019704), (4150520.399184698, 2911490.578552183), (4150554.2556434167, 2911571.022779899), (4150556.5860260455, 2911577.0055858693), (4150558.6605014093, 2911582.563898351), (4150567.119946342, 2911604.7920110333), (4150569.037664128, 2911604.051066638), (4150577.1329149036, 2911622.3969893022), (4150586.505298821, 2911640.2078029565), (4150595.6027763565, 2911659.5725456295), (4150601.1568509242, 2911650.9900229243), (4150614.7841022895, 2911669.098048389), (4150623.7820035564, 2911678.8242081422), (4150632.5817640526, 2911688.6803029166), (4150639.9973605834, 2911690.483837952), (4150643.3783946857, 2911700.327034732), (4150649.1637745295, 2911709.475632058), (4150666.8523380035, 2911720.4085727027), (4150696.2500736085, 2911735.9752390888), (4150741.2153437454, 2911755.4349962035), (4150780.8195174555, 2911758.625158131), (4150847.7685754425, 2911780.2914619897), (4150867.7931987667, 2911787.071551859), (4150886.071303546, 2911794.896135785), (4150903.708954889, 2911804.224489055), (4150923.6762180165, 2911816.940010651), (4150951.6612534686, 2911837.806472459), (4150979.246392069, 2911864.001120347), (4151001.6725961226, 2911892.028559813), (4151012.6338451994, 2911908.9861114426), (4151019.551515238, 2911920.7567864875), (4151033.899080636, 2911954.232703503), (4151031.5951147513, 2911959.435805462), (4151041.4157765093, 2911980.0144049283), (4151041.944906596, 2911982.717075696), (4151045.9180600075, 2912001.194577418), (4151050.756861119, 2912021.4987781206), (4151051.5734277377, 2912037.295255141), (4151049.8081222265, 2912044.20845912), (4151049.2245088895, 2912090.27268964), (4151039.002180304, 2912128.0519934935), (4151025.2913187924, 2912170.6394225215), (4151001.5182855567, 2912222.4466850124), (4150986.84224256, 2912237.993721558), (4150968.74327684, 2912260.448274831), (4150965.01280039, 2912270.4558421634), (4150964.712820048, 2912271.207051588), (4150962.061105941, 2912278.213634184), (4150960.066993618, 2912282.9742903346), (4150953.4223265625, 2912288.9952546423), (4150948.9370746408, 2912289.075990774), (4150939.6383606684, 2912296.850094448), (4150901.350143378, 2912335.6454385733), (4150887.3631428233, 2912349.9347463488), (4150885.6921141674, 2912353.3872276763), (4150882.2030219617, 2912351.705770334), (4150864.80628131, 2912368.1431638077), (4150862.6369521506, 2912369.880916823), (4150844.559921125, 2912390.419240323), (4150832.2180356467, 2912408.1793591026), (4150825.952906997, 2912417.278748944), (4150819.402987536, 2912441.838828801), (4150803.7523208978, 2912435.228888659), (4150794.581493822, 2912447.07842673), (4150791.4102146532, 2912449.0953099094), (4150793.5729996585, 2912457.49400123), (4150793.710229268, 2912463.175834843), (4150794.1897226907, 2912468.1669091955), (4150794.9960339908, 2912471.973263566), (4150796.2543448084, 2912476.0124657573), (4150797.96851521, 2912480.4080062876), (4150800.3403347745, 2912483.8555298815), (4150797.383219874, 2912491.428184365), (4150794.4634378753, 2912537.6905369153), (4150787.798087379, 2912527.5190776438), (4150787.6425558203, 2912561.3321983344), (4150787.554019751, 2912576.6012295657), (4150791.3682723753, 2912611.956843139), (4150790.1487217285, 2912625.963919687), (4150799.7474991316, 2912673.0641884664), (4150800.1040023617, 2912694.808755623), (4150815.0363981556, 2912748.290882647), (4150818.269969277, 2912770.2529710773), (4150825.3420218206, 2912790.8568377346), (4150839.8439475177, 2912817.6522862054), (4150860.9684209805, 2912872.6681851917), (4150883.0766351013, 2912922.9555821493), (4150899.191193042, 2912965.1515461626), (4150932.9963811813, 2913028.8501117956), (4151010.402445752, 2913112.733779705), (4151019.145619515, 2913123.457540774), (4151021.345420189, 2913121.40983634), (4151043.600398972, 2913137.637772014), (4151058.508714409, 2913147.7330921176), (4151065.1993621252, 2913152.279664246), (4151065.0418881164, 2913155.004154459), (4151066.4133196874, 2913158.7925970545), (4151069.33282995, 2913162.964512059), (4151075.269653565, 2913167.9676865265), (4151090.3163297484, 2913178.6149551217), (4151104.185945958, 2913187.8162881164), (4151112.5572916707, 2913193.1130265957), (4151118.3920756998, 2913197.439615253), (4151120.786360297, 2913199.0323609742), (4151134.863922043, 2913213.6043350883), (4151145.8145503295, 2913227.7193658194), (4151151.2668877356, 2913236.631814399), (4151156.9478535913, 2913242.508489695), (4151162.4421438533, 2913246.2896752614), (4151177.8270742157, 2913260.0167658), (4151188.687333496, 2913277.7195259016), (4151193.3933235845, 2913284.739703511), (4151203.8916072957, 2913296.0261040498), (4151209.350782024, 2913292.2062482457), (4151194.434977916, 2913274.0759181795), (4151185.2480078586, 2913260.7701006234), (4151169.803129028, 2913241.2350795134), (4151165.549078969, 2913235.74573661), (4151160.298844394, 2913229.4227320123), (4151153.5601111385, 2913222.03447278), (4151138.825008293, 2913208.410427556), (4151123.895624742, 2913195.0398161514), (4151115.785777421, 2913189.0548844854), (4151107.335589208, 2913183.822451283), (4151092.9179453924, 2913175.194774195), (4151081.2348864228, 2913167.407375995), (4151077.56168976, 2913164.989977141), (4151073.740348567, 2913161.711994641), (4151071.70177723, 2913159.8607440656), (4151069.2052830867, 2913154.9955190294), (4151073.295823495, 2913157.8323019724)),)}
b {'type': 'Polygon', 'coordinates': (((4151327.703666511, 2913613.982142541), (4151331.4359972086, 2913593.9709579535), (4151319.2770194965, 2913593.9709579535), (4151319.2770194965, 2913613.982142541), (4151327.703666511, 2913613.982142541)),)}

c = a.difference(b)
d = b.difference(a)
mapping(c)
[I can't write again the polygon due to body length limitation, but is exactly the same polygon than a]
mapping(d)
{'type': 'GeometryCollection', 'geometries': []}

Can anyone help me to understand what is going on? The behavior seems to describe case 1) and 3) at the same time, which I find impossible.

2

You must understand the differencespatial function: the difference of A and B returns the portion of the first geometry (A) that isn't intersected by the second geometry (B).

In your example, as b is within a, b.difference(a) is an empty geometry

 a.contains(b)
 True
 b.within(a)
 True

Demonstration with the with the Java Topology Suite (JTS) and a simpler example

A = POLYGON((60 350,430 350, 430 30, 60 30, 60 350)) -> rectangle
B = POLYGON((229 255, 156 184, 130 60, 230 110, 229 255))

A.difference(B)

enter image description here

B.difference(A) -> EMPTY POLYGON

enter image description here

Whith shapely

A = "POLYGON((60 350,430 350, 430 30, 60 30, 60 350))"
B = "POLYGON((229 255, 156 184, 130 60, 230 110, 229 255))"
from shapely.wkt import loads
A = loads(A)
B = loads(B)
A.contains(B)
True
A.difference(B).wkt
'POLYGON ((60 350, 430 350, 430 30, 60 30, 60 350), (229 255, 156 184, 130 60, 230 110, 229 255))'
B.difference(A).wkt
'GEOMETRYCOLLECTION EMPTY'
 mapping(A.difference(B))
 {'type': 'Polygon', 'coordinates': (((60.0, 350.0), (430.0, 350.0), (430.0, 30.0), (60.0, 30.0), (60.0, 350.0)), ((229.0, 255.0), (156.0, 184.0), (130.0, 60.0), (230.0, 110.0), (229.0, 255.0)))}
 mapping(B.difference(A))
 {'type': 'GeometryCollection', 'geometries': []}
| improve this answer | |
  • I was totally oblivious about this case, and it is quite obvious actually. Thank you! – ImanolUr Jul 14 '18 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.