当前位置: 首页>>代码示例>>Java>>正文


Java ShapefileDataStore.forceSchemaCRS方法代码示例

本文整理汇总了Java中org.geotools.data.shapefile.ShapefileDataStore.forceSchemaCRS方法的典型用法代码示例。如果您正苦于以下问题:Java ShapefileDataStore.forceSchemaCRS方法的具体用法?Java ShapefileDataStore.forceSchemaCRS怎么用?Java ShapefileDataStore.forceSchemaCRS使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.geotools.data.shapefile.ShapefileDataStore的用法示例。


在下文中一共展示了ShapefileDataStore.forceSchemaCRS方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: collectionToShapeFile

import org.geotools.data.shapefile.ShapefileDataStore; //导入方法依赖的package包/类
/**
 * <p>
 * The easy way to create a shapefile from attributes and geometries
 * </p>
 * <p>
 * <b>NOTE: this doesn't support date attributes</b>
 * </p>
 * 
 * @param shapeFilePath the shapefile name
 * @param crs the destination crs
 * @param fet the featurecollection
 * @throws IOException 
 */
public static boolean collectionToShapeFile( String shapeFilePath, CoordinateReferenceSystem crs,
        SimpleFeatureCollection fet ) throws IOException {

    // Create the file you want to write to
    File file = null;
    if (shapeFilePath.toLowerCase().endsWith(".shp")) { //$NON-NLS-1$
        file = new File(shapeFilePath);
    } else {
        file = new File(shapeFilePath + ".shp"); //$NON-NLS-1$
    }

    ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();

    Map<String, Serializable> create = new HashMap<String, Serializable>();
    create.put("url", file.toURI().toURL());
    ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);

    newDataStore.createSchema(fet.getSchema());
    if (crs != null)
        newDataStore.forceSchemaCRS(crs);
    Transaction transaction = new DefaultTransaction();
    SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource();
    featureStore.setTransaction(transaction);
    try {
        featureStore.addFeatures(fet);
        transaction.commit();
    } catch (Exception problem) {
        problem.printStackTrace();
        transaction.rollback();
    } finally {
        transaction.close();
    }
    return true;
}
 
开发者ID:TheHortonMachine,项目名称:hortonmachine,代码行数:48,代码来源:FeatureUtilities.java

示例2: createShapeFileDatastore

import org.geotools.data.shapefile.ShapefileDataStore; //导入方法依赖的package包/类
/**
 * @param name the shapefile name
 * @param fieldsSpec to create other fields you can use a string like : <br>
 *        "geom:MultiLineString,FieldName:java.lang.Integer" <br>
 *        field name can not be over 10 characters use a ',' between each field <br>
 *        field types can be : java.lang.Integer, java.lang.Long, // java.lang.Double,
 *        java.lang.String or java.util.Date
 * @return
 * @throws Exception 
 */
public static ShapefileDataStore createShapeFileDatastore( String name, String fieldsSpec, CoordinateReferenceSystem crs )
        throws Exception {
    // Create the file you want to write to
    File file = null;
    if (name.toLowerCase().endsWith(".shp")) {
        file = new File(name);
    } else {
        file = new File(name + ".shp");
    }

    ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
    Map<String, Serializable> create = new HashMap<String, Serializable>();
    create.put("url", file.toURI().toURL());
    ShapefileDataStore myData = (ShapefileDataStore) factory.createNewDataStore(create);

    // Tell this shapefile what type of data it will store
    // Shapefile handle only : Point, MultiPoint, MultiLineString,
    // MultiPolygon
    SimpleFeatureType featureType = DataUtilities.createType(name, fieldsSpec);

    // Create the Shapefile (empty at this point)
    myData.createSchema(featureType);

    // Tell the DataStore what type of Coordinate Reference System (CRS)
    // to use
    myData.forceSchemaCRS(crs);

    return myData;

}
 
开发者ID:TheHortonMachine,项目名称:hortonmachine,代码行数:41,代码来源:FeatureUtilities.java

示例3: preparePointFile

