2015-04-14 2 views
1

J'essaie de créer un fichier de forme à partir du fichier texte. Pour ce faire, j'ai utilisé la bibliothèque Geotools. Il peut créer le fichier de formes, mais j'ai reçu un avertissement à propos du système de coordonnées null, que je suis en train de régler!Fichier PRJ non généré pour null CoordinateReferenceSystem

Les données sont dans UTM-wgs84-Zone 30N. J'ai d'abord essayé le système de coordonnées par défaut (WGS84), puis j'ai utilisé EPSG et l'ai décodé. Il renvoie 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; 
} 
} 
+0

Personne n'a de commentaires :( –

Répondre

0

S'il vous plaît changer

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

à

final SimpleFeatureType TYPE = DataUtilities.createFeatureType(); 

OU pls Changer

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

à

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

et supprimer la méthode CreateFeatureType()

+0

Maintenant, j'ai eu cette exception! Exception dans le fil "principal" java.lang.NoClassDefFoundError: sun/jdbc/odbc/ee/DataSource –

+0

Puis-je savoir quelle méthode vous avez suivie? –

+0

J'ai utilisé le second! –