Skip to content

Instantly share code, notes, and snippets.

@mtoonen
Created August 7, 2017 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtoonen/9b200c020f0e4263ac9ef41bccf6b0d1 to your computer and use it in GitHub Desktop.
Save mtoonen/9b200c020f0e4263ac9ef41bccf6b0d1 to your computer and use it in GitHub Desktop.
shape 2 dxf
public static void main(String[] args) throws IOException {
OGRDataStoreFactory factory = new BridjOGRDataStoreFactory();
//OGRDataStoreFactory factory = new JniOGRDataStoreFactory();
Map<String, String> connectionParamsShp = new HashMap<String, String>();
connectionParamsShp.put("DriverName", "ESRI Shapefile");
connectionParamsShp.put("DatasourceName", new File("AssetsShapePoint.shp").getAbsolutePath());
DataStore store = factory.createDataStore(connectionParamsShp);
SimpleFeatureSource source = store.getFeatureSource("AssetsShapePoint");
SimpleFeatureIterator it = source.getFeatures().features();
org.opengis.feature.simple.SimpleFeatureType sft = createNewFeatureType(source);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(sft);
List featureList = new ArrayList();
try {
while (it.hasNext()) {
SimpleFeature feature = it.next();
featureBuilder.add(feature.getDefaultGeometry());
SimpleFeature f = featureBuilder.buildFeature(null);
featureList.add(f);
}
} finally {
it.close();
}
SimpleFeatureCollection features = DataUtilities.collection(featureList);
File file = File.createTempFile("downloaddxf", ".dxf");
Map<String, String> connectionParams = new HashMap<String, String>();
connectionParams.put("DriverName", "DXF");
connectionParams.put("DatasourceName", file.getAbsolutePath());
OGRDataStore dataStore = (OGRDataStore) factory.createNewDataStore(connectionParams);
dataStore.createSchema(features, false, null);
dataStore.dispose();
}
private static org.opengis.feature.simple.SimpleFeatureType createNewFeatureType(SimpleFeatureSource sfs) throws IOException {
org.opengis.feature.simple.SimpleFeatureType oldSft = sfs.getSchema();
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
b.setName(sfs.getName());
b.setCRS(oldSft.getGeometryDescriptor().getCoordinateReferenceSystem());
b.add("the_geom", oldSft.getGeometryDescriptor().getType().getBinding()); // then add geometry
org.opengis.feature.simple.SimpleFeatureType newFeatureType = b.buildFeatureType();
return newFeatureType;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment