Package org.locationtech.udig.catalog.jgrass.core

Examples of org.locationtech.udig.catalog.jgrass.core.JGrassMapGeoResource


        IMap map = ApplicationGIS.getActiveMap();

        LayerFactory layerFactory = map.getLayerFactory();
        for( IGeoResource resource : resources ) {
            if (resource instanceof JGrassMapGeoResource) {
                JGrassMapGeoResource grassMGR = (JGrassMapGeoResource) resource;
                File locationFile = grassMGR.getLocationFile();
                File mapsetFile = grassMGR.getMapsetFile();
                File mapFile = grassMGR.getMapFile();
                IGeoResource addedMapToCatalog = JGrassCatalogUtilities.addMapToCatalog(locationFile.getAbsolutePath(),
                        mapsetFile.getName(), mapFile.getName(), JGrassConstants.GRASSBINARYRASTERMAP);
                int index = map.getMapLayers().size();
                ApplicationGIS.addLayersToMap(map, Arrays.asList(addedMapToCatalog), index);
            } else {
View Full Code Here


                maps.clear();
                final List< ? > toList = selection.toList();
                try {
                    for( Object object : toList ) {
                        if (object instanceof JGrassMapGeoResource) {
                            JGrassMapGeoResource mr = (JGrassMapGeoResource) object;
                            maps.add(mr);
                        }
                    }

                    Dialog dialog = new Dialog(shell){
                        private Text locNameText;
                        private Text mapsetNameText;
                        private Text crsText;
                        private Text xresText;
                        private Text yresText;

                        protected Control createDialogArea( Composite maxparent ) {
                            Composite parent = new Composite(maxparent, SWT.None);
                            parent.setLayout(new GridLayout());
                            parent.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL
                                    | GridData.GRAB_VERTICAL));

                            // the location name group
                            Group locNameGroup = new Group(parent, SWT.None);
                            locNameGroup.setLayout(new GridLayout(2, false));
                            locNameGroup.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL
                                    | GridData.GRAB_VERTICAL));
                            locNameGroup.setText("new location name");

                            locNameText = new Text(locNameGroup, SWT.BORDER);
                            locNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL
                                    | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_CENTER));
                            locNameText.setText("newLocation");

                            // the mapset name group
                            Group mapsetNameGroup = new Group(parent, SWT.None);
                            mapsetNameGroup.setLayout(new GridLayout(2, false));
                            mapsetNameGroup.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL
                                    | GridData.GRAB_VERTICAL));
                            mapsetNameGroup.setText("new mapset name");

                            mapsetNameText = new Text(mapsetNameGroup, SWT.BORDER);
                            mapsetNameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL
                                    | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_CENTER));
                            mapsetNameText.setText("newMapset");

                            // the crs choice group
                            Group crsGroup = new Group(parent, SWT.None);
                            crsGroup.setLayout(new GridLayout(2, false));
                            crsGroup.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL
                                    | GridData.GRAB_VERTICAL));
                            crsGroup.setText("choose the coordinate reference system for the new location");

                            crsText = new Text(crsGroup, SWT.BORDER);
                            crsText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL
                                    | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_CENTER));
                            crsText.setEditable(false);

                            final Button crsButton = new Button(crsGroup, SWT.BORDER);
                            crsButton.setText(" Choose CRS ");
                            crsButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter(){
                                public void widgetSelected( org.eclipse.swt.events.SelectionEvent e ) {
                                    final ChooseCoordinateReferenceSystemDialog crsChooser = new ChooseCoordinateReferenceSystemDialog();
                                    crsChooser.open(new Shell(Display.getDefault()));
                                    CoordinateReferenceSystem readCrs = crsChooser.getCrs();
                                    if (readCrs == null)
                                        return;
                                    crsText.setText(readCrs.getName().toString());
                                    crsText.setData(readCrs);
                                }
                            });

                            // the location name group
                            Group resolutionGroup = new Group(parent, SWT.None);
                            resolutionGroup.setLayout(new GridLayout(2, false));
                            resolutionGroup.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL
                                    | GridData.GRAB_VERTICAL));
                            resolutionGroup.setText("output map resolution");

                            String res = "";
                            if (maps.size() > 0) {
                                try {
                                    JGrassRegion activeWindow = maps.get(0).getActiveWindow();
                                    res = String.valueOf(activeWindow.getNSResolution());
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                            }
                            Label xresLabel = new Label(resolutionGroup, SWT.NONE);
                            xresLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
                            xresLabel.setText("X resolution");

                            xresText = new Text(resolutionGroup, SWT.BORDER);
                            xresText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL
                                    | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_CENTER));
                            xresText.setText(res);

                            Label yresLabel = new Label(resolutionGroup, SWT.NONE);
                            yresLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
                            yresLabel.setText("Y resolution");

                            yresText = new Text(resolutionGroup, SWT.BORDER);
                            yresText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL
                                    | GridData.GRAB_VERTICAL | GridData.VERTICAL_ALIGN_CENTER));
                            yresText.setText(res);

                            return parent;
                        }

                        protected void okPressed() {
                            locationName = locNameText.getText();
                            if (locationName == null || locationName.length() < 1) {
                                locationName = "newLocation";
                            }
                            mapsetName = mapsetNameText.getText();
                            if (mapsetName == null || mapsetName.length() < 1) {
                                mapsetName = "newMapset";
                            }
                            xRes = xresText.getText();
                            yRes = yresText.getText();
                            Object crsData = crsText.getData();
                            if (crsData instanceof CoordinateReferenceSystem) {
                                crs = (CoordinateReferenceSystem) crsData;
                            }
                            super.okPressed();
                        }
                    };
                    dialog.setBlockOnOpen(true);
                    open = dialog.open();

                } catch (Exception e) {
                    String message = "An error occurred while exporting the maps.";
                    ExceptionDetailsDialog.openError("ERROR", message, IStatus.ERROR, JGrassPlugin.PLUGIN_ID, e);
                }

            }
        });

        if (open == SWT.CANCEL) {
            return;
        }
        /*
         * run with backgroundable progress monitoring
         */
        IRunnableWithProgress operation = new IRunnableWithProgress(){

            public void run( IProgressMonitor monitor ) throws InvocationTargetException, InterruptedException {
                if (locationName == null || mapsetName == null || crs == null || maps.size() < 1) {
                    MessageBox msgBox = new MessageBox(shell, SWT.ICON_ERROR);
                    msgBox.setMessage("An error occurred in processing the user supplied data.");
                    msgBox.open();
                    return;
                }
                /*
                 * finally do some processing
                 */
                int mapNum = maps.size();
                monitor.beginTask("Reprojecting maps to new Location...", mapNum);
                for( int i = 0; i < mapNum; i++ ) {
                    JGrassMapGeoResource tmpMap = maps.get(i);
                    JGrassMapEnvironment jgMEnv = new JGrassMapEnvironment(tmpMap.getMapFile());

                    try {
                        /*
                         * TODO get envelope from original region and reproject it.
                         * then adapt it to the output resolution and
                         * finally reproject the map on the new grid.
                         */
                        JGrassRegion sourceRegion = jgMEnv.getActiveRegion();
                        CoordinateReferenceSystem sourceCrs = jgMEnv.getCoordinateReferenceSystem();
                        com.vividsolutions.jts.geom.Envelope sourceEnvelope = sourceRegion.getEnvelope();
                        MathTransform tr = CRS.findMathTransform(sourceCrs, crs);
                        com.vividsolutions.jts.geom.Envelope outputEnvelope = JTS.transform(sourceEnvelope, tr);

                        double west = outputEnvelope.getMinX();
                        double east = outputEnvelope.getMaxX();
                        double south = outputEnvelope.getMinY();
                        double north = outputEnvelope.getMaxY();

                        double xResolution = Double.parseDouble(xRes);
                        double yResolution = Double.parseDouble(yRes);
                        Coordinate tmp1 = new Coordinate(west, south);
                        Coordinate tmp2 = new Coordinate(west + xResolution, south + yResolution);
                        JTS.transform(tmp1, tmp1, tr);
                        JTS.transform(tmp2, tmp2, tr);
                        xResolution = tmp2.x - tmp1.x;
                        yResolution = tmp2.y - tmp1.y;

                        // if in metric, snap to integer bounds
                        int intWest = (int) Math.floor(west);
                        int intSouth = (int) Math.floor(south);
                        if (west - intWest < xResolution && south - intSouth < yResolution) {
                            west = intWest;
                            south = intSouth;
                        }

                        double w = east - west;
                        double h = north - south;
                        // modify the envelope to be in the requested resolution
                        double cols = Math.floor(w / xResolution) + 1.0;
                        double rows = Math.floor(h / yResolution) + 1.0;

                        double newEast = west + cols * xResolution;
                        double newNorth = south + rows * yResolution;

                        ReferencedEnvelope referencedEnvelope = new ReferencedEnvelope(west, newEast, south, newNorth, crs);

                        GridToEnvelopeMapper g2eMapper = new GridToEnvelopeMapper();
                        g2eMapper.setEnvelope(referencedEnvelope);
                        GridEnvelope2D gridEnvelope2D = new GridEnvelope2D(0, 0, (int) cols, (int) rows);
                        g2eMapper.setGridRange(gridEnvelope2D);
                        g2eMapper.setPixelAnchor(PixelInCell.CELL_CENTER);
                        MathTransform gridToEnvelopeTransform = g2eMapper.createTransform();

                        GridGeometry outputGridGeometry = new GridGeometry2D(gridEnvelope2D, gridToEnvelopeTransform, crs);

                        GridCoverage2D coverage2D = JGrassCatalogUtilities.getGridcoverageFromGrassraster(jgMEnv, sourceRegion);
                        // GrassCoverageReadParam gcReadParam = new
                        // GrassCoverageReadParam(sourceRegion);
                        GridCoverage2D reprojected = (GridCoverage2D) Operations.DEFAULT.resample(coverage2D, crs,
                                outputGridGeometry, Interpolation.getInstance(Interpolation.INTERP_BICUBIC));

                        JGrassRegion jgRegion = new JGrassRegion(west, newEast, south, newNorth, xResolution, yResolution);

                        // GridCoverage2D coverage2D = tmp.read(null);
                        // GridCoverage2D reprojected = (GridCoverage2D)
                        // Operations.DEFAULT.resample(
                        // coverage2D, crs);
                        // Envelope2D envelope2D = reprojected.getEnvelope2D();
                        // JGrassRegion jgRegion = new JGrassRegion(envelope2D);

                        if (i == 0) {
                            // create the location structure
                            File grassDbFile = jgMEnv.getLOCATION().getParentFile();
                            File newLocationFile = new File(grassDbFile, locationName);
                            try {
                                JGrassCatalogUtilities.createLocation(newLocationFile.getAbsolutePath(), crs, jgRegion);
                                JGrassCatalogUtilities
                                        .createMapset(newLocationFile.getAbsolutePath(), mapsetName, null, jgRegion);
                            } catch (Exception e) {
                                // ignore this for now
                            }
                            mapsetFile = new File(newLocationFile, mapsetName);
                        }
                        File newMapFile = new File(mapsetFile, JGrassConstants.CELL + File.separator
                                + tmpMap.getMapFile().getName());

                        JGrassCatalogUtilities.writeGridCoverageFromGrassraster(newMapFile, jgRegion, reprojected);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
View Full Code Here

        // 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();
        }
View Full Code Here

        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;
    }
View Full Code Here

                        try {
                            pm.beginTask("Duplicating map...", IProgressMonitor.UNKNOWN);
                            pm.worked(1);

                            if (object instanceof JGrassMapGeoResource) {
                                JGrassMapGeoResource mr = (JGrassMapGeoResource) object;
                                File oldMapFile = mr.getMapFile();
                                String oldMapName = oldMapFile.getName();

                                InputDialog iDialog = new InputDialog(shell, "New map name",
                                        "Please enter the new name for the map: " + oldMapName, oldMapName + "_new", null);
                                iDialog.open();
View Full Code Here

            itemLayers = new ArrayList<IGeoResource>();
            for( String name : itemNames ) {
                ILayer tmpLayer = itemsMap.get(name);
                if (tmpLayer != null) {
                    if (tmpLayer.getGeoResource().canResolve(JGrassMapGeoResource.class)) {
                        JGrassMapGeoResource rasterMapResource = null;
                        try {
                            rasterMapResource = tmpLayer.getGeoResource().resolve(JGrassMapGeoResource.class, null);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
View Full Code Here

                                    || (layer.getGeoResource().resolve(JGrassMapGeoResource.class, null)).getType().equals(
                                            JGrassConstants.FTRASTERMAP) || (layer.getGeoResource().resolve(
                                    JGrassMapGeoResource.class, null)).getType().equals(JGrassConstants.ESRIRASTERMAP)))

                    {
                        JGrassMapGeoResource mapResolve = layer.getGeoResource().resolve(JGrassMapGeoResource.class,
                                new NullProgressMonitor());
                        IResolve mapsetResolve = mapResolve.parent(new NullProgressMonitor());
                        // check for the limiting mapset
                        if (mapsetPath != null && mapsetResolve instanceof JGrassMapsetGeoResource) {
                            JGrassMapsetGeoResource map = (JGrassMapsetGeoResource) mapsetResolve;
                            File refFile = map.getFile();
                            File mapsetFile = new File(mapsetPath);
View Full Code Here

                        try {
                            pm.beginTask("Renaming map...", IProgressMonitor.UNKNOWN);
                            pm.worked(1);
                            if (map instanceof JGrassMapGeoResource) {
                                JGrassMapGeoResource mr = (JGrassMapGeoResource) map;
                                String type = mr.getType();
                                if (type.equals(JGrassConstants.GRASSBINARYRASTERMAP)) {
                                    String[] mapsetpathAndMapname = JGrassCatalogUtilities
                                            .getMapsetpathAndMapnameFromJGrassMapGeoResource(mr);
                                    String oldMapName = mapsetpathAndMapname[1];
                                    String mapsetName = mapsetpathAndMapname[0];
                                    try {
                                        InputDialog iDialog = new InputDialog(Display.getDefault().getActiveShell(),
                                                "New map name", "Please enter the new name for the map: " + oldMapName, "new_"
                                                        + oldMapName, null);
                                        iDialog.open();
                                        String newMapName = iDialog.getValue();
                                        if (newMapName.indexOf(' ') != -1) {
                                            MessageBox msgBox = new MessageBox(Display.getDefault().getActiveShell(),
                                                    SWT.ICON_ERROR);
                                            msgBox.setMessage("Map names can't contain spaces. Please choose a name without spaces.");
                                            msgBox.open();
                                            newMapName = null;
                                            return;
                                        }

                                        if (newMapName != null && newMapName.length() > 0) {
                                            // remove map from layer view
                                            IMap activeMap = ApplicationGIS.getActiveMap();
                                            List<ILayer> mapLayers = activeMap.getMapLayers();
                                            List<ILayer> toRemove = new ArrayList<ILayer>();
                                            for( int i = 0; i < mapLayers.size(); i++ ) {
                                                String layerName = mapLayers.get(i).getName();
                                                if (layerName.equals(oldMapName)) {
                                                    // remove it from layer list
                                                    toRemove.add(mapLayers.get(i));
                                                }

                                            }
                                            if (toRemove.size() > 0)
                                                activeMap.sendCommandSync(new DeleteLayersCommand((ILayer[]) toRemove
                                                        .toArray(new ILayer[toRemove.size()])));

                                            // rename the map
                                            renameGrassRasterMap(mapsetName, oldMapName, newMapName);
                                            // remove old map
                                            ((JGrassMapsetGeoResource) mr.parent(new NullProgressMonitor())).removeMap(
                                                    oldMapName, JGrassConstants.GRASSBINARYRASTERMAP);
                                            // add new name map
                                            ((JGrassMapsetGeoResource) mr.parent(new NullProgressMonitor())).addMap(newMapName,
                                                    JGrassConstants.GRASSBINARYRASTERMAP);
                                        }

                                    } catch (Exception e) {
                                        MessageDialog.openInformation(shell, "Information",
View Full Code Here

        if (updatedObject instanceof List) {
            String text = null;
            List layers = (List) updatedObject;
            for( Object layer : layers ) {
                if (layer instanceof JGrassMapGeoResource) {
                    JGrassMapGeoResource rasterMapResource = (JGrassMapGeoResource) layer;
                    try {
                        text = rasterMapResource.getInfo(null).getName();

                        JGrassRegion fileWindow = rasterMapResource.getFileWindow();
                        if (fileWindow != null) {
                            setWidgetsToWindow(fileWindow);
                        }
                    } catch (IOException e1) {
                        return;
View Full Code Here

                        try {
                            pm.beginTask("Exporting maps...", toList.size());

                            for( Object object : toList ) {
                                if (object instanceof JGrassMapGeoResource) {
                                    JGrassMapGeoResource mr = (JGrassMapGeoResource) object;
                                    File mapFile = mr.getMapFile();
                                    JGrassMapEnvironment mapEnvironment = new JGrassMapEnvironment(mapFile);
                                    JGrassRegion jGrassRegion = mapEnvironment.getActiveRegion();

                                    GeneralParameterValue[] readParams = createGridGeometryGeneralParameter(
                                            jGrassRegion.getCols(), jGrassRegion.getRows(), jGrassRegion.getNorth(),
View Full Code Here

TOP

Related Classes of org.locationtech.udig.catalog.jgrass.core.JGrassMapGeoResource

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.