jts icon indicating copy to clipboard operation
jts copied to clipboard

Polygon.normalize does not work with CoordinateXY

Open jodygarnett opened this issue 5 years ago • 3 comments

Key issue reported via @nmco geotools-devel

Another issues, the following test [3] is failing when invoking the normalize function [4] on polygon:

Caused by: java.lang.IllegalArgumentException: Invalid ordinate index: 2
    at org.locationtech.jts.geom.CoordinateXY.getOrdinate(CoordinateXY.java:77)
    at org.locationtech.jts.geom.impl.CoordinateArraySequence.getOrdinate(CoordinateArraySequence.java:260)
    at org.locationtech.jts.geom.CoordinateSequences.swap(CoordinateSequences.java:47)
    at org.locationtech.jts.geom.CoordinateSequences.reverse(CoordinateSequences.java:32)
    at org.locationtech.jts.geom.Polygon.normalize(Polygon.java:429)
    at org.locationtech.jts.geom.Polygon.normalized(Polygon.java:416)
    at org.locationtech.jts.geom.Polygon.normalize(Polygon.java:374)
    at it.geosolutions.jaiext.vectorbin.ROIGeometry.<init>(ROIGeometry.java:269)
    at it.geosolutions.jaiext.vectorbin.ROIGeometry.<init>(ROIGeometry.java:195)
    at it.geosolutions.jaiext.vectorbin.ROIGeometry.<init>(ROIGeometry.java:150)
    at org.geotools.coverage.grid.io.footprint.MultiLevelROIGeometry$FastClipROIGeometry.<init>(MultiLevelROIGeometry.java:162)
    at org.geotools.coverage.grid.io.footprint.MultiLevelROIGeometry.getTransformedROI(MultiLevelROIGeometry.java:128)
    at org.geotools.coverage.grid.io.footprint.MultiLevelROIGeometry.getTransformedROI(MultiLevelROIGeometry.java:45)
    at org.geotools.gce.imagemosaic.GranuleDescriptor.loadRaster(GranuleDescriptor.java:1348)
    at org.geotools.gce.imagemosaic.GranuleLoader.call(GranuleLoader.java:108)
    ... 44 more

[4] https://github.com/locationtech/jts/blob/master/modules/core/src/main/java/org/locationtech/jts/geom/Polygon.java#L373-L379

Other feedback addressed:

  • [x] PackedCoordianteSequence dimension and measure being final prevented LiteCoordinateSequence subclass from updating geometries in place.

  • [x] Code accessing PackedCoordianteSequence directly (not via factory) was broken by need to include both dimension and measure

    • The constructor PackedCoordinateSequence.Double(double[], int) is undefined
    • The method create(int, int) in the type PackedCoordinateSequenceFactory is not applicable for the arguments (double[], int)

jodygarnett avatar Aug 14 '18 23:08 jodygarnett

The method looks fine:

  public static void swap(CoordinateSequence seq, int i, int j)
  {
    if (i == j) return;
    for (int dim = 0; dim < seq.getDimension(); dim++) {
      double tmp = seq.getOrdinate(i, dim);
      seq.setOrdinate(i, dim, seq.getOrdinate(j, dim));
      seq.setOrdinate(j, dim, tmp);
    }
  }

For some reason @nmco is using CoordinateXY with a sequence of dimension > 2?

jodygarnett avatar Aug 15 '18 00:08 jodygarnett

Looking at the failure in GeoTools, was based on combining coordinates from two different sequences into an array.

Added defensive code:

  • Added an enforceArrayConsistency method to catch this during creation since I hope it will be an uncommon occurrence.
  • Made use of Coordinate.copy(), CoordinateSequence.createCoordinate(), and coordinate.setCoordinate( other ) as appropriate.

Found two issues:

  • setCoordinate( other ) needed to be implemented for coordinate subclasses individually
  • Coordinates dimension and measure methods are now based on checking the entire array

jodygarnett avatar Aug 15 '18 05:08 jodygarnett

We upgraded from geotools 13.0 to geotools 21.2 and we got this error when using org.geotools.data.wfs.WFSDataStore to download data from a WFS service.

java.lang.IllegalArgumentException: Invalid ordinate index: 2 at org.locationtech.jts.geom.CoordinateXY.getOrdinate(CoordinateXY.java:85) at org.locationtech.jts.geom.impl.PackedCoordinateSequence$Double.<init>(PackedCoordinateSequence.java:301) at org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory.create(PackedCoordinateSequenceFactory.java:81) at org.locationtech.jts.geom.GeometryFactory.createLinearRing(GeometryFactory.java:343) at org.locationtech.jts.geomgraph.EdgeRing.computeRing(EdgeRing.java:100) at org.locationtech.jts.geomgraph.EdgeRing.<init>(EdgeRing.java:54)

