码迷,mamicode.com
首页 > Web开发 > 详细

geojson输出

时间:2017-07-10 14:25:55      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:list   new   eve   return   ack   obj   file   包括   color   

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GeoJsonOutput {

    /**
     * geoJson
     * 包括 features 和 type
     */
    Map<String,Object> geoJson =null;

    /**
     * features
     * 包括 geometry、properties、type
     */
    List<Map<String,Object>> features = null;
    Map<String,Object> feature = null;

    /**
     * geometry
     * 包括 coordinates 和 type
     */
    Map<String,Object> geometry =null;

    /**
     * coordinates
     */
    List<List<Double>> coordinates = null;

    /**
     * properties
     * 包括 color 和 lever
     */
    Map<String,String> properties = null;

    /**
     * coordinate
     */
    List<Double> coordinate = null;

    /**
     * 填充 coordinate
     */
    public List<Double> fillCoordnate(double lon,double lat,double z){
        coordinate = new ArrayList<>();
        coordinate.add(lon);
        coordinate.add(lat);
        coordinate.add(z);
        return coordinate;
    }

    /**
     * 填充properties
     */
    public Map<String,String> fillProperties(String color,String lever){
        properties = new HashMap<String,String>();
        properties.put("color",color);
        properties.put("lever",lever);
        return properties;
    }

    /**
     * 填充 coordinates
     */
    public List<List<Double>> fillCoordinates(List<List<Double>> list){
        coordinates = new ArrayList<List<Double>>();
        for (List coordinate: list) {
            coordinates.add(coordinate);
        }
        return coordinates;
    }

    /**
     * 填充 geometry
     */
    public Map<String,Object> fillGeometry(List<List<Double>> list){
        geometry = new HashMap<>();
        geometry.put("coordinates",this.fillCoordinates(list));
        geometry.put("type","LineString");
        return geometry;
    }

    /**
     * 填充 feature
     */
    public Map<String,Object> fillFeature(List<List<Double>> coordinates,String color,String lever){
        feature = new HashMap<String,Object>();
        feature.put("geometry",this.fillGeometry(coordinates));
        feature.put("properties",this.fillProperties(color,lever));
        feature.put("type","Feature");
        return feature;
    }

    /**
     * 填充 features
     */
    public List<Map<String,Object>> fillFeatures(List<Map<String,Object>> feature){
        features = new ArrayList<>();
        for (Map map: feature) {
            features.add(map);
        }
        return features;
    }

    /**
     * 填充 geoJson
     */
    public Map<String,Object> fillGeoJson(List<Map<String,Object>> feature){
        geoJson = new HashMap<>();
        geoJson.put("features",this.fillFeatures(feature));
        geoJson.put("type","FeatureCollection");
        return geoJson;
    }
}

 

public void jsonOutPut(Map map) {
        ObjectMapper mapper = new ObjectMapper();
        try{
            mapper.writeValue(new File("D:/geoJsonTest.json"), map);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

 

public class Test3 {

    public static void main(String[] args){
        GeoJsonOutput go = new GeoJsonOutput();

        /*填充 coordinates*/
        List<List<Double>> coordiantes = new ArrayList<>();
        coordiantes.add(go.fillCoordnate(104.63227074146543,28.769242770389777,0));
        coordiantes.add(go.fillCoordnate(104.64619058834954,28.77423963850202,0));
        coordiantes.add(go.fillCoordnate(104.66260886928977,28.76246130652316,0));

        /*填充 features*/
        List<Map<String,Object>> features = new ArrayList();
        features.add(go.fillFeature(coordiantes,"#FF0000","4"));
        features.add(go.fillFeature(coordiantes,"#00FF00","3"));
        features.add(go.fillFeature(coordiantes,"#FF0000","2"));
        features.add(go.fillFeature(coordiantes,"#00FF00","1"));

        /*填充 geoJson*/
        SiteGeneralImpl sg = new SiteGeneralImpl();
        sg.jsonOutPut(go.fillGeoJson(features));
    }
}

 

结果:

{
    "features": [
        {
            "geometry": {
                "coordinates": [
                    [
                        104.63227074146543, 
                        28.769242770389777, 
                        0
                    ], 
                    [
                        104.64619058834954, 
                        28.77423963850202, 
                        0
                    ], 
                    [
                        104.66260886928977, 
                        28.76246130652316, 
                        0
                    ]
                ], 
                "type": "LineString"
            }, 
            "type": "Feature", 
            "properties": {
                "color": "#FF0000", 
                "lever": "4"
            }
        }, 
        {
            "geometry": {
                "coordinates": [
                    [
                        104.63227074146543, 
                        28.769242770389777, 
                        0
                    ], 
                    [
                        104.64619058834954, 
                        28.77423963850202, 
                        0
                    ], 
                    [
                        104.66260886928977, 
                        28.76246130652316, 
                        0
                    ]
                ], 
                "type": "LineString"
            }, 
            "type": "Feature", 
            "properties": {
                "color": "#00FF00", 
                "lever": "3"
            }
        }, 
        {
            "geometry": {
                "coordinates": [
                    [
                        104.63227074146543, 
                        28.769242770389777, 
                        0
                    ], 
                    [
                        104.64619058834954, 
                        28.77423963850202, 
                        0
                    ], 
                    [
                        104.66260886928977, 
                        28.76246130652316, 
                        0
                    ]
                ], 
                "type": "LineString"
            }, 
            "type": "Feature", 
            "properties": {
                "color": "#FF0000", 
                "lever": "2"
            }
        }, 
        {
            "geometry": {
                "coordinates": [
                    [
                        104.63227074146543, 
                        28.769242770389777, 
                        0
                    ], 
                    [
                        104.64619058834954, 
                        28.77423963850202, 
                        0
                    ], 
                    [
                        104.66260886928977, 
                        28.76246130652316, 
                        0
                    ]
                ], 
                "type": "LineString"
            }, 
            "type": "Feature", 
            "properties": {
                "color": "#00FF00", 
                "lever": "1"
            }
        }
    ], 
    "type": "FeatureCollection"
}

 

geojson输出

标签:list   new   eve   return   ack   obj   file   包括   color   

原文地址:http://www.cnblogs.com/unique1319/p/7145296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!