[Gvsig_english] Issue with layer position when creating new layers

Francisco José Peñarrubia fpenarru at gmail.com
Wed May 5 15:52:30 CEST 2010


Hi Flavio.

I didn't test your code, but maybe you are missing 
mapContext.beginAtomicEvent() and mapContext.endAtomicEvent().

I have something like this, and it works:

            mc.getMapContext().beginAtomicEvent();
            final FLayers mcLayers = mc.getMapContext().getLayers();
            FLayers newGroup = 
mc.getMapContext().getNewGroupLayer(mcLayers);
            newGroup.setProjection(mc.getProjection());
            newGroup.setName(nom);

            for (int i=0; i < shpLayers.size(); i++) {
                ShpLegend aux = shpLayers.get(i);
                FLyrVect lyr = (FLyrVect) LayerFactory.createLayer(nom + 
aux.shpName, "gvSIG shp driver",
                                new File(dirShps.getPath() + 
File.separator + nom+
                                        aux.shpName + ".shp"), 
mc.getProjection());
               
                newGroup.addLayer(lyr);
                loadLegend(lyr, aux.gvlName);
            }
            mcLayers.addLayer(newGroup);
            mcLayers.moveTo(0, mcLayers.getLayersCount()-1);
            mc.getMapContext().endAtomicEvent();

Hope it helps.

Best regards.

Fran Peñarrubia
www.scolab.es


Flavio Pompermaier escribió:
> Hi to all,
> I'm using gvSIG 1.9 revision 1253 and I have some trouble whit layer
> positioning.
> I have a list of layer to add on login and, if this layer is not
> present, I want to add it on the bottom of the toc and not on top as
> default.
> However, if I use Flayers.addLayer(0, layer) it is not added at the
> bottom of the toc..
> This is the code I use in my project (a simplified version):
>
>        // 1 store layer position before addition
>        for (MyLayer workLayer : workLayers) {
> 		String layerName = workLayer.getLayerNameI();
> 		int position = getLayerPostion(flayers, layerName);
> 		if (position >= 0) {
> 			layerPositions.put(layerName, position);
> 		}
> 	}
>
>         //2 remove layer to add if present and then add
> 	for (MyLayer workLayer : workLayers) {
> 		String layerName = workLayer.getLayerNameI();
> 		/remove layer if it already exists
> 		FLayer alreadyExistingLayer = flayers.getLayer(layerName);
> 		if (alreadyExistingLayer != null) {
> 			flayers.removeLayer(alreadyExistingLayer);
> 		}
> 		//and substitute with the new generated one
> 		flayers.addLayer(createLayerInView(workLayer, projection));
> 		log.debug("Layer substituted: " + workLayer.getServiceLayerName());
> 	}
>
>         // 3 reorder as before removing/adding (point 2)
>         int numLayers = flayers.getLayersCount() - 1;
> 	for (MyLayer workLayer : workLayers) {
> 		String layerName = workLayer.getLayerNameI();
> 		Integer oldPosition = layerPositions.get(layerName);
> 		int currentPosition = getLayerPostion(flayers, layerName);
> 		if (oldPosition != null) {
> 			flayers.moveTo(numLayers - currentPosition, numLayers -
>                         oldPosition.intValue());
> 		}
> 	}
>
>
> and the function getLayerPostion():
>
>        private static int getLayerPostion(FLayers flayers, String layerName) {
> 		for (int i = 0; i < flayers.getLayersCount(); i++) {
> 			if (layerName.equals(flayers.getLayer(i).getName()))
> 				return i;
> 		}
> 		return -1;
> 	}
>
>
> Any help?
> _______________________________________________
> Gvsig_internacional mailing list
> Gvsig_internacional at listserv.gva.es
> http://listserv.gva.es/cgi-bin/mailman/listinfo/gvsig_internacional
>   



More information about the Gvsig_internacional mailing list