shp2geojson code example

Example: shp2geojson

/**     * The background will convert shp data to geojson, return     * @return     */    @RequestMapping("/geojson")    @ResponseBody    public Object shp2geojson()    {         String shpPath=this.getClass().getResource("/").getFile()+"/file/pointgbk.shp";        String  jsonPath=this.getClass().getResource("/").getFile()+"file/point.geojson";        Map map = new HashMap();                 / / New json object        FeatureJSON fjson = new FeatureJSON();        JSONObject geojsonObject=new JSONObject();        geojsonObject.put("type","FeatureCollection");        try{                         / / Get the feature collection            File file = new File(shpPath);            ShapefileDataStore shpDataStore = null;            shpDataStore = new ShapefileDataStore(file.toURL());                         / / Set the code/*            Charset charset = Charset.forName("GBK");            shpDataStore.setCharset(charset);*/            String typeName = shpDataStore.getTypeNames()[0];            SimpleFeatureSource featureSource = null;            featureSource =  shpDataStore.getFeatureSource (typeName);            SimpleFeatureCollection result = featureSource.getFeatures();            SimpleFeatureIterator itertor = result.features();            JSONArray array = new JSONArray();                         / / Traversing feature to json object            while (itertor.hasNext())            {                SimpleFeature feature = itertor.next();                StringWriter writer = new StringWriter();                fjson.writeFeature(feature, writer);                String temp=writer.toString();                byte[] b=temp.getBytes("iso8859-1");                temp=new String(b,"gbk");                System.out.println(temp);                JSONObject json =  JSON.parseObject(temp);                array.add(json);            }            geojsonObject.put("features",array);            itertor.close();             long startTime=System.currentTimeMillis();                          / / Write the json string to the file using the character stream/*            File outputfile=new File(jsonPath);            BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(outputfile));            bufferedWriter.write(JSON.toJSONString(geojsonObject));            bufferedWriter.flush();            bufferedWriter.close();*/            File outputfile=new File(jsonPath);            FileOutputStream fileOutputStream=new FileOutputStream(outputfile);            OutputStreamWriter outputStreamWriter=new OutputStreamWriter(fileOutputStream,"utf-8");            outputStreamWriter.write(JSON.toJSONString(geojsonObject));            outputStreamWriter.flush();            outputStreamWriter.close();                          / / Write the json string to the file using a byte stream/*            File outputfile=new File(jsonPath);            BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(outputfile));            byte[] bytes= JSON.toJSONString(geojsonObject).getBytes("utf-8");            bufferedOutputStream.write(bytes);            //fileOutputStream.write(JSON.toJSONString(geojsonObject));            bufferedOutputStream.flush();            bufferedOutputStream.close();*/             long endTime=System.currentTimeMillis();                         System.out.println("current program time: "+(endTime-startTime)+"ms");        }        catch(Exception e){            map.put("status", "failure");            map.put("message", e.getMessage());            e.printStackTrace();         }         return geojsonObject;    }

Tags:

Misc Example