2

I want to add a custom label which would contain param1 on the map. So, I created a datastore from the .csv file, added my custom parameters to the SimpleFeatureTypeBuilder as properties and created a TextSymbolizer. But I've got only lat/lon parameters on my map. What do I do wrong?

SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();

    b.setName("MyFeatureType");
    b.setCRS(crs);
    b.add("location", Point.class);
    b.add("param1", String.class);
    b.add("param2", Double.class);
    final SimpleFeatureType TYPE = b.buildFeatureType();

    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
    GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();

    Point point = geometryFactory.createPoint(new Coordinate( coordinate.getX(), coordinate.getY()));
    featureBuilder.add(point);
    SimpleFeature feature = featureBuilder.buildFeature(null);
    DefaultFeatureCollection featureCollection = new DefaultFeatureCollection("internal", TYPE);
    featureCollection.add(feature);

// Style style = SLD.createSimpleStyle(TYPE,Color.GREEN);

    StyleBuilder styleBuilder = new StyleBuilder();
    FilterFactory ff = styleBuilder.getFilterFactory();
    Style style = SLD.createSimpleStyle(TYPE, Color.GREEN);
    style.setName("MyStyle");
    TextSymbolizer textSymb = styleBuilder.createTextSymbolizer(Color.black, styleBuilder.createFont("Arial", 10),
        "fred");
    textSymb.setLabel(ff.function("Concatenate", ff.function("getX", ff.property("location")), ff.literal(" "),
        ff.function("getY", ff.property("location")), ff.literal(" "), ff.function("env", ff.property("param1")), 
        ff.literal(" "), ff.function("env", ff.property("param2"))));
    //textSymb.setHalo(styleBuilder.createHalo(Color.white, 1));
    style.featureTypeStyles().get(0).rules().get(0).symbolizers().add(textSymb);

    Layer layer = new FeatureLayer(featureCollection, style);

here is the fragment of my .csv file

LAT,LON,param1,param2,param3,param4,param5
27.0,-30.0,280.9,-6.37312,-1.79618,6.621398154805826,74.2571170243146,
27.0,-27.0,281.1,-6.43312,-2.77618,7.006582132534355,66.65431728639881,
27.0,-24.0,281.3,-4.54312,-3.10618,5.50347827615005,55.63552261029227,
27.0,-21.0,281.1,-3.38312,-3.72618,5.032881324695736,42.23329167237091,
27.0,-18.0,280.7,-2.34312,-4.09618,4.718990859456285,29.766254126085926,
27.0,-15.0,280.3,-2.90312,-3.95618,4.907080650538935,36.26771217705186,
27.0,-12.0,280.0,-4.31312,-4.31618,6.101834652637045,44.97572026458005,
27.0,-9.0,279.2,-5.72312,-4.12618,7.055454644009236,54.20595659912361,
27.0,-6.0,279.3,-6.43312,-3.90618,7.526171230830957,58.73038946633453,
27.0,-3.0,279.4,-8.03312,-4.58618,9.250083120363465,60.27405416053762,
27.0,0.0,279.7,-8.76312,-4.05618,9.656336569885982,65.15865639285987,
27.0,3.0,283.7,-8.00312,-1.12618,8.081968231070002,81.98720020607146,
27.0,6.0,284.8,-6.68312,1.19382,6.788910822952606,100.1257130758201,
27.0,9.0,284.8,-4.84312,3.27382,5.84583022595845,124.05602094541278

1 Answer 1

1

The main issue is that you are using the env function to access an "environment" variable rather than the property you want. So modifying your label expression to

Expression label = ff.function("Concatenate", ff.function("getX", ff.property("location")), ff.literal(" "),
    ff.function("getY", ff.property("location")), ff.literal(" "), ff.property("param1"), ff.literal(" "),
    ff.property("param2"));

will produce the label you want.

enter image description here

You can also make life easier for yourself by using a CSVDataStore to read in your file.

File file = new File(fileName);
Map<String, Object> params = new HashMap<>();
params.put(CSVDataStoreFactory.URL_PARAM.key, URLs.fileToUrl(file));
params.put(CSVDataStoreFactory.STRATEGYP.key, CSVDataStoreFactory.SPECIFC_STRATEGY);
params.put(CSVDataStoreFactory.LATFIELDP.key, "LAT");
params.put(CSVDataStoreFactory.LnGFIELDP.key, "LON");
ds = DataStoreFinder.getDataStore(params);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.