I am implementing a web service in Java which takes a GeoJSON file as input and converts it into a shapefile. My code looks something like this :-
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String submit(@RequestParam("file") MultipartFile file, ModelMap modelMap) throws IOException {
modelMap.addAttribute("file", file);
File ew = convert(file);
// String[] cmd = {"-t_srs", "CRS:84", "-f", "GeoJSON","[name].geojson","[name].shp"};
// ogr2ogr.main(cmd);
toShp(ew);
return "fileUploadView"; // ignore this return statement
}
public File convert(MultipartFile file) throws IOException
{
File convFile = new File("D:\\Gauravmmi\\0projects\\sample.GeoJSON");
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
return convFile;
}
public static void toShp(File geojson) throws IOException {
File shpFile = new File("D:\\Gauravmmi\\0projects\\test.shp");
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", shpFile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
/*
* InputStream in = new FileInputStream(geojson); int decimals = 15;
* GeometryJSON gjson = new GeometryJSON(decimals); FeatureJSON fjson = new
* FeatureJSON(gjson);
*
* FeatureCollection<SimpleFeatureType, SimpleFeature> fc =
* fjson.readFeatureCollection(in); fc.getSchema();
*
* WriteShapefile writer = new WriteShapefile(shpFile);
* writer.writeFeatures(fc);
*/
ShapefileDataStore shpDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
InputStream in = new FileInputStream(geojson);
int decimals = 15;
GeometryJSON gjson = new GeometryJSON(decimals);
FeatureJSON fjson = new FeatureJSON(gjson);
FeatureCollection fc = fjson.readFeatureCollection(in);
SimpleFeatureType type = (SimpleFeatureType) fc.getSchema();
shpDataStore.createSchema(type);
Transaction transaction = new DefaultTransaction("create");
String typeName = shpDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = shpDataStore.getFeatureSource(typeName);
if (featureSource instanceof FeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(fc);
transaction.commit();
} catch (Exception ex) {
ex.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
} else {
System.out.println(typeName + " does not support read/write access");
}
}
And my sample GeoJSON looks something like this:-
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": { // no luck changing this to "the_geom"
"type": "LineString",
"coordinates": [
[
72.99316406249999,
27.449790329784214
],
[
78.134765625,
27.68352808378776
],
[
77.5634765625,
26.15543796871355
],
[
83.8037109375,
24.806681353851964
],
[
84.111328125,
26.82407078047018
],
[
86.2646484375,
25.045792240303445
],
[
86.17675781249999,
22.998851594142913
],
[
82.001953125,
19.80805412808859
],
[
77.82714843749999,
19.062117883514652
],
[
76.1572265625,
16.25686733062344
],
[
77.2119140625,
13.539200668930816
],
[
75.3662109375,
14.43468021529728
],
[
74.970703125,
17.476432197195518
],
[
75.05859375,
19.766703551716976
],
[
73.47656249999999,
21.248422235627014
],
[
71.5869140625,
22.59372606392931
],
[
71.89453125,
24.086589258228027
],
[
72.8173828125,
26.902476886279832
],
[
74.92675781249999,
22.998851594142913
],
[
75.8056640625,
25.085598897064752
],
[
71.7462158203125,
27.415662034561
],
[
73.531494140625,
28.134971934813155
],
[
73.0206298828125,
27.42053815128712
],
[
74.619140625,
27.761329874505233
],
[
74.59167480468749,
27.49365405370312
]
]
}
}
]
}
When I am running the sample GeoJSON through the controller I am getting five files in output (test.dbf,test.fix,test.prj,test.shp.test.shx) where test.prj contains nothing. And so when I import this shapefile in QGIS I get nothing?