1

I have written the Java code to read the shapefile and insert it into the table. But the way I have written code it will accept only a multistring .shp file. It fails for other shapefile geometry types, e.g. polygon.

@RequestMapping(value = "/file.htm", method = RequestMethod.POST)
    public String fileUploaded(Model model, @Validated File file,
            BindingResult result) throws IOException, SchemaException {
        System.out.println("File Name is given below");
        System.out.println("File Name is " +file.getFile().getOriginalFilename());
        String returnVal = "successFile";
        String fileLoc="D:\\SpringMVCFileUpload\\springexample\\src\\upload\\"+file.getFile().getOriginalFilename();
        java.io.File newFile = new java.io.File(fileLoc);

        // if the directory does not exist, create it
        if (!newFile.getParentFile().exists()) {
          newFile.getParentFile().mkdirs();  
        }
        FileCopyUtils.copy(file.getFile().getBytes(), newFile);

        DataStore fileDataStore=FileDataStoreFinder.getDataStore(newFile);
        String typeName=fileDataStore.getTypeNames()[0];

        SimpleFeatureCollection collection = fileDataStore.getFeatureSource(typeName).getFeatures();

        SimpleFeatureIterator iterator=collection.features();

        java.util.Map params = new java.util.HashMap();
        params.put( "dbtype", "Oracle");
        params.put( "host", "localhost");
        params.put( "port", 1521);
       // params.put( "schema", "PUBLIC");
        params.put( "database", "orcl");
        params.put( "user", "mdsys");
        params.put( "passwd", "mdsys");
        DataStore dataStore=DataStoreFinder.getDataStore(params);

        SimpleFeatureSource featureSource= dataStore.getFeatureSource("COAST_TEST");


        final SimpleFeatureType TYPE = DataUtilities.createType("COAST_TEST",
                "feature_id:Integer,GEOMETRY:MultiLineString,SCALERANK:Integer"
        );
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
        int i=0;
        ListFeatureCollection dbCollection=new ListFeatureCollection(TYPE);
        while(iterator.hasNext()){
            SimpleFeature feature=iterator.next();
            featureBuilder.add(i++);
            featureBuilder.add(feature.getAttribute("the_geom"));
            featureBuilder.add(feature.getAttribute("scalerank"));
            SimpleFeature dbFeature = featureBuilder.buildFeature(null);
            dbCollection.add(dbFeature);

        }

    Transaction transaction = new DefaultTransaction("create");
        if (featureSource instanceof SimpleFeatureStore) {
            SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
            featureStore.setTransaction(transaction);
            try {
                featureStore.addFeatures(dbCollection);
                transaction.commit();

            } catch (Exception problem) {
                problem.printStackTrace();
                transaction.rollback();

            } finally {
                transaction.close();
            }
        } else {
            System.out.println(typeName + " des not support read/write access");
            System.exit(1);
        }


        if (result.hasErrors()) {
        //  System.out.println("File Name is given below");
            returnVal = "file";
        } else {            
            MultipartFile multipartFile = file.getFile();   
        //  System.out.println("File Name is given below");
            returnVal = "successFile";
            }
        return returnVal;
    }

In the above code you will see that Simplefeaturetype object TYPE for table "COAST_TEST" geometry field accepts only multistring. How do I let that object have different type for geometry field based on the kind of shapefile I am uploading?

1 Answer 1

1

Usually you would ask the features to give you their schema using:

SimpleFeatureType schema = collection.getSchema();

You can then use that to generate the Oracle schema to write the data into.

2
  • Good answer by iant, but I just want to add a caveat: mixing geometry types in the same field is frowned upon. There are a lot of applications that have a hard time dealing with that.
    – Mintx
    Apr 13, 2015 at 20:40
  • I was assuming/hoping he would put each shapefile into a different table as there is no checking to make sure the attributes are the same
    – Ian Turton
    Apr 14, 2015 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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