@Override
  public boolean performFinish() {
    // TODO create a nice workspace with the properties
    for (String m : props.mapsets) {
      System.out.println(m);
    }
    System.out.println(props.locationPath);
    System.out.println(props.north);
    System.out.println(props.xres);
    System.out.println(props.crs.getName().toString());

    try {
      JGrassRegion window =
          new JGrassRegion(
              props.west, props.east, props.south, props.north, props.xres, props.yres);
      JGrassCatalogUtilities.createLocation(props.locationPath, props.crs, window);
      for (String mapset : props.mapsets) {
        JGrassCatalogUtilities.createMapset(props.locationPath, mapset, null, null);
        // set the WIND file
        String mapsetPath = props.locationPath + File.separator + mapset;
        JGrassRegion.writeWINDToMapset(mapsetPath, window);
      }

      JGrassCatalogUtilities.addServiceToCatalog(
          props.locationPath + File.separator + JGrassCatalogUtilities.JGRASS_WORKSPACE_FILENAME,
          new NullProgressMonitor());
    } catch (IOException e) {
      JGrassPlugin.log(
          "JGrassPlugin problem: eu.hydrologis.udig.catalog.workspacecreation.wizard#NewJGrassLocationWizard#performFinish",
          e); //$NON-NLS-1$
      e.printStackTrace();
    }

    return true;
  }
  /**
   * Returns a mapgraphics layer by its class.
   *
   * @param theClass the class of the mapgraphics.
   * @return the layer or null, if none was found (not visible or not existing).
   */
  public static ILayer getMapgraphicLayerByClass(Class<?> theClass) {

    try {
      List<IResolve> mapgraphics =
          CatalogPlugin.getDefault().getLocalCatalog().find(MapGraphicService.SERVICE_URL, null);
      List<IResolve> members = mapgraphics.get(0).members(null);
      for (IResolve resolve : members) {
        if (resolve.canResolve(theClass)) {
          IGeoResource resolve2 = resolve.resolve(IGeoResource.class, null);
          String resName = resolve2.getInfo(new NullProgressMonitor()).getName();

          List<ILayer> mapLayers = ApplicationGIS.getActiveMap().getMapLayers();
          for (ILayer layer : mapLayers) {
            if (layer.getName().trim().equals(resName.trim())) {
              return layer;
            }
          }
        }
      }
    } catch (IOException e) {
      JGrassPlugin.log(
          "JGrassPlugin problem: eu.hydrologis.udig.catalog.utils#JGrassCatalogUtilities#getMapgraphicLayerByClass",
          e); //$NON-NLS-1$
      e.printStackTrace();
      return null;
    }
    return null;
  }
  /**
   * @param locationPath
   * @return the crs of the supplied location
   */
  public static synchronized CoordinateReferenceSystem getLocationCrs(String locationPath) {
    CoordinateReferenceSystem readCrs = null;
    String projWtkFilePath;
    BufferedReader crsReader = null;
    try {
      projWtkFilePath =
          locationPath
              + File.separator
              + JGrassConstants.PERMANENT_MAPSET
              + File.separator
              + JGrassConstants.PROJ_WKT;
      File projWtkFile = new File(projWtkFilePath);
      if (projWtkFile.exists()) {
        crsReader = new BufferedReader(new FileReader(projWtkFile));
        StringBuffer wtkString = new StringBuffer();
        String line = null;
        while ((line = crsReader.readLine()) != null) {
          wtkString.append(line.trim());
        }

        readCrs = CRS.parseWKT(wtkString.toString());
      }

    } catch (Exception e1) {
      JGrassPlugin.log(
          "JGrassPlugin problem: eu.hydrologis.udig.catalog.internal.jgrass#JGrassMapsetGeoResource#getJGrassCrs",
          e1); //$NON-NLS-1$

      e1.printStackTrace();
    } finally {
      try {
        if (crsReader != null) crsReader.close();
      } catch (IOException e) {
        JGrassPlugin.log(
            "JGrassPlugin problem: eu.hydrologis.udig.catalog.internal.jgrass#JGrassMapsetGeoResource#getJGrassCrs",
            e); //$NON-NLS-1$
        e.printStackTrace();
      }
    }
    return readCrs;
  }
  /**
   * Reload the service, to which the location refers to. This is ment to be used when new maps are
   * added inside a mapset and the catalog doesn't care to see them. So the tree has to be reloaded.
   *
   * @param locationPath the path to the affected location
   * @param monitor the progress monitor
   */
  public static void refreshJGrassService(String locationPath, final IProgressMonitor monitor) {

    System.out.println("Lock on locationPath = " + Thread.holdsLock(locationPath));
    synchronized (locationPath) {

      /*
       * if the catalog is active, refresh the location in the catalog window
       */
      ID id = null;
      if (JGrassPlugin.getDefault() != null) {
        File locationFile = new File(locationPath);
        try {
          id = new ID(locationFile.toURI().toURL());
        } catch (MalformedURLException e) {
          e.printStackTrace();
          return;
        }
      } else {
        return;
      }
      /*
       * test code to make the catalog understand that the map should be added
       */
      final ICatalog catalog = CatalogPlugin.getDefault().getLocalCatalog();
      final JGrassService originalJGrassService = catalog.getById(JGrassService.class, id, monitor);

      /*
       * create the same service
       */
      if (originalJGrassService == null) return;
      final URL ID = originalJGrassService.getIdentifier();
      Map<String, Serializable> connectionParams = originalJGrassService.getConnectionParams();
      IServiceFactory locator = CatalogPlugin.getDefault().getServiceFactory();
      final List<IService> rereadService = locator.acquire(ID, connectionParams);

      /*
       * replace the service
       */
      if (rereadService.size() > 0) {
        Runnable refreshCatalogRunner =
            new Runnable() {
              public void run() {
                final IService newJGrassService = rereadService.get(0);
                catalog.remove(originalJGrassService);
                catalog.add(newJGrassService);
              }
            };

        new Thread(refreshCatalogRunner).start();
      }
    }
  }
  /**
   * Extract mapset path and mapname from a georesource containing a JGrassMapGeoResource
   *
   * @param resource
   * @return a String array with mapset path and map name
   */
  public static synchronized String[] getMapsetpathAndMapnameFromJGrassMapGeoResource(
      IGeoResource resource) {
    String[] mapsetPathAndMapName = new String[2];

    // check that the underlying resource is a propertyservice
    if (resource == null || !resource.canResolve(JGrassMapGeoResource.class)) {
      return null;
    }
    JGrassMapGeoResource mapResource = null;
    try {
      mapResource = resource.resolve(JGrassMapGeoResource.class, null);
      // String tmp = jg.getInfo(null).getDescription();
      // if (!tmp.equals(JGrassConstants.GRASSBINARYRASTERMAP))
      // return null;
    } catch (IOException e) {
      JGrassPlugin.log(
          "JGrassPlugin problem: eu.hydrologis.udig.catalog.utils#JGrassCatalogUtilities#getMapsetpathAndMapnameFromJGrassMapGeoResource",
          e); //$NON-NLS-1$

      e.printStackTrace();
      return null;
    }

    JGrassMapsetGeoResource mapsetResource = null;
    try {
      mapsetResource = (JGrassMapsetGeoResource) mapResource.parent(null);
      mapsetPathAndMapName[0] = mapsetResource.getFile().getAbsolutePath();
      mapsetPathAndMapName[1] = mapResource.getTitle();
    } catch (IOException e) {
      JGrassPlugin.log(
          "JGrassPlugin problem: eu.hydrologis.udig.catalog.utils#JGrassCatalogUtilities#getMapsetpathAndMapnameFromJGrassMapGeoResource",
          e); //$NON-NLS-1$
      e.printStackTrace();
    }
    return mapsetPathAndMapName;
  }
