Package chunmap.data.provider

Source Code of chunmap.data.provider.LayerFactory

/**
* Copyright (c) 2009-2011, chunquedong(YangJiandong)
*
* This file is part of ChunMap project
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
*
* History:
*     2010-05-05  Jed Young  Creation
*/
package chunmap.data.provider;

import java.nio.charset.Charset;
import java.util.List;

import chunmap.data.feature.Feature;
import chunmap.data.feature.FeatureCollection;
import chunmap.data.feature.LayerMetadata;
import chunmap.data.feature.ShapeFeature;
import chunmap.data.feature.VisitAction;
import chunmap.data.index.FeatureList;
import chunmap.data.provider.postgis.PostgisConnection;
import chunmap.data.provider.postgis.PostgisDataSource;
import chunmap.data.provider.shp.ShpDataSource;
import chunmap.model.elem.EnvelopeBuf;
import chunmap.model.geom.Geometry;
import chunmap.model.geom.GeometryType;
import chunmap.view.layer.VectorLayer;

/**
* 地图数据工厂
* @author chunquedong
*
*/
public class LayerFactory {
  public static VectorLayer openShapeFile(String path)
    {
    ShpDataSource pDR = new ShpDataSource(path);
        GeoDataAdapter pDA = new GeoDataAdapter(pDR);

        FeatureCollection fc = pDA.createFeatureList();
        pDR.close();
        return new VectorLayer(fc);
    }
    public static VectorLayer openShapeFile(String path, final int labelColum)
    {
      final ShpDataSource pDR = new ShpDataSource(path,Charset.forName("GBK"));
      //System.out.print(Charset.defaultCharset());
        GeoDataAdapter pDA = new GeoDataAdapter(pDR);

        FeatureCollection fc = pDA.createFeatureList();
        fc.each(new VisitAction(){
      @Override
      public void execute(Feature obj) {
        ShapeFeature shp = (ShapeFeature)obj;
        Object[] data=pDR.data(shp.getId());
        shp.setValues(data);
              String s = data[labelColum].toString();
              shp.setLabel(s);
      }});
        fc.setFeatureSchama(pDR.getFeatureSchama());
        pDR.close();
        return new VectorLayer(fc);
    }

    public static VectorLayer OpenPostGIS(String tableName
        , PostgisConnection ConnectionString, String geom, String id)
    {
      PostgisDataSource pDR = new PostgisDataSource(tableName , geom, id,ConnectionString);
        GeoDataAdapter pDA = new GeoDataAdapter(pDR);
        VectorLayer layer = new VectorLayer(pDA.createFeatureList());
        pDR.close();
        return layer;
    }

    //------------------------------------------------------------------------

    public static VectorLayer createGeometryLayer(List<Geometry> gs)
    {
        if (gs.size() == 0) return null;

        FeatureCollection fc = new FeatureList();
        EnvelopeBuf eb = new EnvelopeBuf();
        for(Geometry g : gs){
          ShapeFeature f = createFeature(g);
            fc.insert(f);
            f.setFeatureClass(fc);
            eb.mergeEnvelop(g.getEnvelop());
        }

        fc.setMetadata( new LayerMetadata("Geometry",gs.get(0).getGeometryType()));
        fc.setEnvelope(eb.toEnvelop());

        return new VectorLayer(fc);
    }
    private static ShapeFeature createFeature(Geometry g)
    {
      ShapeFeature shp = new ShapeFeature();
        shp.setGeometry(g);
        return shp;
    }

    public static VectorLayer createFeatureLayer(List<Feature> fs)
    {
      FeatureList fc = new FeatureList();
        EnvelopeBuf eb = new EnvelopeBuf();
        for (Feature f : fs)
        {
            fc.insert(f);
            eb.mergeEnvelop(f.getEnvelop());
        }

        fc.setMetadata (new LayerMetadata("Feature",GeometryType.GeometryCollection));
        fc.setEnvelope ( eb.toEnvelop());

        return new VectorLayer(fc);
    }
}
TOP

Related Classes of chunmap.data.provider.LayerFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.