1

I am trying to create a shape file from the the text file. To do this I used Geotools library. It can create the shapefile, but I got a warning about the coordinate system that is null, which I'm setting!

The data are in UTM-wgs84-Zone 30N. First, I tried default Coordinate System (WGS84), then I used EPSG and decoded it. It returns null.

public static void main(String[] args) throws Exception {
    File[] files = getFiles("C://ArtCSVFiles//");

    for (int i=0;i<files.length;i++) {
        String outputFilePath = "C://Art//" +files[i].getName()+".shp";
        //String inputFilePath = "C://ParkTxtFiles//ParkCluster0Mp10Dist0.005.csv";
        String inputFilePath = files[i].getAbsolutePath();
        final SimpleFeatureType TYPE = DataUtilities.createType("Location", "location:Point"); // see createFeatureType();

        FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection();
        BufferedReader reader = new BufferedReader(new FileReader(files[i]));
        try {

            GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);

            for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                String split[] = line.split("\\,");
                double longitude = Double.parseDouble(split[0]);
                double latitude = Double.parseDouble(split[1]);

                Point point = factory.createPoint(new Coordinate(longitude, latitude));
                SimpleFeature feature = SimpleFeatureBuilder.build(TYPE, new Object[]{point}, null);


                collection.add(feature);
            }
        } finally {
            reader.close();
        }
        File newFile = getNewShapeFile(files[i], outputFilePath);


        DataStoreFactorySpi factory = new ShapefileDataStoreFactory();

        Map<String, Serializable> create = new HashMap<String, Serializable>();
        create.put("url", newFile.toURI().toURL());
        create.put("create spatial index", Boolean.TRUE);

        ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
        newDataStore.createSchema(TYPE);
        newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);

        Transaction transaction = new DefaultTransaction("create");
        String typeName = newDataStore.getTypeNames()[0];
        FeatureStore<SimpleFeatureType, SimpleFeature> featureStore;
        featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>)
                newDataStore.getFeatureSource(typeName);

        featureStore.setTransaction(transaction);
        try {
            featureStore.addFeatures(collection);
            transaction.commit();
        } catch (Exception problem) {
            problem.printStackTrace();
            transaction.rollback();
        } finally {
            transaction.close();
        }
        //System.exit(0); // we are actually exiting because we will use a Swing JFileChooser
    }
}
public static File[] getFiles(String args) {
    return new File(args).listFiles();
}
private static File getNewShapeFile(File file, String outputFilePath) {
    String path = file.getAbsolutePath();
    String newPath = path.substring(0,path.length()-4)+".shp";

    File newFile = new File(outputFilePath);
    if( newFile.equals( file )){
        System.out.println("Cannot replace "+file);
        System.exit(0);
    }
    return newFile;
}


private static File getCSVFile(String[] args) throws FileNotFoundException {
    File file;
    if (args.length == 0){
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("Open CSV file");
        chooser.setFileFilter( new FileFilter(){
            public boolean accept( File f ) {
                return f.isDirectory() || f.getPath().endsWith("csv") || f.getPath().endsWith("CSV");
            }
            public String getDescription() {
                return "Comma Seperated Value";
            }
        });
        int returnVal = chooser.showOpenDialog( null );

        if(returnVal != JFileChooser.APPROVE_OPTION) {
            System.exit(0);
        }
        file = chooser.getSelectedFile();

        System.out.println("Opening CVS file: " + file.getName());
    }
    else {
        file = new File( args[0] );
    }
    if (!file.exists()){
        throw new FileNotFoundException( file.getAbsolutePath() );
    }
    return file;
}
/**
 * Here is how you can use a SimpleFeatureType build to create
 * the schema for your shapefile dynamically.
 * <p>
 * This method is an improvement on the origional example as we
 * are specifying DefaultGeographicCRS.WGS84 and a maximum field length.
 * <p>
 * @return SimpleFeatureType
 */
static SimpleFeatureType createFeatureType() throws FactoryException {

    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setName( "Location" );

    CoordinateReferenceSystem crs = CRS.decode("EPSG:32630");

    builder.setCRS(crs);

    //add attributes in order
    builder.add("Location", Point.class );
    builder.length(15).add( "Name", String.class );
    System.out.println(builder.crs(crs));

    //build the type
    final SimpleFeatureType LOCATION = builder.buildFeatureType();
    return LOCATION;
}
}
Nirmal
  • 1,229
  • 1
  • 15
  • 31

1 Answers1

0

Please change

final SimpleFeatureType TYPE = DataUtilities.createType("Location", "location:Point");

to

final SimpleFeatureType TYPE = DataUtilities.createFeatureType();

OR pls Change

final SimpleFeatureType TYPE = DataUtilities.createType("Location",   "location:Point");

to

final SimpleFeatureType TYPE = DataUtilities.createType("Location",
            "location:Point:srid=4326," + 
                    "name:String," + 
                    "number:Integer" 
    );

and delete the method CreateFeatureType()

Akalya Raj
  • 68
  • 1
  • 7