Trying to convert kml to geojson and getting ArrayIndexOutOfBoundsException with geotools

回眸只為那壹抹淺笑 提交于 2019-12-13 04:16:54

问题


I am trying to convert a kml to geojson with geotools 20.5 and i am getting ArrayIndexOutOfBoundsException. The file i am trying to convert can be converted on other frameworks correctly but on geotools i had this error. I am using geotools 20.5 and java 8. Can someone help me?

My code:

try {
            reader = new FileInputStream("file.kml");
            PullParser parser = new PullParser(new KMLConfiguration(), reader,
                    SimpleFeature.class);

            FeatureJSON fjson = new FeatureJSON();
            FileWriter tmp = new FileWriter("file.geojson");
            BufferedWriter writer = new BufferedWriter(tmp);
            ArrayList<SimpleFeature> features = new ArrayList<>();
            SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
            while (simpleFeature != null) {
                features.add(simpleFeature);
                simpleFeature = (SimpleFeature) parser.parse();
            }
            SimpleFeatureCollection fc = DataUtilities.collection(features);
            fjson.writeFeatureCollection(fc, writer);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

The Error:

nov 05, 2019 10:34:58 AM org.geotools.feature.DefaultFeatureCollection add
ADVERTÊNCIA: Feature Collection contains a heterogeneous mix of features
nov 05, 2019 10:34:58 AM org.geotools.feature.DefaultFeatureCollection add
ADVERTÊNCIA: Feature Collection contains a heterogeneous mix of features
java.lang.ArrayIndexOutOfBoundsException: 12
    at org.geotools.feature.simple.SimpleFeatureImpl.getAttribute(SimpleFeatureImpl.java:179)
    at org.geotools.geojson.feature.FeatureJSON$FeatureEncoder.toJSONString(FeatureJSON.java:603)
    at org.geotools.geojson.feature.FeatureJSON$FeatureCollectionEncoder.writeJSONString(FeatureJSON.java:663)
    at org.json.simple.JSONValue.writeJSONString(Unknown Source)
    at org.json.simple.JSONObject.writeJSONString(Unknown Source)
    at org.geotools.geojson.GeoJSONUtil.encode(GeoJSONUtil.java:276)
    at org.geotools.geojson.feature.FeatureJSON.writeFeatureCollection(FeatureJSON.java:311)
    at br.gov.serpro.dnit.sgpd.managedbean.util.KmlUtil.main(KmlUtil.java:65)

my file:

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Schema name="FILEP" id="FILEP">
    <SimpleField name="descriptio" type="string"></SimpleField>
    <SimpleField name="timestamp" type="string"></SimpleField>
    <SimpleField name="begin" type="string"></SimpleField>
    <SimpleField name="end" type="string"></SimpleField>
    <SimpleField name="altitudeMo" type="string"></SimpleField>
    <SimpleField name="tessellate" type="string"></SimpleField>
    <SimpleField name="extrude" type="string"></SimpleField>
    <SimpleField name="visibility" type="string"></SimpleField>
    <SimpleField name="drawOrder" type="string"></SimpleField>
    <SimpleField name="icon" type="string"></SimpleField>
    <SimpleField name="Vertice" type="float"></SimpleField>
    <SimpleField name="N" type="float"></SimpleField>
    <SimpleField name="E" type="float"></SimpleField>
    <SimpleField name="Name_2" type="string"></SimpleField>
    <SimpleField name="descript_2" type="string"></SimpleField>
    <SimpleField name="timestam_2" type="string"></SimpleField>
    <SimpleField name="begin_2" type="string"></SimpleField>
    <SimpleField name="end_2" type="string"></SimpleField>
    <SimpleField name="altitude_2" type="string"></SimpleField>
    <SimpleField name="tessella_2" type="string"></SimpleField>
    <SimpleField name="extrude_2" type="string"></SimpleField>
    <SimpleField name="visibili_2" type="string"></SimpleField>
    <SimpleField name="drawOrde_2" type="string"></SimpleField>
    <SimpleField name="icon_2" type="string"></SimpleField>
    <SimpleField name="Vertice_2" type="float"></SimpleField>
    <SimpleField name="N_2" type="float"></SimpleField>
    <SimpleField name="E_2" type="float"></SimpleField>
</Schema>
<Folder><name>FILEP</name>
  <Placemark>
    <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
    <ExtendedData><SchemaData schemaUrl="#FILEP">
        <SimpleData name="tessellate">-1</SimpleData>
        <SimpleData name="extrude">0</SimpleData>
        <SimpleData name="visibility">-1</SimpleData>
        <SimpleData name="Vertice">99.000000000000000</SimpleData>
        <SimpleData name="N">8183148.594999999700000</SimpleData>
        <SimpleData name="E">745077.081499999970000</SimpleData>
        <SimpleData name="tessella_2">-1</SimpleData>
        <SimpleData name="extrude_2">0</SimpleData>
        <SimpleData name="visibili_2">-1</SimpleData>
        <SimpleData name="Vertice_2">191.000000000000000</SimpleData>
        <SimpleData name="N_2">8210502.734000000200000</SimpleData>
        <SimpleData name="E_2">736058.699299999980000</SimpleData>
    </SchemaData></ExtendedData>
      <Polygon><altitudeMode>relativeToGround</altitudeMode><outerBoundaryIs><LinearRing><altitudeMode>relativeToGround</altitudeMode><coordinates>-54.7917363536269,-16.1742594701822 -54.7895814786832,-16.1734821399515 -54.7895800001009,-16.1734859993162 -54.7917363536269,-16.1742594701822</coordinates></LinearRing></outerBoundaryIs></Polygon>
  </Placemark>
  <Placemark>
    <name>Lote 1.1</name>
    <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
    <ExtendedData><SchemaData schemaUrl="#FILEP">
        <SimpleData name="tessellate">-1</SimpleData>
        <SimpleData name="extrude">0</SimpleData>
        <SimpleData name="visibility">-1</SimpleData>
        <SimpleData name="Vertice">99.000000000000000</SimpleData>
        <SimpleData name="N">8183148.594999999700000</SimpleData>
        <SimpleData name="E">745077.081499999970000</SimpleData>
    </SchemaData></ExtendedData>
      <Polygon><altitudeMode>relativeToGround</altitudeMode><outerBoundaryIs><LinearRing><altitudeMode>relativeToGround</altitudeMode><coordinates>-54.7029330000828,-16.418827003687 -54.7050349995717,-16.4206240055189 -54.7066909997516,-16.4186669998587 -54.7097899998139,-16.4152840050737 -54.711284000096,-16.4137840012491 -54.7155259996671,-16.4091309999561 -54.7159569996756,-16.4085860056906 -54.7164260000864,-16.4078180037582 -54.7167560002403,-16.4070890068105 -54.7170000000898,-16.4063140036972 -54.71716799977,-16.4053310064691 -54.7171859996123,-16.4043040003729 -54.7142569996992,-16.3849039995536 -54.714219000075,-16.3831400057174 -54.7143560002966,-16.3824300040174 -54.7146070001402,-16.3817500050653 -54.71507299986,-16.3809680075699 -54.7166420002163,-16.3791200011844 -54.7294440001037,-16.3653540023959 -54.7303290003941,-16.3640530055761 -54.735744,-16.354199003456 -54.7362829999685,-16.3533360017841 -54.7370200002648,-16.3524110008306 -54.7434739998639,-16.345426002351 -54.7443780000589,-16.3442420020091 -54.7452380003799,-16.3428140000797 -54.7458840000393,-16.3414490038868 -54.7474169999057,-16.3376550078664 -54.7585620001911,-16.3081250034321 -54.7593819998013,-16.3057130032081 -54.7608040000599,-16.3021880024424 -54.7683409995231,-16.2821870054936 -54.7697669996656,-16.2788180019092 -54.7760910000798,-16.2658120055877 -54.777188000237,-16.2633620029799 -54.779149000021,-16.2595250017392 -54.7795659996092,-16.2585190004681 -54.7799890001377,-16.2568250072184 -54.7834109998234,-16.2351520022125 -54.7835120001112,-16.2338919997834 -54.7834749995869,-16.2329900000154 -54.7833099997024,-16.2319180067533 -54.7808520003183,-16.2222720071977 -54.7798469999294,-16.2187010033988 -54.7796859996364,-16.2177010019671 -54.7796989996353,-16.2169960072611 -54.7812380003992,-16.2104790041366 -54.7817089997954,-16.207916001936 -54.7855579997548,-16.1919780052685 -54.7859999995245,-16.1905900033254 -54.7921979998959,-16.1744260003105 -54.7917363536269,-16.1742594701822 -54.7895800001009,-16.1734859993162 -54.7895814786832,-16.1734821399515 -54.7895699999615,-16.1734779992206 -54.783170000137,-16.1901650015924 -54.7826590000205,-16.191985000899 -54.7795909999637,-16.2046900069761 -54.7788599999872,-16.2078260046823 -54.7784839999407,-16.2099620009427 -54.7769399996402,-16.2164780005795 -54.7768780002604,-16.2177400055293 -54.7770959995268,-16.2192370022489 -54.7780970002354,-16.2227990005203 -54.7805239996236,-16.2323020024959 -54.7807069998312,-16.2337339998721 -54.7806530003063,-16.2346350009768 -54.7796580001503,-16.2410680053836 -54.7772139998481,-16.2564260020182 -54.7769069997749,-16.2576530053378 -54.7759169995376,-16.2597970009449 -54.7743950003939,-16.2627320054949 -54.7735329996322,-16.2646990079405 -54.7672499999347,-16.277618002331 -54.7658179997151,-16.2809599995937 -54.758144000203,-16.3013210010215 -54.7571670001441,-16.303667006542 -54.7559009997175,-16.3072620028212 -54.7447920002151,-16.3366950055191 -54.7432859997169,-16.3404240038616 -54.742708999522,-16.3416370066335 -54.7420549997786,-16.3427210051719 -54.7412709997459,-16.3437460057347 -54.7349009995287,-16.3506340073675 -54.7339589999036,-16.3518160028645 -54.733309000119,-16.3528480065132 -54.7279190004019,-16.3626599997099 -54.7272100000687,-16.3637130028044 -54.7145129997873,-16.3773530021394 -54.7128520003898,-16.3793080023477 -54.7123450001003,-16.3800669997029 -54.7120129994799,-16.3807140075095 -54.7116419998058,-16.3817360064993 -54.7114329998469,-16.3828000045365 -54.7113769994761,-16.3836630077601 -54.7114639999648,-16.385185999841 -54.7143010000668,-16.4039050008073 -54.7143900001083,-16.4047160012712 -54.714286000192,-16.4056130011044 -54.7139429998633,-16.4065520004201 -54.713322000095,-16.4074510052629 -54.7045899997694,-16.4168690051387 -54.7029330000828,-16.418827003687</coordinates></LinearRing></outerBoundaryIs></Polygon>
  </Placemark>
  <Placemark>
    <name>Lote 1.2</name>
    <Style><LineStyle><color>ff0000ff</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
    <ExtendedData><SchemaData schemaUrl="#FILEP">
        <SimpleData name="tessella_2">-1</SimpleData>
        <SimpleData name="extrude_2">0</SimpleData>
        <SimpleData name="visibili_2">-1</SimpleData>
        <SimpleData name="Vertice_2">191.000000000000000</SimpleData>
        <SimpleData name="N_2">8210502.734000000200000</SimpleData>
        <SimpleData name="E_2">736058.699299999980000</SimpleData>
    </SchemaData></ExtendedData>
      <Polygon><altitudeMode>relativeToGround</altitudeMode><outerBoundaryIs><LinearRing><altitudeMode>relativeToGround</altitudeMode><coordinates>-54.7917363536269,-16.1742594701822 -54.7922089999336,-16.1744290055315 -54.7938209996617,-16.1701900053446 -54.7940470003131,-16.1693140075326 -54.7941340003908,-16.1685960014647 -54.7941199996626,-16.1676940072815 -54.7939350003224,-16.1666630066868 -54.7925110002514,-16.1622500063043 -54.7925040003942,-16.1617810034919 -54.7925990000144,-16.1614320074617 -54.7930269994926,-16.1608480052903 -54.7937059998702,-16.1604610002311 -54.7973809996163,-16.1589220019762 -54.7982700001723,-16.158481002857 -54.8022849996873,-16.1558349992484 -54.8035560001701,-16.1550730046464 -54.8332190002705,-16.1352210001957 -54.8363979998726,-16.1329980068579 -54.8380640000272,-16.1319970063641 -54.8464969996475,-16.1263600076539 -54.8484350002558,-16.1254090021821 -54.8492340003468,-16.1249420055654 -54.8570829999307,-16.1197110008907 -54.8580929996083,-16.1189100034289 -54.8589340002961,-16.1179430043422 -54.85972399999,-16.1165230014504 -54.8662159998127,-16.1006880035181 -54.8666899998138,-16.0992420076199 -54.867387999647,-16.0974970010668 -54.8689899996549,-16.0938380071676 -54.8717319996195,-16.0869240066956 -54.873719999521,-16.0822280066624 -54.8747899998844,-16.079422005047 -54.8753869997046,-16.0774110066516 -54.8757060002378,-16.0759370074174 -54.8759,-16.0741260051309 -54.8759159998384,-16.0723200021113 -54.8757810003674,-16.0707000042784 -54.8755039994578,-16.0690960071385 -54.8750910001775,-16.0675200054315 -54.8731150001449,-16.0622580058197 -54.8730270000878,-16.0611799999066 -54.8731909998119,-16.0602930048823 -54.8733900000749,-16.0597860010721 -54.873757000174,-16.0591580055416 -54.8814580000444,-16.0483999998769 -54.8821729998501,-16.0472170015211 -54.8827150000317,-16.0457300025457 -54.8842359996216,-16.0385130012909 -54.8845860000154,-16.0375840038308 -54.8885070001226,-16.0318600014585 -54.8889900001126,-16.0313100078803 -54.8899300003221,-16.0307230027508 -54.8978859999417,-16.0264379999814 -54.8991510001851,-16.0255870077106 -54.9026539997779,-16.0223329993815 -54.9040479997293,-16.0211300014127 -54.9116059999491,-16.0140509997868 -54.9147340001337,-16.0110170020562 -54.9153340002629,-16.0103260001817 -54.9158880003838,-16.0093850036588 -54.9161809995596,-16.0085290061173 -54.9177380003519,-16.0010910002096 -54.9181470001722,-16.0000830001473 -54.9187279997894,-15.9991700001243 -54.92523499991,-15.992183000068 -54.9270239998885,-15.9898680010555 -54.9284549998288,-15.9882140031562 -54.9329190002499,-15.9826310042419 -54.9338029999333,-15.9813890003924 -54.9353190001611,-15.9794860026873 -54.9370679997235,-15.9775199990725 -54.9414250003811,-15.9720110062388 -54.9419960001669,-15.970875001874 -54.9422859996544,-15.9696560010253 -54.9430359996107,-15.9693570053339 -54.9436990002224,-15.9692150069177 -54.9461180000472,-15.9692400070883 -54.9461319997248,-15.9689280011463 -54.9446259998902,-15.9688420064682 -54.9441319996873,-15.96868100092 -54.9435749996239,-15.9683400021668 -54.9430159999523,-15.9678280049986 -54.9429949998482,-15.967699003753 -54.9431099998527,-15.9675130056331 -54.9430280001149,-15.9670160032897 -54.9427140002326,-15.9664270069603 -54.9424819996609,-15.966486004333 -54.9422089999057,-15.966202002875 -54.9421579997093,-15.9629520022339 -54.9423149994915,-15.9626270066505 -54.942588999661,-15.9623849995062 -54.9429989996229,-15.9622560060469 -54.943641000118,-15.9622500052059 -54.9436739997101,-15.9595390016343 -54.9426650000815,-15.9595640042039 -54.9417190001236,-15.9597900018166 -54.9408989995735,-15.9602180041673 -54.9400980002731,-15.9609700064163 -54.9396289999933,-15.9617480063748 -54.9393789995464,-15.9625820068765 -54.9393249998396,-15.9632910075316 -54.9393810001848,-15.9649400067881 -54.9390790002001,-15.9649940024099 -54.9385820000376,-15.9649260038096 -54.9373350001855,-15.9642440069293 -54.937143000049,-15.9645160038586 -54.9382380003222,-15.9653269996369 -54.9383609999695,-15.9655400047613 -54.9385350002078,-15.9662660066783 -54.9394800002914,-15.9676930069366 -54.9394820003079,-15.9694900043374 -54.9392689997784,-15.9701800071804 -54.938962999916,-15.9706970026846 -54.9347969996021,-15.9759280032372 -54.9330899998383,-15.9778430069674 -54.9315250003736,-15.9798089996532 -54.9306500002115,-15.9810389997913 -54.926256999649,-15.9865330033011 -54.9247730003493,-15.9882520078848 -54.9231989996791,-15.9903090003603 -54.9198549998185,-15.993961007981 -54.9171739998167,-15.9967350012632 -54.9165219995898,-15.997497006295 -54.9159489997194,-15.9983230042391 -54.9153659999944,-15.9994540072115 -54.9150069997949,-16.0004720001409 -54.9134840002342,-16.007782005397 -54.9132590002368,-16.0084260045417 -54.9129520000097,-16.0088780014516 -54.909653000019,-16.0121070070789 -54.9020910003464,-16.0191900042551 -54.9007810002864,-16.0203160004128 -54.89727700017,-16.0235679996742 -54.8963449995521,-16.0241689999587 -54.8881849999514,-16.0285670064647 -54.8871169995592,-16.0292930051009 -54.8861510000528,-16.0303910038224 -54.8821339996596,-16.0362700022902 -54.8818149994902,-16.0369230074527 -54.8815420001116,-16.037747004198 -54.8800190003427,-16.0449770035663 -54.8796360003403,-16.0460650055883 -54.879100999669,-16.0469310002938 -54.8714400002761,-16.0576320029156 -54.8709329997891,-16.0584710067496 -54.8705179999957,-16.0594770060334 -54.8703050001203,-16.0603560022655 -54.8702259996582,-16.0610740077299 -54.8702410001438,-16.0619770010153 -54.870345000364,-16.0626920023187 -54.8706159995148,-16.063652005651 -54.8724080001386,-16.0683060045964 -54.8727719999225,-16.0697070012208 -54.873010999579,-16.071134004702 -54.8731199996537,-16.072575003067 -54.8730979999925,-16.0740200025374 -54.8729550000146,-16.0753989993192 -54.8726819996942,-16.0766960017518 -54.8721819997228,-16.0784169998499 -54.8710159999692,-16.0814700047165 -54.8692869995768,-16.0855260031509 -54.8663839996935,-16.0928380032664 -54.8648179995951,-16.0964090072594 -54.8640360000779,-16.0983650067959 -54.863602999897,-16.0997060030276 -54.857052000037,-16.1156579997902 -54.8566540002702,-16.1163640040636 -54.856183000342,-16.1169240013657 -54.8554629995512,-16.1174990048953 -54.8482029996459,-16.1223420035287 -54.8471339997851,-16.1230080035295 -54.8450510002112,-16.1240360023776 -54.8365010003375,-16.1297450005548 -54.8349329995611,-16.1306830007023 -54.831599999663,-16.1330080016845 -54.8020919996387,-16.1527580010607 -54.8005209996253,-16.1537150011526 -54.7972820003853,-16.1558800050084 -54.7963080003236,-16.1564160056591 -54.7922299995441,-16.1581400046299 -54.7911179997587,-16.1588620057383 -54.7904979996332,-16.159536001425 -54.7900399999569,-16.1603210036527 -54.7897649997954,-16.1611829996721 -54.7896849997155,-16.1620810077988 -54.7898909999428,-16.1633270059747 -54.7912170002848,-16.1673310000546 -54.7913320002567,-16.1679950049167 -54.7913269997345,-16.1685360055641 -54.7910729997534,-16.1695890027316 -54.7895814786832,-16.1734821399515 -54.7917363536269,-16.1742594701822</coordinates></LinearRing></outerBoundaryIs></Polygon>
  </Placemark>
</Folder>
</Document></kml>

my pom:

<properties>
        <geotools.version>20.5</geotools.version>
    </properties>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-main</artifactId>
            <version>${geotools.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.jdom</groupId>
                    <artifactId>jdom</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>xml-apis</groupId>
                    <artifactId>xml-apis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.geotools.xsd</groupId>
            <artifactId>gt-xsd-core</artifactId>
            <version>${geotools.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>xml-apis</groupId>
                    <artifactId>xml-apis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-geojson</artifactId>
            <version>${geotools.version}</version>
        </dependency>
        <dependency>
            <groupId>org.geotools.xsd</groupId>
            <artifactId>gt-xsd-kml</artifactId>
            <version>${geotools.version}</version>
        </dependency>       
        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-xml</artifactId>
            <version>${geotools.version}</version>
        </dependency>

        <dependency>
            <groupId>org.geotools</groupId>
            <artifactId>gt-api</artifactId>
            <version>${geotools.version}</version>
        </dependency>

回答1:


The clue is in the error message "Feature Collection contains a heterogeneous mix of features". The GeoJSON writer creates the schema based on the first feature and assumes that the rest are the same.

you need to use setFeatureType to tell it what the "real" schema looks like.




回答2:


I had to attach some parts manually to geojson. this way worked for me:

private static StringWriter kmlToGeojson(InputStream input) throws XMLStreamException, IOException, SAXException {
        PullParser parser = new PullParser(new KMLConfiguration(), input, SimpleFeature.class);
        FeatureJSON fjson = new FeatureJSON();
        StringWriter sw = new StringWriter();
        ArrayList<SimpleFeature> features = new ArrayList<>();
        SimpleFeature simpleFeature = (SimpleFeature) parser.parse();
        sw.append("{    \"type\": \"FeatureCollection\",    \"features\": [");
        while (simpleFeature != null) {
            SimpleFeatureType featureType = simpleFeature.getFeatureType();
            fjson.setFeatureType(featureType);
            fjson.writeFeature(simpleFeature, sw);
            simpleFeature = (SimpleFeature) parser.parse();
            if (simpleFeature != null) {
                sw.append(",");
            }
        }
        sw.append("]}");
        sw.close();
        return sw;
    }


来源:https://stackoverflow.com/questions/58711737/trying-to-convert-kml-to-geojson-and-getting-arrayindexoutofboundsexception-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!