geojson输出

2021-06-27 21:05

阅读:549

标签: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 geoJson =null;

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

    /**
     * geometry
     * 包括 coordinates 和 type
     */
    Map geometry =null;

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

    /**
     * properties
     * 包括 color 和 lever
     */
    Map properties = null;

    /**
     * coordinate
     */
    List coordinate = null;

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

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

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

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

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

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

    /**
     * 填充 geoJson
     */
    public Map fillGeoJson(List> 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> 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> 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


评论


亲,登录后才可以留言!