@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;
  }
  public void preApply() {
    // collect new info for the active region
    double n = Double.parseDouble(northText.getText());
    double s = Double.parseDouble(southText.getText());
    double w = Double.parseDouble(westText.getText());
    double e = Double.parseDouble(eastText.getText());
    double xr = Double.parseDouble(xresText.getText());
    double yr = Double.parseDouble(yresText.getText());

    // write the region to file
    JGrassRegion newRegion = new JGrassRegion(w, e, s, n, xr, yr);
    try {
      File mapsetFile = new File(style.windPath).getParentFile();
      File locationFile = mapsetFile.getParentFile();
      JGrassRegion.writeWINDToMapset(mapsetFile.getAbsolutePath(), newRegion);
      CoordinateReferenceSystem locationCrs =
          JGrassCatalogUtilities.getLocationCrs(locationFile.getAbsolutePath());
      style.fAlpha = Float.parseFloat(forgroundAlphaText.getText());
      style.bAlpha = Float.parseFloat(backgroundAlphaText.getText());
      RGB bg = backgroundColour.getColorValue();
      style.backgroundColor = new Color(bg.red, bg.green, bg.blue);
      bg = foregroundColor.getColorValue();
      style.foregroundColor = new Color(bg.red, bg.green, bg.blue);
      style.doGrid = gridButton.getSelection();

      commitToBlackboards(newRegion, locationCrs, style.windPath);
      super.preApply();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
  }
Ejemplo n.º 3
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());
    }
  }
Ejemplo n.º 4
0
  public static void copyToNewMapset(String mapsetName, String... maps) {
    String originalMapsetPath = p_locationPath + File.separator + p_mapsetName;
    String mapsetPath = p_locationPath + File.separator + mapsetName;

    File f = new File(mapsetPath);
    if (f.exists()) {
      p_err.println(
          "The mapset already exists. Can't export to an existing mapset. Choose a different name.");
      return;
    }

    boolean createdMapset =
        JGrassCatalogUtilities.createMapset(p_locationPath, mapsetName, null, null);
    if (!createdMapset) {
      p_err.println(
          "An error occurred while creating the new mapset structure. Check your permissions.");
    }

    StringBuilder warnings = new StringBuilder();
    monitor.beginTask("Copy maps...", maps.length);
    for (String mapName : maps) {
      monitor.worked(1);
      String[] originalMapsPath = JGrassUtilities.filesOfRasterMap(originalMapsetPath, mapName);
      String[] copiedMapsPath = JGrassUtilities.filesOfRasterMap(mapsetPath, mapName);

      for (int i = 0; i < originalMapsPath.length; i++) {
        File orig = new File(originalMapsPath[i]);
        if (!orig.exists()) {
          warnings.append("\nWarning: The following file didn't exist: " + originalMapsPath[i]);
          continue;
        }
        if (orig.isDirectory()) {
          continue;
        }
        File copiedParent = new File(copiedMapsPath[i]).getParentFile();
        if (!copiedParent.exists()) {
          copiedParent.mkdirs();
        }
        FileUtilities.copyFile(originalMapsPath[i], copiedMapsPath[i]);
      }
    }
    monitor.done();
    p_out.println(warnings.toString());
  }
 /**
  * Reads a {@link GridCoverage2D} from the grass raster.
  *
  * @param jGrassMapEnvironment the {@link JGrassMapEnvironment}.
  * @param readRegion the region to read, or <code>null</code>.
  * @return the read coverage.
  * @throws Exception
  */
 public static GridCoverage2D getGridcoverageFromGrassraster(
     JGrassMapEnvironment jGrassMapEnvironment, JGrassRegion readRegion) throws Exception {
   CoordinateReferenceSystem crs = jGrassMapEnvironment.getCoordinateReferenceSystem();
   JGrassRegion jGrassRegion = readRegion;
   if (jGrassRegion == null) jGrassRegion = jGrassMapEnvironment.getActiveRegion();
   GeneralParameterValue[] readParams =
       JGrassCatalogUtilities.createGridGeometryGeneralParameter(
           jGrassRegion.getCols(),
           jGrassRegion.getRows(),
           jGrassRegion.getNorth(),
           jGrassRegion.getSouth(),
           jGrassRegion.getWest(),
           jGrassRegion.getEast(),
           crs);
   AbstractGridFormat format =
       (AbstractGridFormat) new GrassCoverageFormatFactory().createFormat();
   GridCoverageReader reader = format.getReader(jGrassMapEnvironment.getCELL());
   GridCoverage2D mapCoverage = ((GridCoverage2D) reader.read(readParams));
   return mapCoverage;
 }
  public static void writeGridCoverageFromGrassraster(
      File mapFile, JGrassRegion writeRegion, GridCoverage2D grassCoverage) throws Exception {
    JGrassMapEnvironment mapEnvironment = new JGrassMapEnvironment(mapFile);
    GrassCoverageFormat format = new GrassCoverageFormatFactory().createFormat();
    GridCoverageWriter writer = format.getWriter(mapEnvironment.getCELL(), null);

    GeneralParameterValue[] readParams = null;
    if (writeRegion == null) {
      writeRegion = mapEnvironment.getActiveRegion();
    }
    readParams =
        JGrassCatalogUtilities.createGridGeometryGeneralParameter(
            writeRegion.getCols(),
            writeRegion.getRows(),
            writeRegion.getNorth(),
            writeRegion.getSouth(),
            writeRegion.getEast(),
            writeRegion.getWest(),
            mapEnvironment.getCoordinateReferenceSystem());

    writer.write(grassCoverage, readParams);
  }
