[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
RE: [udig-devel] RE: Loading a Udig project programatically (MarioNu?ez Jimenez)
|
Hello Frederick,
i think it's a great help, i will read it carefully because it is something that i have to do after i finish loading layers programatically from a WMS, WFS and Postgis.
I will tell you about it when i try it.
Thank you very much!!
Kinds Regards.
Mario.
________________________________
De: udig-devel-bounces@xxxxxxxxxxxxxxxxxxxxx en nombre de Frederic Renard
Enviado el: lun 28/07/2008 10:07
Para: udig-devel@xxxxxxxxxxxxxxxxxxxxx
Asunto: [udig-devel] RE: Loading a Udig project programatically (MarioNu?ez Jimenez)
Hi Mario,
Here is my code: I open an existing project ,or create one if not found.
Then I check if new layers exists or are (un)authorized for a particular
user. So the user's layers are customisable by an administrator. List of
authorized layers comes from a properties files for test purpose.
the method 'LoadProject.createProject();' is called into
ApplicationWorkbenchWindowAdvisor.postWindowOpen()
the method 'LoadProject.UpdateProject();' is called into
MapOpenInterceptor.run(Map map)
I hope it helps.
Regards,
Frederick.
***************************
public class LoadProject {
private static boolean newproject=false; // when creating a new
project, 'UpdateProject' is also called at map opening. this key avoid
this. ( bad programming I know )
private LoadProject(){}
public static void UpdateProject() {
boolean projectChanged=false;
if(newproject)
return;
try {
ProjectRegistry projectRegistry =
ProjectPlugin.getPlugin().getProjectRegistry();
// get project
Project project
=projectRegistry.getCurrentProject();
project.setName("TranslogisticProject");
// get map
Map map1 = (Map)
project.getElements(Map.class).get(0);
IProgressMonitor monitor = new
NullProgressMonitor();
List<IService> services =
sodServices(sodUrlFactory());
// Add Layers
List<? extends IGeoResource>
localresources=null;
// Create list of authorized layers
String
autString=TelticProperties.getProp("AUTHORIZEDLAYERS").toLowerCase();
for (Iterator iterator = services.iterator();
iterator.hasNext();) {
IService service = (IService)
iterator.next();
localresources =
service.resources(monitor);
for (Iterator iterator2 =
localresources.iterator(); iterator2 .hasNext();) {
IGeoResource geoResource
= (IGeoResource) iterator2 .next();
// Get layer name:
suppress prefix like sod: or sod_
String
layerName=getLayerName(geoResource,monitor);
// Search into
authorized list if layer exist
if(autString.indexOf(
layerName )<0)
// not
authorized -> suppress from list
iterator2.remove();
}
// Add only inexisting layers
for (Iterator iterator2 =
localresources.iterator(); iterator2 .hasNext();) {
IGeoResource geoResource
= (IGeoResource) iterator2 .next();
String rlname =
getLayerName(geoResource, monitor);
List<Layer> layers =
map1.getLayersInternal();
for (Layer layer :
layers) {
String
layerName=getLayerName(layer.getGeoResource(), monitor);
if
(layerName.indexOf(rlname) >= 0){
//
already present into list -> suppress from list
iterator2.remove();
break;
}
}
}
// Add layers
if(localresources.size()>0){
AddLayersCommand alCommand =
new AddLayersCommand( localresources, 0);
map1.sendCommandASync(alCommand);
}
}
// Check if there is layers to suppress
List<Layer> layers = map1.getLayersInternal();
for (Layer layer : layers) {
if
(autString.indexOf(getLayerName(layer.getGeoResource(), monitor)) < 0) {
// delete layers
DeleteLayerCommand delCommand =
new DeleteLayerCommand(layer);
map1.sendCommandASync(delCommand);
}
}
// open project
OpenProjectElementCommand command=new
OpenProjectElementCommand(map1);
project.sendASync(command);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void createProject() {
try {
ProjectRegistry projectRegistry =
ProjectPlugin.getPlugin().getProjectRegistry();
ProjectFactory projectFactory =
ProjectFactory.eINSTANCE;
// create project
Project project
=projectRegistry.getCurrentProject();
// create map
if( project.getElements().size()>0)
// project already created : update
return;
Map map1 =
projectFactory.createMap(project,"Translogistic",new
ArrayList<Layer>());
IProgressMonitor monitor = new
NullProgressMonitor();
List<? extends IGeoResource>
localresources=null;
String
autString=TelticProperties.getProp("AUTHORIZEDLAYERS");
List<IService> services =
sodServices(sodUrlFactory());
for (Iterator iterator = services.iterator();
iterator.hasNext();) {
IService service = (IService)
iterator.next();
System.out.println(("open
service:"+service.getInfo(monitor).getTitle()));
localresources =
service.resources(monitor);
for (Iterator iterator2 =
localresources.iterator(); iterator2 .hasNext();) {
IGeoResource geoResource
= (IGeoResource) iterator2 .next();
String nm =
geoResource.getInfo(monitor).getName();
// Get layer name:
suppress prefix like sod: or sod_
String
layerName=getLayerName(geoResource,monitor);
// Search into
authorized list if layer exist
if(autString.toLowerCase().indexOf( layerName )<0)
// not
authorized -> suppress from list
iterator2.remove();
}
// Add layers
AddLayersCommand alCommand =
new AddLayersCommand( localresources, 0);
map1.executeSyncWithoutUndo(alCommand);
for (Iterator iterator2 =
localresources.iterator(); iterator2
.hasNext();) {
IGeoResource lrsrc =
(IGeoResource) iterator2.next();
System.out.println("Add
layer:"+lrsrc.getInfo(monitor).getName());
}
}
// open project
OpenProjectElementCommand command=new
OpenProjectElementCommand(map1);// ??? NOK with wms , needed with
shapefile!
project.sendASync(command);
newproject=true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private static List<IService> sodServices(List<URL> urls) {
IServiceFactory serviceFactory =
CatalogPlugin.getDefault()
.getServiceFactory();
List<IService> services = new LinkedList<IService>();
List<IService> servicesShape = new
LinkedList<IService>();
if (urls.size() <= 0)
return services;
for (URL url : urls) {
if(url.getFile().toLowerCase().indexOf(".shp")>0){
// SHAPEFILE URL
// System.out.println("create service
shape:"+serviceFactory.createService(url).get(1).getIdentifier());
servicesShape.add(serviceFactory.createService(url).get(0));
}
else
// OTHER URL
services=serviceFactory.createService(url);
}
for (Iterator iterator = servicesShape.iterator();
iterator.hasNext();) {
IService service = (IService) iterator.next();
try {
System.out.println("Service shape:
"+service.getIdentifier()+"/"+service.getInfo(new
NullProgressMonitor()).getTitle());
} catch (IOException e) {
}
}
services.addAll(servicesShape);
for (Iterator iterator = services.iterator();
iterator.hasNext();) {
IService service = (IService) iterator.next();
try {
System.out.println("Services:
"+service.getIdentifier()+"/"+service.getInfo(new
NullProgressMonitor()).getTitle());
} catch (IOException e) {
}
}
return services;
}
private static List<URL> sodUrlFactory() throws
MalformedURLException{
/* WMS URLS */
List<URL> urls = new ArrayList<URL>();
urls.add(MapGraphicService.SERVICE_URL);
try {
urls.add(new
URL(TelticProperties.getProp("CARTOSERVER")));
} catch (TlticException e) {}
/* SHAPEFILE URLS */
try {
String path = TelticProperties.getProp("SHAPEFILEDIR");
// get Shapefile list
if (new File(path).exists()) {
// String[] filenames = {
"sod_communes_bel_wgs84.shp",
// "sod_eu_country_wgs84.shp" };
SufixFilter shapefilesSufixFilter = new
SufixFilter(".shp");
File[] shapefiles = new File(path)
.listFiles(shapefilesSufixFilter);
for (int i = 0; i < shapefiles.length;
i++) {
urls.add(shapefiles[i].toURL());
}
}
} catch (TlticException e) {}
for (Iterator iterator = urls.iterator();
iterator.hasNext();) {
URL url = (URL) iterator.next();
System.out.println("URL="+url.toString());
}
return urls;
}
/*
* Get layer name: suppress prefix like sod: or sod_
*/
private static String getLayerName(IGeoResource geoResource,
IProgressMonitor monitor) throws TlticException, IOException{
String
shapefilePrefix=TelticProperties.getProp("SHAPEFILEPREFIX").toLowerCase(
);
String
wmsPrefix=TelticProperties.getProp("WMSPREFIX").toLowerCase();
String
layerName=geoResource.getInfo(monitor).getName().toLowerCase();
if ( layerName.startsWith(shapefilePrefix)){
layerName=layerName.split(shapefilePrefix)[1];
}
if ( layerName.startsWith(wmsPrefix)){
layerName=layerName.split(wmsPrefix)[1];
}
return layerName;
}
private static class SufixFilter implements FileFilter{
String afn;
SufixFilter(String afn) { this.afn = afn; }
public boolean accept(File arg0) {
if(arg0.getName().toLowerCase().endsWith(this.afn.toLowerCase()))
return true;
return false;
}
}
// OPEN MY OWN PROJECT ( FROM TEXT PROJECT FILE )
// ProjectRegistry registry =
ProjectPlugin.getPlugin().getProjectRegistry();
// Project project;
// String
prj=TelticProperties.getRootPath()+"\\Translogistic.product\\project.udi
g";
// project = registry.getProject(prj);
// project.getElements(IMap.class);
//
}
_______________________________________________
User-friendly Desktop Internet GIS (uDig)
http://udig.refractions.net <http://udig.refractions.net/>
http://lists.refractions.net/mailman/listinfo/udig-devel
------------------------------------------------------------------
This e-mail and the documents attached are confidential and intended solely
for the addressee; it may also be privileged. If you receive this e-mail
in error, please notify the sender immediately and destroy it.
As its integrity cannot be secured on the Internet, the Atos Origin group
liability cannot be triggered for the message content. Although the
sender endeavours to maintain a computer virus-free network, the sender does
not warrant that this transmission is virus-free and will not be liable for
any damages resulting from any virus transmitted.
Este mensaje y los ficheros adjuntos pueden contener informacion
confidencial destinada solamente a la(s) persona(s) mencionadas
anteriormente. Pueden estar protegidos por secreto profesional Si usted
recibe este correo electronico por error, gracias de informar inmediatamente
al remitente y destruir el mensaje.
Al no estar asegurada la integridad de este mensaje sobre la red, Atos
Origin no se hace responsable por su contenido. Su contenido no constituye
ningun compromiso para el grupo Atos Origin, salvo ratificacion escrita por
ambas partes.
Aunque se esfuerza al maximo por mantener su red libre de virus, el emisor
no puede garantizar nada al respecto y no sera responsable de cualesquiera
danos que puedan resultar de una transmision de virus
------------------------------------------------------------------