import org.geotools.data.shapefile.ShapefileDataStore; //导入方法依赖的package包/类
public void preparePointFile(ArrayList<osmNode> osmNodes) throws IOException, SchemaException {
	uniquePointTagKeys = getUniquePointTagKeys(osmNodes);
    String passPointString = new String("location:Point:srid=4326,"+"id:Double");
    for(String attribute:uniquePointTagKeys){
    	int colon;
    	do{
     	colon = attribute.indexOf(":");
     	if(colon!=-1){
     		attribute = attribute.substring(0, colon)+"_"+attribute.substring(colon+1);
     	}
    	}while(colon!=-1);
    	passPointString=passPointString+","+attribute+":String";
    }
    
    pointType = DataUtilities.createType("Location", passPointString);
    
    pointCollection = FeatureCollections.newCollection();
    
    pointBuilder = new SimpleFeatureBuilder(pointType);
    
    pointFeature = pointBuilder.buildFeature(null);
    
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    
    Map <String,Serializable> parameters = new HashMap<String, Serializable>();
    
    parameters.put("url",shpPointFile.toURI().toURL());
    parameters.put("create spatial index",Boolean.TRUE);
    
    ShapefileDataStore newdatastore = (ShapefileDataStore)dataStoreFactory.createNewDataStore(parameters);
    newdatastore.createSchema(pointType);
    
    newdatastore.forceSchemaCRS(DefaultGeographicCRS.WGS84);  
    
    String typename = newdatastore.getTypeNames()[0];
    
    SimpleFeatureSource simpfeatsource = newdatastore.getFeatureSource(typename);
    
    pointFeatureStore = (SimpleFeatureStore)simpfeatsource;
}
 
开发者ID:banmedo,项目名称:Converter,代码行数:41,代码来源:shpMain.java

示例4: prepareLineFile

import org.geotools.data.shapefile.ShapefileDataStore; //导入方法依赖的package包/类
public void prepareLineFile(ArrayList<osmLine> osmLines) throws SchemaException, MalformedURLException, IOException {
	uniqueLineTagKeys = getUniqueTagKeys(osmLines,"lines");
    String passLineString = new String("location:LineString:srid=4326,"+"id:Double");
    for(String attribute:uniqueLineTagKeys){
    	int colon;
    	do{
     	colon = attribute.indexOf(":");
     	if(colon!=-1){
     		attribute = attribute.substring(0, colon)+"_"+attribute.substring(colon+1);
     	}
    	}while(colon!=-1);
    	passLineString=passLineString+","+attribute+":String";
    }
    
	lineType = DataUtilities.createType("Location", passLineString);
    
    lineCollection = FeatureCollections.newCollection();
    
    lineBuilder = new SimpleFeatureBuilder(lineType);
    
    lineFeature = lineBuilder.buildFeature(null);
    
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    
    Map <String,Serializable> parameters = new HashMap<String, Serializable>();
    
    parameters.put("url",shpLineFile.toURI().toURL());
    parameters.put("create spatial index",Boolean.TRUE);
    
    ShapefileDataStore newdatastore = (ShapefileDataStore)dataStoreFactory.createNewDataStore(parameters);
    newdatastore.createSchema(lineType);
    
    newdatastore.forceSchemaCRS(DefaultGeographicCRS.WGS84);  
    
    String typename = newdatastore.getTypeNames()[0];
    
    SimpleFeatureSource simpfeatsource = newdatastore.getFeatureSource(typename);
    
    lineFeatureStore = (SimpleFeatureStore)simpfeatsource;
}
 
开发者ID:banmedo,项目名称:Converter,代码行数:41,代码来源:shpMain.java

示例5: writeShapeFile

import org.geotools.data.shapefile.ShapefileDataStore; //导入方法依赖的package包/类
/**
 * Write a feature collection to a shape(.shp) file using the
 * standard GeoTools way of creating shape files. This function will
 * write the shape file using the Coordinate system WGS84.
 *
 * @param SimpleFeatureCollection collection The collection of features you wish to write to the shape file.
 * @param SimpleFeatureType TYPE The type object of the feature you wish to use.
 */
