Re: Forced Coordinate Display for LineString (Example) - More effective/better/flexible?

3 messages Options
Embed this post
Permalink
Oliver Gottwald

Re: Forced Coordinate Display for LineString (Example) - More effective/better/flexible?

Reply Threaded More More options
Print post
Permalink
Michael,
The below example is really cool and something I would have had to tackle most likely early next week for multi lineString render.
I took portions of your example and applied them to the Point (code listed below).   Weird thing is my label does not show up.  Style Code futher down...
========================================================
static final CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
    static final int MAX_NAME_LENGTH = 20;
   
    public FeatureCollection getPoint(TreeMap allPoints) throws Exception {
        SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
        typeBuilder.setName("mytype");
        typeBuilder.setCRS(crs);
        typeBuilder.add("route", Point.class);
        typeBuilder.length(MAX_NAME_LENGTH).add("name", String.class);
        final SimpleFeatureType TYPE = typeBuilder.buildFeatureType();
       
        FeatureCollection collection = FeatureCollections.newCollection();
        GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);   
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
       
          Collection cl = allPoints.values();
          Iterator it = cl.iterator();
          while (it.hasNext()){
              XYZPoint xyz = (XYZPoint)it.next();
              Point point = factory.createPoint( new Coordinate(xyz.getX(),xyz.getY()));
              featureBuilder.add(point);
              collection.add(featureBuilder.buildFeature(null));
          }
          return collection;
    }
===============================================================

Weird thing is my Point changes border color and fill color based on my style --- but the Label went by by.
Does the Label not showing up have to do with the following line:
typeBuilder.length(MAX_NAME_LENGTH).add("name", String.class);
somehow the style not mapping to the attribute "name"??

The following is my style settings for the Label:
===============================================================
    Fill labelFill = styleFactory.createFill(filterFactory.literal(Color.RED));
   
    AnchorPoint anchor = styleFactory.createAnchorPoint(filterFactory.literal(-0.5), filterFactory.literal(0));
    Displacement disp = styleFactory.createDisplacement(filterFactory.literal(0), filterFactory.literal(-10));
    LabelPlacement placement = styleFactory.createPointPlacement(anchor, disp, filterFactory.literal(0));
   
     String labelField="name";
     Font font=styleFactory.createFont(filterFactory.literal("Arial"), filterFactory.literal(true), filterFactory.literal(true), filterFactory.literal(8));
   
    TextSymbolizer textSym = styleFactory.createTextSymbolizer(
            labelFill, new Font[]{font}, null, filterFactory.property(labelField),
    placement, null);
  
    Rule rule = styleFactory.createRule();
    //Label rule
    rule.symbolizers().add(textSym);
   
    FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new Rule[]{rule});
    Style style = styleFactory.createStyle();
    style.featureTypeStyles().add(fts);   

===============================================================








Hi Oliver,

I've meddled with your example a bit so that it generates one than one
feature and uses SimpleFeatureTypeBuilder (as shown in the CSV 2 SHP
Lab example at the bottom of the code).

public class Foo
static final CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
static final int MAX_NAME_LENGTH = 20;

public static void main(String[] args) throws Exception {
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.setName("mytype");
typeBuilder.setCRS(crs);
typeBuilder.add("route", LineString.class);
typeBuilder.length(MAX_NAME_LENGTH).add("name", String.class);

final SimpleFeatureType TYPE = typeBuilder.buildFeatureType();

FeatureCollection collection = FeatureCollections.newCollection();

double x[][] = {{10, 20, 30, 40, 50}, {10, 20, 30, 40, 50}};
double y[][] = {{10, 20, 10, 20, 10}, {20, 30, 20, 30, 20}};
String[] names = {"foo", "bar"};

GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
ArrayList<Coordinate> coordList = new ArrayList<Coordinate>();

for (int line = 0; line < x.length; line++) {
for (int i = 0; i < x[line].length; i++) {
coordList.add(new Coordinate(x[line][i], y[line][i]));
}

Coordinate[] coords = CoordinateArrays.toCoordinateArray(coordList);
LineString lineString = factory.createLineString(coords);

featureBuilder.add(lineString);
featureBuilder.add(names[line]);
collection.add(featureBuilder.buildFeature(null));
coordList.clear();
}


MapContext map = new DefaultMapContext();
map.setTitle("My beautiful lines");
map.addLayer(collection, null);

// Now display the map
JMapFrame.showMap(map);
}
}


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
mbedward

Re: Forced Coordinate Display for LineString (Example) - More effective/better/flexible?

Reply Threaded More More options
Print post
Permalink
Hi Oliver,

In your code you have a "name" attribute in the feature type but when
you build each feature you are not adding an object for name. So it is
displaying the labels, but they are all blank :-)

Michael

PS. there's nothing sacred about the name "name" of course - the
attribute can be called anything you like.

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
Oliver Gottwald

Re: Forced Coordinate Display for LineString (Example) - More effective/better/flexible?

Reply Threaded More More options
Print post
Permalink

coreduuu  on my part :-)
simply adding:
featureBuilder.set("gravytrain",xyz.getName());
before: featureBuilder.add(point);
did the trick :-)
thanks

Michael Bedward wrote:

> Hi Oliver,
>
> In your code you have a "name" attribute in the feature type but when
> you build each feature you are not adding an object for name. So it is
> displaying the labels, but they are all blank :-)
>
> Michael
>
> PS. there's nothing sacred about the name "name" of course - the
> attribute can be called anything you like.
>
>  

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users