Ejemplo n.º 6
0
  public static void load(String mapname) {
    if (JGrassPlugin.getDefault() != null) {
      JGrassMapGeoResource addedMap =
          JGrassCatalogUtilities.addMapToCatalog(
              p_locationPath, p_mapsetName, mapname, JGrassConstants.GRASSBINARYRASTERMAP);
      if (addedMap == null)
        p_err.println("An error occurred while trying to add the map to the catalog.");

      IMap activeMap = ApplicationGIS.getActiveMap();
      ApplicationGIS.addLayersToMap(
          activeMap,
          Collections.singletonList((IGeoResource) addedMap),
          activeMap.getMapLayers().size());
    }
  }
 /**
  * @param outputFile
  * @param addToCatalog
  * @param progressMonitor
  */
 public static synchronized void addServiceToCatalog(
     String serviceFile, IProgressMonitor progressMonitor) {
   try {
     URL fileUrl = new File(serviceFile).toURI().toURL();
     // add the service to the catalog and map
     IServiceFactory sFactory = CatalogPlugin.getDefault().getServiceFactory();
     ICatalog catalog = CatalogPlugin.getDefault().getLocalCatalog();
     List<IService> services = sFactory.createService(fileUrl);
     for (IService service : services) {
       catalog.add(service);
     }
   } catch (Exception e) {
     JGrassPlugin.log(
         "JGrassPlugin problem: eu.hydrologis.udig.catalog.utils#JGrassCatalogUtilities#addServiceToCatalog",
         e); //$NON-NLS-1$
     e.printStackTrace();
   }
 }
  /**
   * @param resource
   * @return the full path to the resource
   */
  public static synchronized String getDirectPathFromJGrassMapGeoResource(IGeoResource resource) {
    String mapPath = null;
    // check that the underlying resource is a propertyservice
    if (resource == null || !resource.canResolve(JGrassMapGeoResource.class)) {
      return null;
    }
    JGrassMapGeoResource jg = null;
    try {
      jg = resource.resolve(JGrassMapGeoResource.class, null);
    } catch (IOException e) {
      JGrassPlugin.log(
          "JGrassPlugin problem: eu.hydrologis.udig.catalog.utils#JGrassCatalogUtilities#getDirectPathFromJGrassMapGeoResource",
          e); //$NON-NLS-1$

      e.printStackTrace();
      return null;
    }

    mapPath = URLUtils.urlToFile(jg.getIdentifier()).getAbsolutePath();
    return mapPath;
  }
  public static boolean createMapset(
      String locationPath, String mapset, CoordinateReferenceSystem crs, JGrassRegion window) {
    String path = locationPath + File.separator + mapset;

    /* Create mapset directory */
    if (!(new File(path).mkdirs())) return false;

    if (mapset.equals(JGrassConstants.PERMANENT_MAPSET)) {
      /* Create blank DEFAULT_WIND and WIND files */
      try {
        if (window != null) {
          JGrassRegion.writeWINDToMapset(path, window);
          JGrassRegion.writeDEFAULTWINDToLocation(locationPath, window);
        } else {
          // create blank windows
          BufferedWriter out =
              new BufferedWriter(
                  new FileWriter(path + File.separator + JGrassConstants.DEFAULT_WIND));
          out.write(JGrassRegion.BLACKBOARD_KEY);
          out.close();

          out = new BufferedWriter(new FileWriter(path + File.separator + JGrassConstants.WIND));
          out.write(JGrassRegion.BLANK_REGION);
          out.close();
        }

        /* Create projection files */
        if (crs != null) {
          // FIXME create GRASS proj files

          BufferedWriter prjOut =
              new BufferedWriter(new FileWriter(path + File.separator + JGrassConstants.PROJ_WKT));
          prjOut.write(crs.toWKT());
          prjOut.close();
        }
      } catch (IOException e) {
        JGrassPlugin.log(
            "JGrassPlugin problem: eu.hydrologis.udig.catalog.utils#JGrassCatalogUtilities#createMapset",
            e);
        e.printStackTrace();
        return false;
      }

    } else {
      /* Copy WIND file from PERMANENT mapset of this location */
      try {
        BufferedReader in =
            new BufferedReader(
                new FileReader(
                    locationPath
                        + File.separator
                        + JGrassConstants.PERMANENT_MAPSET
                        + File.separator
                        + JGrassConstants.DEFAULT_WIND));
        BufferedWriter out =
            new BufferedWriter(new FileWriter(path + File.separator + JGrassConstants.WIND));
        String line;
        while ((line = in.readLine()) != null) {
          out.write(line);
          out.write("\n");
        }
        out.close();
        in.close();
      } catch (IOException e) {
        JGrassPlugin.log(
            "JGrassPlugin problem: eu.hydrologis.udig.catalog.utils#JGrassCatalogUtilities#createMapset",
            e);
        e.printStackTrace();
        return false;
      }
    }

    /* Create point/site directories */
    if (!(new File(path + File.separator + JGrassConstants.SITE_LISTS).mkdirs())) return false;

    /* Create raster directories */
    if (!(new File(path + File.separator + JGrassConstants.FCELL).mkdirs())) return false;
    if (!(new File(path + File.separator + JGrassConstants.CELL).mkdirs())) return false;
    if (!(new File(path + File.separator + JGrassConstants.CELLHD).mkdirs())) return false;
    if (!(new File(path + File.separator + JGrassConstants.CATS).mkdirs())) return false;
    if (!(new File(path + File.separator + JGrassConstants.COLR).mkdirs())) return false;
    if (!(new File(path + File.separator + JGrassConstants.CELL_MISC).mkdirs())) return false;

    /* Create vector directories */
    if (!(new File(path + File.separator + JGrassConstants.DIG).mkdirs())) return false;
    if (!(new File(path + File.separator + JGrassConstants.DIG_ATTS).mkdirs())) return false;
    if (!(new File(path + File.separator + JGrassConstants.DIG_CATS).mkdirs())) return false;

    if (!createJGrassFolders(path)) return false;

    return true;
  }