private static void writeShapeFile(SimpleFeatureCollection collection, SimpleFeatureType TYPE) throws IOException{

	//Prompt the user for a save location and filename.
	File newFile = getNewShapeFile();

	//Set up data store factory.
       ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();

       //Set up maps and parameters.
       Map<String, Serializable> params = new HashMap<String, Serializable>();
       params.put("url", newFile.toURI().toURL());
       params.put("create spatial index", Boolean.TRUE);

       //Set up data store.
       ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
       dataStore.createSchema(TYPE);

       //Set CRS.
       dataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
	
       //Create transaction.
       Transaction transaction = new DefaultTransaction("create");

       //Set up type name and feature source.
       String typeName = dataStore.getTypeNames()[0];
       SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);

       if(featureSource instanceof SimpleFeatureStore) {
           
       	//Set up feature store and attempt the transaction.
       	SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
           featureStore.setTransaction(transaction);
           try{
               featureStore.addFeatures(collection);
               transaction.commit();
           }catch (Exception e) {
               e.printStackTrace();
               transaction.rollback();
           }finally{
               transaction.close();
           }
       
       //If the type does not support read/write access.
       }else{
           System.out.println(typeName + " does not support read/write access");
       }
	
}
 
开发者ID:Stefangemfind,项目名称:MapMatching,代码行数:57,代码来源:App.java

示例6: buildBlocks

import org.geotools.data.shapefile.ShapefileDataStore; //导入方法依赖的package包/类
public void buildBlocks(File shapeFile) throws IOException, FactoryException {
	ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
	
	SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setName("Block");
    builder.setCRS(DefaultGeographicCRS.WGS84); 

    // add attributes in order
    builder.add("Block", Polygon.class);
    builder.length(16).add("id", String.class); 
    for(String attribute : attributes.attributeGroups.keySet()) {
    	builder.add(attribute, Long.class); 
    }

    Map<String, Serializable> params = new HashMap<String, Serializable>();
	params.put("url", shapeFile.toURI().toURL());
	params.put("create spatial index", Boolean.TRUE);
	
	ShapefileDataStore dataStore = (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
	dataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
    
    // build the type
    final SimpleFeatureType BLOCKS_TYPE = builder.buildFeatureType();
    
    dataStore.createSchema(BLOCKS_TYPE);
    
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
    
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(BLOCKS_TYPE);
    
    for(String blockId : blocks.lodesBlocks.keySet()) {
    	if(blocks.lodesBlocks.get(blockId).percentLand < 0.5)
    		continue;
    	
    	featureBuilder.add(blocks.lodesBlocks.get(blockId).geom);
    	
    	featureBuilder.add(blockId);
    	
    	for(String attributeId : attributes.attributeGroups.keySet()) {
    		
    		HashMap<String, Long> lodesAttribute = attributes.lodesAttributes.get(blockId);
    		if(lodesAttribute != null) {
    			Long attributeValue = lodesAttribute.get(attributeId);
    			featureBuilder.add(attributeValue);
    		}
    		else {
    			//System.out.println("blockId: " + blockId + " is missing " + attributeId);
    		}
    	}
    	
        SimpleFeature feature = featureBuilder.buildFeature(null);
        featureCollection.add(feature);
        
    }
    
    Transaction transaction = new DefaultTransaction("create");

    String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);

    if (featureSource instanceof SimpleFeatureStore) 
    {
        SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

        featureStore.setTransaction(transaction);
       
        featureStore.addFeatures(featureCollection);
        transaction.commit();

        transaction.close();
    } 
}
 
开发者ID:conveyal,项目名称:lodes-processor,代码行数:73,代码来源:LodesProcessor.java

示例7: buildHaltonPopulation