Ejemplo n.º 7
0
  public void render(Graphics2D g2d, IProgressMonitor monitor) throws RenderException {
    try {
      final IRenderContext currentContext = getContext();
      currentContext.setStatus(ILayer.WAIT);
      CoordinateReferenceSystem destinationCRS = currentContext.getCRS();

      // the bounds of the visible area in world coordinates
      // get the envelope and the screen extent
      Envelope envelope = getRenderBounds();
      if (envelope == null || envelope.isNull()) {
        envelope = context.getImageBounds();
      }

      Point upperLeft =
          currentContext.worldToPixel(new Coordinate(envelope.getMinX(), envelope.getMinY()));
      Point bottomRight =
          currentContext.worldToPixel(new Coordinate(envelope.getMaxX(), envelope.getMaxY()));
      Rectangle screenSize = new Rectangle(upperLeft);
      screenSize.add(bottomRight);

      final IGeoResource resource = getContext().getGeoResource();
      if (resource == null || !resource.canResolve(JGrassMapGeoResource.class)) {
        return;
      }
      JGrassMapGeoResource grassMapGeoResource =
          resource.resolve(JGrassMapGeoResource.class, monitor);

      JGrassRegion fileWindow = new JGrassRegion(grassMapGeoResource.getFileWindow());
      JGrassMapsetGeoResource parent =
          (JGrassMapsetGeoResource) grassMapGeoResource.parent(new NullProgressMonitor());
      CoordinateReferenceSystem grassCrs = parent.getLocationCrs();
      JGrassRegion screenDrawWindow =
          new JGrassRegion(
              envelope.getMinX(),
              envelope.getMaxX(),
              envelope.getMinY(),
              envelope.getMaxY(),
              fileWindow.getRows(),
              fileWindow.getCols());

      // to intersect with the data window, we transform the screen window
      JGrassRegion reprojectedScreenDrawWindow = screenDrawWindow;
      if (!CRS.equalsIgnoreMetadata(destinationCRS, grassCrs)) {
        reprojectedScreenDrawWindow = screenDrawWindow.reproject(destinationCRS, grassCrs, true);
      }

      /*
       * if the map is not visible, do not render it
       */
      // JGrassRegion fileWindow = grassMapGeoResource.getFileWindow();
      Rectangle2D.Double fileRectDouble = fileWindow.getRectangle();
      Double reprojScreenRectangle = reprojectedScreenDrawWindow.getRectangle();
      if (!reprojScreenRectangle.intersects(fileRectDouble)) {
        getContext().setStatus(ILayer.DONE);
        getContext().setStatusMessage(THE_MAP_IS_OUTSIDE_OF_THE_VISIBLE_REGION);
        System.out.println(THE_MAP_IS_OUTSIDE_OF_THE_VISIBLE_REGION);
        return;
      }
      /*
       * we will draw only the intersection of the map in the display system = part of visible map
       */
      Rectangle2D drawMapRectangle =
          reprojectedScreenDrawWindow.getRectangle().createIntersection(fileRectDouble);
      // Rectangle2D drawMapRectangle = fileRectDouble.getBounds2D();
      // resolution is that of the file window
      double ewRes = fileWindow.getWEResolution();
      double nsRes = fileWindow.getNSResolution();
      if (fileRectDouble.getWidth() < ewRes || fileRectDouble.getHeight() < nsRes) {
        getContext().setStatus(ILayer.DONE);
        getContext().setStatusMessage(THE_MAP_IS_OUTSIDE_OF_THE_VISIBLE_REGION);
        System.out.println(THE_MAP_IS_OUTSIDE_OF_THE_VISIBLE_REGION);
        return;
      }
      MathTransform transform = CRS.findMathTransform(destinationCRS, grassCrs, true);
      Coordinate pixelSize = getContext().getPixelSize();

      Coordinate c1 = new Coordinate(envelope.getMinX(), envelope.getMinY());
      Coordinate c2 =
          new Coordinate(envelope.getMinX() + pixelSize.x, envelope.getMinY() + pixelSize.y);
      Envelope envy = new Envelope(c1, c2);
      Envelope envyTrans = JTS.transform(envy, transform);

      pixelSize = new Coordinate(envyTrans.getWidth(), envyTrans.getHeight());
      /*
       * if the resolution is higher of that of the screen, it doesn't make much sense to draw it
       * all. So for visualization we just use the screen resolution to do things faster.
       */
      if (ewRes < pixelSize.x) {
        ewRes = pixelSize.x;
      }
      if (nsRes < pixelSize.y) {
        nsRes = pixelSize.y;
      }
      fileWindow.setNSResolution(nsRes);
      fileWindow.setWEResolution(ewRes);
      nsRes = fileWindow.getNSResolution();
      ewRes = fileWindow.getWEResolution();
      /*
       * redefine the region of the map to be drawn
       */
      /*
       * snap the screen to fit into the active region grid. This is mandatory for the exactness
       * of the query of the pixels (ex. d.what.rast).
       */
      JGrassRegion activeWindow = grassMapGeoResource.getActiveWindow();
      Coordinate minXY =
          JGrassRegion.snapToNextHigherInRegionResolution(
              drawMapRectangle.getMinX(), drawMapRectangle.getMinY(), activeWindow);
      Coordinate maxXY =
          JGrassRegion.snapToNextHigherInRegionResolution(
              drawMapRectangle.getMaxX(), drawMapRectangle.getMaxY(), activeWindow);

      JGrassRegion drawMapRegion =
          new JGrassRegion(minXY.x, maxXY.x, minXY.y, maxXY.y, ewRes, nsRes);
      // JGrassRegion drawMapRegion = new JGrassRegion(drawMapRectangle.getMinX(),
      // drawMapRectangle.getMaxX(), drawMapRectangle.getMinY(), drawMapRectangle
      // .getMaxY(), ewRes, nsRes);
      JGrassMapEnvironment grassMapEnvironment = grassMapGeoResource.getjGrassMapEnvironment();
      GridCoverage2D coverage =
          JGrassCatalogUtilities.getGridcoverageFromGrassraster(grassMapEnvironment, drawMapRegion);
      coverage = coverage.view(ViewType.RENDERED);
      if (coverage != null) {

        // setting rendering hints
        RenderingHints hints = new RenderingHints(Collections.EMPTY_MAP);
        hints.add(
            new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED));
        hints.add(
            new RenderingHints(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE));
        hints.add(
            new RenderingHints(
                RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF));
        hints.add(new RenderingHints(JAI.KEY_INTERPOLATION, new InterpolationNearest()));
        g2d.addRenderingHints(hints);
        final TileCache tempCache = JAI.createTileCache();
        tempCache.setMemoryCapacity(16 * 1024 * 1024);
        tempCache.setMemoryThreshold(1.0f);
        hints.add(new RenderingHints(JAI.KEY_TILE_CACHE, tempCache));

        // draw

        AffineTransform worldToScreen =
            RendererUtilities.worldToScreenTransform(envelope, screenSize, destinationCRS);
        final GridCoverageRenderer paint =
            new GridCoverageRenderer(destinationCRS, envelope, screenSize, worldToScreen, hints);
        RasterSymbolizer rasterSymbolizer =
            CommonFactoryFinder.getStyleFactory(null).createRasterSymbolizer();

        paint.paint(g2d, coverage, rasterSymbolizer);

        tempCache.flush();

        // IBlackboard blackboard = context.getMap().getBlackboard();
        // String legendString = coverageReader.getLegendString();
        // String name = grassMapGeoResource.getTitle();
        // blackboard.putString(JGrassMapGeoResource.READERID + "#" + name, legendString);

      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      getContext().setStatus(ILayer.DONE);
      getContext().setStatusMessage(null);
    }
  }