As a temporary fix to continue the upgrade we I used the following code

Polygon teigGeo=(Polygon) new WKTReader().read(teigGeoTmp.toText()); teigGeo.setSRID(srid.getSrid());

The reason why I found this solution was that I tried to make a test case that failed using the WKTReader with for a geom that failed, but that fixed the problem for a geom like the one below.

Geometry g2=new WKTReader().read("POLYGON ((10.72226277438168 59.67036728446327, 10.722252072723348 59.67036634715074, 10.72218290889186 59.6703472259182, 10.722116727557488 59.67031701800477, 10.722028819317611 59.67026131776931, 10.72195959595502 59.67019603125409, 10.721890288237375 59.670118710330286, 10.721780317810326 59.6699548005454, 10.721742597372993 59.66987365193037, 10.721723726872241 59.669793064271005, 10.721707939914292 59.669748362949115, 10.72163256279254 59.66969115074779, 10.721556892979502 59.66963861283094, 10.721522277174566 59.66959721235455, 10.721512466823086 59.669572102509306, 10.721515530744865 59.66951413032495, 10.721547407697406 59.66921129809674, 10.72154413354341 59.66895356619759, 10.721530670765373 59.6686460297252, 10.721517713182289 59.66847644557512, 10.721504945895074 59.668359042620885, 10.721495241082891 59.66831174658075, 10.721472653264076 59.66829713336544, 10.721437144432043 59.66829715024314, 10.72128252910943 59.66834274032207, 10.721040761571672 59.66840959263017, 10.720847416259952 59.6684651210759, 10.720615291977593 59.66854001943342, 10.720428344547464 59.66859222992108, 10.720056192406487 59.66871683605515, 10.719813901508374 59.66880129704618, 10.719702765245247 59.6688361658927, 10.719627369553463 59.668844339587736, 10.719568447257226 59.66883442372858, 10.719234703819312 59.6687383826062, 10.719103696935726 59.66869034083449, 10.718985800007841 59.668655599694645, 10.718897287939722 59.6686191261739, 10.718799129675322 59.66857110277482, 10.718638323330804 59.6684577049616, 10.718572401254137 59.66840467840549, 10.718494186049744 59.66844100110229, 10.718476805620128 59.668465658995856, 10.718476805620128 59.668465658995856, 10.718367763251413 59.66843044205165, 10.71835338222563 59.668430450792115, 10.717975478910388 59.668734221304675, 10.717799433902767 59.66885733042524, 10.717714122986848 59.66893892347313, 10.71771472462609 59.66895068161447, 10.717930702516764 59.66902500623065, 10.717934932690481 59.66904174660329, 10.71782273390777 59.6691808153964, 10.717783218249442 59.66925174930831, 10.717747790299336 59.669270715492914, 10.717438091779266 59.669269830364065, 10.717438091779266 59.669269830364065, 10.717416766042644 59.669227805775854, 10.717325139017664 59.66911655447683, 10.717192203667736 59.66903772884848, 10.717088960450184 59.668980519600275, 10.717088960450184 59.668980519600275, 10.717085229252742 59.66895964109429, 10.717184107559733 59.66881697499889, 10.717161539068842 59.66878188254262, 10.717116861861001 59.668741510821455, 10.717116861861001 59.668741510821455, 10.717445393075465 59.66854491212083, 10.717707859015915 59.668361931631196, 10.71794716620096 59.668180960391815, 10.718075075278044 59.66808874977353, 10.718074298981566 59.66807358087445, 10.71764622872522 59.66785141070558, 10.717588513187591 59.66781992210667, 10.717588513187591 59.66781992210667, 10.717549027188744 59.66779771540578, 10.717538756071757 59.66779129324556, 10.717538756071757 59.66779129324556, 10.717641228153267 59.667725912080826, 10.717641228153267 59.667725912080826, 10.717972140375862 59.66750323382572, 10.718130211330744 59.66742454974544, 10.71820462635715 59.667407677483446, 10.718349794120384 59.66739257291695, 10.71851636012747 59.66738275652009, 10.718934935882979 59.66736748234394, 10.719911500193746 59.667334293864585, 10.720517962067774 59.6673164613122, 10.72107178615238 59.66729698131395, 10.721754504507741 59.66727401095688, 10.722235470481 59.66725673971443, 10.722235470481 59.66725673971443, 10.722262032760653 59.66726923482472, 10.72242743507731 59.667513611026145, 10.722584620542797 59.66776043017248, 10.722648833697404 59.667873475002615, 10.722667467683422 59.667887151951476, 10.722688063011352 59.66788715094719, 10.722737696659056 59.6678786850657, 10.722851478582887 59.66785051512289, 10.72287815962046 59.66784109318798, 10.722872024791114 59.66769387375863, 10.722859574034596 59.667551587397966, 10.7228431138829 59.66732770996703, 10.72284681908272 59.66723730532407, 10.72284681908272 59.66723730532407, 10.723046406671097 59.667227498553736, 10.723343318816637 59.66721829910799, 10.723728815623533 59.66720200708095, 10.724020934509886 59.667182450297005, 10.724285660662122 59.66715498974706, 10.724423261935273 59.667130906352305, 10.724734417852948 59.66706376409463, 10.72500418630394 59.666975519404225, 10.72513365633257 59.66693142313888, 10.72513365633257 59.66693142313888, 10.72516206899718 59.66692808551922, 10.725182661574017 59.666931497205645, 10.725195618564952 59.66694524846087, 10.725211210751334 59.666993005753355, 10.725238846813475 59.667088566999155, 10.725288112644042 59.66718698736652, 10.72539944069013 59.66734261331038, 10.725455295829748 59.66741031930719, 10.725503334559585 59.66746411662163, 10.7255660056843 59.66752625407377, 10.725631052375206 59.66758279158549, 10.725792221474459 59.66769940965198, 10.725825742801495 59.667726362880025, 10.725856918647485 59.66775604147177, 10.725883815785217 59.66778883013159, 10.725904461530874 59.667820892678584, 10.725925658967306 59.66786713907271, 10.725936157108778 59.6679021194546, 10.725944634361062 59.66793577916701, 10.7259533047544 59.66799736946893, 10.725970326301953 59.66810052504796, 10.725980342185665 59.6681917373012, 10.725995034752284 59.66830139035219, 10.726006376009007 59.668363125142605, 10.726006376009007 59.668363125142605, 10.725958934427366 59.668414226560955, 10.725918462299502 59.668452392417066, 10.725880599417023 59.66846878818557, 10.725842809884114 59.668479704132814, 10.725791777264503 59.66848513580945, 10.725670371724744 59.668489337598, 10.725562420633317 59.6684893205278, 10.725457481595695 59.66849608985288, 10.725360388808525 59.66850697730326, 10.725328138429788 59.66851512573465, 10.725301107640089 59.66853155850354, 10.725190577078834 59.66866791743363, 10.725098311260881 59.66879002460864, 10.725098311260881 59.66879002460864, 10.725038580740534 59.668888800525075, 10.724925827671903 59.66911671206053, 10.72485786616004 59.669445797333886, 10.724669891052127 59.66975445518258, 10.724567561653366 59.66995726015047, 10.724472809515706 59.670131313770625, 10.724427607988881 59.670198462392335, 10.724352243849113 59.67026259440946, 10.724245786967316 59.6703816540054, 10.724087839411203 59.670507677399634, 10.723961551613746 59.67067280406281, 10.723915597741636 59.67082214498408, 10.723902105602612 59.67087405690351, 10.72388309886913 59.67092577183301, 10.723861665343097 59.67095443566384, 10.723745521229436 59.67107856203689, 10.72368371563877 59.67116128724799, 10.723664911216183 59.67123078325704, 10.72368107022411 59.67127574892554, 10.723767295112195 59.67144724438628, 10.72381311082044 59.67148535354127, 10.72390966077774 59.67161584648934, 10.723925258151164 59.67167752548086, 10.723905530121185 59.67178089468455, 10.723909826917142 59.67192679092361, 10.72387218343597 59.67204782003486, 10.72384372297999 59.67212292177838, 10.723809175831184 59.672145201253066, 10.723809175831184 59.672145201253066, 10.723682762847197 59.67214551492835, 10.723500566301237 59.672138387975416, 10.722957399175774 59.672124864552465, 10.722700452727999 59.672128238757125, 10.72265916734255 59.67211611666828, 10.722657375429057 59.6720812013787, 10.722661069812498 59.67207360823984, 10.722777387977326 59.67201154418282, 10.72288129132193 59.67194631982533, 10.722934623699937 59.671892178304674, 10.722966026112188 59.67183976184219, 10.72295365155062 59.67180624297329, 10.722912648700538 59.671744268807345, 10.722824674269319 59.671642403991925, 10.722733317851835 59.671523069211254, 10.722635846571736 59.67146102810595, 10.722563364510414 59.671429196609125, 10.72231816266703 59.67136720874968, 10.722289686927711 59.67134863119276, 10.7222865511816 59.6713325052961, 10.722289842742656 59.67131360051122, 10.722337048235792 59.67124040863751, 10.722387435876943 59.67117040837913, 10.722406216050729 59.67107962625958, 10.722415507833215 59.67094926958545, 10.722427889917553 59.670879139557925, 10.722427889917553 59.670879139557925, 10.7224565922833 59.67080178952488, 10.722572934992398 59.670712600538224, 10.722679855871117 59.670647426492934, 10.722746010394042 59.67060443359974, 10.722783566724535 59.67056469037116, 10.722799478119796 59.67050107059363, 10.722768058781785 59.67044588644688, 10.722726992527402 59.670417235181766, 10.72267041254769 59.67040459541708, 10.722573039595611 59.67039976644381, 10.72226277438168 59.67036728446327), (10.717629471862017 59.66855453753879, 10.717630238365283 59.6685591081669, 10.717827918058425 59.66862981038496, 10.717838209067505 59.668629675626825, 10.717863241349882 59.66861228261232, 10.7182707245174 59.668307136990016, 10.718274292599897 59.66830053361652, 10.718262081511522 59.66829090351035, 10.71810783571155 59.66820849580597, 10.718091076426465 59.668210601456416, 10.717629471862017 59.66855453753879), (10.723336698437638 59.669621082681424, 10.723355119517736 59.66962021197292, 10.723358585490221 59.66959780199344, 10.723381680337212 59.6695357942808, 10.72339940807037 59.6694972095073, 10.723399373654107 59.66947924654664, 10.723369249002358 59.66946311595018, 10.72332320862198 59.66941018979357, 10.723436732906432 59.66932849180238, 10.723449185806905 59.66929437735285, 10.723422485511863 59.669282692635655, 10.723314368151382 59.66928986124271, 10.723287605825265 59.66926659091199, 10.723300019651267 59.66923517150273, 10.72335517654757 59.669202831327276, 10.723387015880377 59.669162444464334, 10.723530808915658 59.66905475087111, 10.723594710380215 59.66895412454264, 10.723647921746204 59.66887699107646, 10.723665667282205 59.66882493346208, 10.723665605089288 59.66879260013092, 10.72359464405379 59.66877646723698, 10.723481062567036 59.6687809234581, 10.72338700754127 59.66880605043967, 10.7233319620522 59.668826712953425, 10.723232698826415 59.66886798549538, 10.723106630475414 59.66884018145368, 10.723074759993175 59.668817696714044, 10.723039154410937 59.668788165477345, 10.723003770016565 59.668762942545236, 10.72292748180914 59.6687467894443, 10.722886573313996 59.668755769516075, 10.722822731577052 59.66879531910267, 10.722751824080733 59.66885633790559, 10.722696820543321 59.66892280629579, 10.722652406200085 59.669029553283615, 10.722652414560358 59.669057396460076, 10.722670195860868 59.669095963932385, 10.72270569469375 59.669230672437706, 10.722741165525694 59.66930601223045, 10.722801410093357 59.66931052029115, 10.722849441734585 59.66931231456933, 10.723097780301833 59.66932845343245, 10.723179418135873 59.66935540404053, 10.723229083072619 59.6693993907454, 10.723261084973474 59.669469386950496, 10.723273465725645 59.66951341431145, 10.72327880454366 59.66954478015184, 10.723314313939788 59.66961392191652, 10.723336698437638 59.669621082681424), (10.722903645650502 59.670880437032345, 10.723178559121992 59.6709055678054, 10.723404662443356 59.67092415385237, 10.723428972106154 59.67092383450282, 10.723448933299505 59.67091836288583, 10.723462603363453 59.67090794415828, 10.723536696672522 59.67073254605508, 10.723523418590748 59.67070909861331, 10.723498321039562 59.670694069616836, 10.723463372021696 59.67068428960811, 10.723073662170766 59.67067809175818, 10.722934787268336 59.67069841800227, 10.722859670962094 59.670736319293674, 10.722825854295367 59.67079011470645, 10.722827696965007 59.67082601732168, 10.722843260547869 59.67085248859484, 10.722866355384959 59.670866556028265, 10.722889238326164 59.670876494656895, 10.722903645650502 59.670880437032345))");

larsop avatar Nov 06 '19 03:11 larsop