import org.geotools.data.shapefile.ShapefileDataStore; //导入方法依赖的package包/类
public void buildHaltonPopulation(File shapeFile) throws IOException, FactoryException {
	ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
	
	SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setName("Block");
    builder.setCRS(DefaultGeographicCRS.WGS84); 

    // add attributes in order
    builder.add("Jobs", MultiPoint.class);
    builder.length(16).add("id", String.class); 
    builder.length(5).add("type", String.class);

    Map<String, Serializable> params = new HashMap<String, Serializable>();
	params.put("url", shapeFile.toURI().toURL());
	params.put("create spatial index", Boolean.TRUE);
	
	ShapefileDataStore dataStore = (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
	dataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
    
    // build the type
    final SimpleFeatureType BLOCKS_TYPE = builder.buildFeatureType();
    
    dataStore.createSchema(BLOCKS_TYPE);
    
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
    
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(BLOCKS_TYPE);
    
    for(String blockId : blocks.lodesBlocks.keySet()) {
    	
    	IndicatorItem block = blocks.lodesBlocks.get(blockId);
    	   	
    	
    	for(String attributeId : attributes.attributeGroups.keySet()) {
    		
    		HashMap<String, Long> lodesAttribute = attributes.lodesAttributes.get(blockId);
    		if(lodesAttribute != null) {
        		long attributeValue = lodesAttribute.get(attributeId);
        		
        		if (attributeValue < Integer.MIN_VALUE || attributeValue > Integer.MAX_VALUE) {
        	        System.out.println(blockId + " " + attributeId + " exceeds int val max: " + attributeValue);
        	    } 
        		else {
        			featureBuilder.add(block.haltonPoints((int)attributeValue));
            		featureBuilder.add(blockId);
            		featureBuilder.add(attributeId);
            		
            		SimpleFeature feature = featureBuilder.buildFeature(null);
                    featureCollection.add(feature);
        		}
    		}
    		else {
    			//System.out.println("blockId: " + blockId + " is missing " + attributeId);
    		}
    	}   
    }
    
    Transaction transaction = new DefaultTransaction("create");

    String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);

    if (featureSource instanceof SimpleFeatureStore) 
    {
        SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

        featureStore.setTransaction(transaction);
       
        featureStore.addFeatures(featureCollection);
        transaction.commit();

        transaction.close();
    } 
}
 
开发者ID:conveyal,项目名称:lodes-processor,代码行数:75,代码来源:LodesProcessor.java

示例8: writeToShapefile

import org.geotools.data.shapefile.ShapefileDataStore; //导入方法依赖的package包/类
private static void writeToShapefile(String output_fn,
		HashMap<Feature, Double> dissMags) throws MalformedURLException,
		IOException {
	System.out.println( "printing to shapefile..." );
	ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
	
	Map<String, Serializable> params = new HashMap<String, Serializable>();
	params.put("url", new File(output_fn).toURI().toURL());
	params.put("create spatial index", Boolean.TRUE);
	
	ShapefileDataStore outputStore = (ShapefileDataStore)dataStoreFactory.createNewDataStore(params);
	outputStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
	
	// build the type
	SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
	builder.setName("diss");
	builder.setCRS(DefaultGeographicCRS.WGS84); 
	builder.add("the_geom", MultiPolygon.class);
	builder.length(16).add("mag", Float.class); 
	
	final SimpleFeatureType dissType = builder.buildFeatureType();
	outputStore.createSchema(dissType);
	
	DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
	SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(dissType);
	
	int j=0;
	for(Entry<Feature, Double> entry : dissMags.entrySet()) {
		if(j%1000==0){
			System.out.println("writing feature "+j);
		}
		
		Feature diss = entry.getKey();
		Geometry dissGeom = (Geometry)diss.getDefaultGeometryProperty().getValue();
		double mag = entry.getValue();

		featureBuilder.add(dissGeom);
		featureBuilder.add(mag);
	
	    SimpleFeature feature = featureBuilder.buildFeature(null);
	    featureCollection.add(feature);
	    
	    j++;
	}
	
	Transaction transaction = new DefaultTransaction("create");
	String outputTypeName = outputStore.getTypeNames()[0];
	SimpleFeatureSource featureSource = outputStore.getFeatureSource(outputTypeName);
	if (featureSource instanceof SimpleFeatureStore) 
	{
		System.out.println("committing");
	    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

	    featureStore.setTransaction(transaction);
	   
	    featureStore.addFeatures(featureCollection);
	    transaction.commit();

	    transaction.close();
	}
}
 
开发者ID:conveyal,项目名称:aggregate-disser,代码行数:62,代码来源:Disser.java

示例9: createShapeFile

