Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

How create a line on empty layer

Fabio
2013-07-27
2013-07-27
  • Fabio
    Fabio
    2013-07-27

    Hi
    I'm a beginner and I would like to see a simple example of hoe create a line on a layer.
    I've already read some tutorial about but some methods written are already deprecated.

    I tried to deploy the code below without sucess:

    Coordinate c1 = new Coordinate(10,70);
    Coordinate c2 = new Coordinate(5,55);

           final Collection<Coordinate> listCord = new ArrayList<Coordinate>();
           listCord.add(c1);
           listCord.add(c2);
    
           LineString[] geo1 = GeometryFactory.toLineStringArray(listCord);
    
           FeatureSchema fs1 = new FeatureSchema();
           fs1.addAttribute("geometry", AttributeType.GEOMETRY);
    
           Feature feature1 = new BasicFeature(fs1);
           feature1.setAttribute("geometry", c1);
    
           FeatureCollection fc1 = new FeatureDataset(fs1);
           fc1.add(feature1);
    
           getPlugInContext().addLayer(StandardCategoryNames.WORKING, "New Layer",fc1);
    

    Please what is wrong with this code?

    Thanks in advance for your help.

    Fabio

     
  • mentaer
    mentaer
    2013-07-27

    Hi Fabio,

    attached my code, which should work.
    I have also made a plugin class, but I guess you have this already? Anyway, I can post my full code as well.

    GeometryFactory gf = new GeometryFactory(); //this is a new line

    Coordinate c1 = new Coordinate(10,70);
    Coordinate c2 = new Coordinate(5,55);

    final Collection<Coordinate> listCord = new ArrayList<Coordinate>();
    listCord.add(c1);
    listCord.add(c2);

    Coordinate[] coords = listCord.toArray(new Coordinate[listCord.size()]); //sstein: new line
    LineString line = gf.createLineString(coords); //sstein: new line

    FeatureSchema fs1 = new FeatureSchema();
    fs1.addAttribute("geometry", AttributeType.GEOMETRY);

    Feature feature1 = new BasicFeature(fs1);
    feature1.setGeometry(line); //sstein: changed this lines

    FeatureCollection fc1 = new FeatureDataset(fs1);
    fc1.add(feature1);

    //display the result FCs
    if(fc1.size() > 0){
    context.addLayer(StandardCategoryNames.WORKING, "my LineString" , fc1);
    }

    cheers,
    stefan

     
  • Fabio
    Fabio
    2013-07-27

    Hi Stefan!

    It's perfect! From this code now I can arrived in others shapes.

    Thanks a lot!
    Fabio