import org.geotools.data.shapefile.ShapefileDataStore; //导入方法依赖的package包/类
private File createShapeFile(FeatureCollection<SimpleFeatureType, SimpleFeature> collection) throws IOException {

		String shapeFileSuffix = ".shp";

		File tempBaseFile = File.createTempFile("resolveDir", ".tmp");
		tempBaseFile.deleteOnExit();
		File parent = tempBaseFile.getParentFile();

		File shpBaseDirectory = new File(parent, UUID.randomUUID().toString());

		if (!shpBaseDirectory.mkdir()) {
			throw new IllegalStateException("Could not create temporary shp directory.");
		}

		File tempSHPfile = File.createTempFile("shp", shapeFileSuffix, shpBaseDirectory);
		tempSHPfile.deleteOnExit();
		DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
		Map<String, Serializable> params = new HashMap<String, Serializable>();
		params.put("url", tempSHPfile.toURI().toURL());
		params.put("create spatial index", Boolean.TRUE);

		ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory
				.createNewDataStore(params);

		newDataStore.createSchema(collection.getSchema());
		if(collection.getSchema().getCoordinateReferenceSystem()==null){
			newDataStore.forceSchemaCRS(getCRS_WGS84());
		}else{
			newDataStore.forceSchemaCRS(collection.getSchema()
				.getCoordinateReferenceSystem());
		}

		Transaction transaction = new DefaultTransaction("create");

		String typeName = newDataStore.getTypeNames()[0];
		@SuppressWarnings("unchecked")
		FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>) newDataStore
				.getFeatureSource(typeName);
		featureStore.setTransaction(transaction);
		try {
			featureStore.addFeatures(collection);
			transaction.commit();
		} catch (Exception problem) {
			transaction.rollback();
		} finally {
			transaction.close();
		}

		// Get names of additional files
		String path = tempSHPfile.getAbsolutePath();
		String baseName = path.substring(0, path.length() - shapeFileSuffix.length());
		File shx = new File(baseName + ".shx");
		File dbf = new File(baseName + ".dbf");
		File prj = new File(baseName + ".prj");

		// mark created files for delete
		tempSHPfile.deleteOnExit();
		shx.deleteOnExit();
		dbf.deleteOnExit();
		prj.deleteOnExit();
		shpBaseDirectory.deleteOnExit();

		return shpBaseDirectory;
	}
 
开发者ID:enviroCar,项目名称:enviroCar-server,代码行数:65,代码来源:TrackShapefileEncoder.java

示例10: preparePolygonFIle

import org.geotools.data.shapefile.ShapefileDataStore; //导入方法依赖的package包/类
public void preparePolygonFIle(ArrayList <osmLine> osmPolygons) throws SchemaException, IOException {
	uniquePolygonTagKeys = getUniqueTagKeys(osmPolygons,"polygons");
    Collections.sort(uniquePolygonTagKeys);
    String passPolygonString = new String("location:Polygon:srid=4326,"+"id:Double");
    for(String attribute:uniquePolygonTagKeys){
    	int colon;
    	do{
     	colon = attribute.indexOf(":");
     	if(colon!=-1){
     		attribute = attribute.substring(0, colon)+"_"+attribute.substring(colon+1);
     	}
    	}while(colon!=-1);
    	passPolygonString=passPolygonString+","+attribute+":String";
    }

    pointType = DataUtilities.createType("Location", passPolygonString);
    
    polygonCollection = FeatureCollections.newCollection();
    
    polygonBuilder = new SimpleFeatureBuilder(pointType);
    
    polygonFeature = polygonBuilder.buildFeature(null);
    
    ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
    
    Map <String,Serializable> parameters = new HashMap<String, Serializable>();
    
    parameters.put("url",shpPolygonFile.toURI().toURL());
    parameters.put("create spatial index",Boolean.TRUE);
    
    ShapefileDataStore newdatastore = (ShapefileDataStore)dataStoreFactory.createNewDataStore(parameters);
    newdatastore.createSchema(pointType);
    
    newdatastore.forceSchemaCRS(DefaultGeographicCRS.WGS84);  
    
    String typename = newdatastore.getTypeNames()[0];
    
    SimpleFeatureSource simpfeatsource = newdatastore.getFeatureSource(typename);
    
    polygonFeatureStore = (SimpleFeatureStore)simpfeatsource;
}
 
开发者ID:banmedo,项目名称:Converter,代码行数:42,代码来源:shpMain.java


注:本文中的org.geotools.data.shapefile.ShapefileDataStore.forceSchemaCRS方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。