void ChippingWindow::createView() { if (mpChippingWidget == NULL) { return; } RasterElement* pRaster = getRasterElement(); if (pRaster == NULL) { return; } // Create the new raster element from the primary element of the source. // Note that this does not chip displayed elements if they differ from the primary element. // This causes a special case below where the stretch values are being applied to the chipped layer. RasterElement* pRasterChip = pRaster->createChip(pRaster->getParent(), "_chip", mpChippingWidget->getChipRows(), mpChippingWidget->getChipColumns(), mpChippingWidget->getChipBands()); if (pRasterChip == NULL) { QMessageBox::critical(this, windowTitle(), "Unable to create a new cube!"); return; } const RasterDataDescriptor* pDescriptor = dynamic_cast<const RasterDataDescriptor*>(pRasterChip->getDataDescriptor()); VERIFYNRV(pDescriptor != NULL); // Create a view for the new chip SpatialDataWindow* pWindow = dynamic_cast<SpatialDataWindow*>( Service<DesktopServices>()->createWindow(pRasterChip->getName(), SPATIAL_DATA_WINDOW)); if (pWindow == NULL) { return; } SpatialDataView* pView = pWindow->getSpatialDataView(); if (pView == NULL) { Service<DesktopServices>()->deleteWindow(pWindow); return; } UndoLock lock(pView); if (pView->setPrimaryRasterElement(pRasterChip) == false) { Service<DesktopServices>()->deleteWindow(pWindow); return; } // RasterLayerImp is needed for the call to setCurrentStretchAsOriginalStretch(). RasterLayerImp* pLayer = dynamic_cast<RasterLayerImp*>(pView->createLayer(RASTER, pRasterChip)); if (pLayer == NULL) { Service<DesktopServices>()->deleteWindow(pWindow); return; } string origName = pRaster->getName(); SpatialDataWindow* pOrigWindow = dynamic_cast<SpatialDataWindow*>( Service<DesktopServices>()->getWindow(origName, SPATIAL_DATA_WINDOW)); if (pOrigWindow != NULL) { SpatialDataView* pOrigView = pOrigWindow->getSpatialDataView(); if (pOrigView != NULL) { LayerList* pLayerList = pOrigView->getLayerList(); if (pLayerList != NULL) { RasterLayer* pOrigLayer = static_cast<RasterLayer*>(pLayerList->getLayer(RASTER, pRaster)); if (pOrigLayer != NULL) { // Set the stretch type first so that stretch values are interpreted correctly. pLayer->setStretchType(GRAYSCALE_MODE, pOrigLayer->getStretchType(GRAYSCALE_MODE)); pLayer->setStretchType(RGB_MODE, pOrigLayer->getStretchType(RGB_MODE)); pLayer->setDisplayMode(pOrigLayer->getDisplayMode()); // Set the properties of the cube layer in the new view. // For each channel, display the first band if the previously displayed band was chipped. vector<RasterChannelType> channels = StringUtilities::getAllEnumValues<RasterChannelType>(); for (vector<RasterChannelType>::const_iterator iter = channels.begin(); iter != channels.end(); ++iter) { bool bandCopied = true; DimensionDescriptor newBand; DimensionDescriptor oldBand = pOrigLayer->getDisplayedBand(*iter); if (oldBand.isOriginalNumberValid() == true) { newBand = pDescriptor->getOriginalBand(oldBand.getOriginalNumber()); } if (newBand.isValid() == false) { bandCopied = false; newBand = pDescriptor->getBands().front(); } // No need to explicitly set the RasterElement here since the new view only has one RasterElement. pLayer->setDisplayedBand(*iter, newBand); // Use the default stretch properties if the displayed band was removed from the view or // if the non-primary raster element was displayed. Otherwise, copy the stretch properties. if (bandCopied && pRaster == pOrigLayer->getDisplayedRasterElement(*iter)) { // Set the stretch units first so that stretch values are interpreted correctly. pLayer->setStretchUnits(*iter, pOrigLayer->getStretchUnits(*iter)); double lower; double upper; pOrigLayer->getStretchValues(*iter, lower, upper); pLayer->setStretchValues(*iter, lower, upper); } } pLayer->setCurrentStretchAsOriginalStretch(); pView->refresh(); } } } } // Create a GCP layer if (pRaster->isGeoreferenced() == true) { const vector<DimensionDescriptor>& rows = mpChippingWidget->getChipRows(); const vector<DimensionDescriptor>& columns = mpChippingWidget->getChipColumns(); if ((rows.empty() == false) && (columns.empty() == false)) { // Get the geocoordinates at the chip corners VERIFYNRV(rows.front().isActiveNumberValid() == true); VERIFYNRV(rows.back().isActiveNumberValid() == true); VERIFYNRV(columns.front().isActiveNumberValid() == true); VERIFYNRV(columns.back().isActiveNumberValid() == true); unsigned int startRow = rows.front().getActiveNumber(); unsigned int endRow = rows.back().getActiveNumber(); unsigned int startCol = columns.front().getActiveNumber(); unsigned int endCol = columns.back().getActiveNumber(); GcpPoint ulPoint; ulPoint.mPixel = LocationType(startCol, startRow); ulPoint.mCoordinate = pRaster->convertPixelToGeocoord(ulPoint.mPixel); GcpPoint urPoint; urPoint.mPixel = LocationType(endCol, startRow); urPoint.mCoordinate = pRaster->convertPixelToGeocoord(urPoint.mPixel); GcpPoint llPoint; llPoint.mPixel = LocationType(startCol, endRow); llPoint.mCoordinate = pRaster->convertPixelToGeocoord(llPoint.mPixel); GcpPoint lrPoint; lrPoint.mPixel = LocationType(endCol, endRow); lrPoint.mCoordinate = pRaster->convertPixelToGeocoord(lrPoint.mPixel); GcpPoint centerPoint; centerPoint.mPixel = LocationType((startCol + endCol) / 2, (startRow + endRow) / 2); centerPoint.mCoordinate = pRaster->convertPixelToGeocoord(centerPoint.mPixel); // Reset the coordinates to be in active numbers relative to the chip const vector<DimensionDescriptor>& chipRows = pDescriptor->getRows(); const vector<DimensionDescriptor>& chipColumns = pDescriptor->getColumns(); VERIFYNRV(chipRows.front().isActiveNumberValid() == true); VERIFYNRV(chipRows.back().isActiveNumberValid() == true); VERIFYNRV(chipColumns.front().isActiveNumberValid() == true); VERIFYNRV(chipColumns.back().isActiveNumberValid() == true); unsigned int chipStartRow = chipRows.front().getActiveNumber(); unsigned int chipEndRow = chipRows.back().getActiveNumber(); unsigned int chipStartCol = chipColumns.front().getActiveNumber(); unsigned int chipEndCol = chipColumns.back().getActiveNumber(); ulPoint.mPixel = LocationType(chipStartCol, chipStartRow); urPoint.mPixel = LocationType(chipEndCol, chipStartRow); llPoint.mPixel = LocationType(chipStartCol, chipEndRow); lrPoint.mPixel = LocationType(chipEndCol, chipEndRow); centerPoint.mPixel = LocationType((chipStartCol + chipEndCol) / 2, (chipStartRow + chipEndRow) / 2); // Create the GCP list Service<ModelServices> pModel; GcpList* pGcpList = static_cast<GcpList*>(pModel->createElement("Corner Coordinates", TypeConverter::toString<GcpList>(), pRasterChip)); if (pGcpList != NULL) { list<GcpPoint> gcps; gcps.push_back(ulPoint); gcps.push_back(urPoint); gcps.push_back(llPoint); gcps.push_back(lrPoint); gcps.push_back(centerPoint); pGcpList->addPoints(gcps); // Create the layer if (pView->createLayer(GCP_LAYER, pGcpList) == NULL) { QMessageBox::warning(this, windowTitle(), "Could not create a GCP layer."); } } else { QMessageBox::warning(this, windowTitle(), "Could not create a GCP list."); } } } }
bool ChangeUpDirection::execute(PlugInArgList* pInArgList, PlugInArgList* pOutArgList) { if (pInArgList == NULL || pOutArgList == NULL) { return false; } ProgressTracker progress(pInArgList->getPlugInArgValue<Progress>(Executable::ProgressArg()), "Rotating data.", "app", "{11adadb9-c133-49de-8cf5-a16372da2578}"); RasterElement* pData = pInArgList->getPlugInArgValue<RasterElement>(Executable::DataElementArg()); if (pData == NULL) { progress.report("No data element specified.", 0, ERRORS, true); return false; } bool display = false; if (!pInArgList->getPlugInArgValue("Display Results", display)) { progress.report("Unsure if results should be displayed. Invalid argument.", 0, ERRORS, true); return false; } double rotation = 0.0; SpatialDataView* pOrigView = NULL; if (isBatch()) { if (!pInArgList->getPlugInArgValue("Rotation", rotation)) { progress.report("No rotation specified.", 0, ERRORS, true); return false; } } else { pOrigView = pInArgList->getPlugInArgValue<SpatialDataView>(Executable::ViewArg()); if (pOrigView == NULL) { progress.report("No view specified.", 0, ERRORS, true); return false; } GraphicLayer* pLayer = dynamic_cast<GraphicLayer*>(pOrigView->getActiveLayer()); if (pLayer == NULL) { pLayer = dynamic_cast<GraphicLayer*>(pOrigView->getTopMostLayer(ANNOTATION)); } GraphicObject* pArrow = NULL; if (pLayer != NULL) { std::list<GraphicObject*> objects; pLayer->getObjects(ARROW_OBJECT, objects); if (!objects.empty()) { pArrow = objects.back(); } if (objects.size() > 1) { progress.report("Multiple arrow objects found. Using the most recently added one.", 0, WARNING, true); } } if (pArrow == NULL) { progress.report("Unable to locate up direction. Add an arrow annotation and re-run this plugin.", 0, ERRORS, true); return false; } LocationType ur = pArrow->getUrCorner(); LocationType ll = pArrow->getLlCorner(); double xlen = ur.mX - ll.mX; double ylen = ur.mY - ll.mY; // Initial rotatation value. The 90 degrees is due to the difference // in the "0 point" (right vs. up). Also account for explicit rotation // of the annotation object. Convert this to radians. rotation = GeoConversions::convertDegToRad(90 + pArrow->getRotation()); // Determine a rotation adjustment based on the bounding box rotation += atan2(ylen, xlen); } progress.report("Rotating data.", 10, NORMAL); ModelResource<RasterElement> pRotated(pData->copyShallow(pData->getName() + "_rotated", pData->getParent())); if (pRotated.get() == NULL) { progress.report("Unable to create destination raster element.", 0, ERRORS, true); return false; } int defaultBadValue(0); // the rotate method will handle setting the default bad values into the rotated raster if (!RasterUtilities::rotate(pRotated.get(), pData, rotation, defaultBadValue, INTERP_NEAREST_NEIGHBOR, progress.getCurrentProgress(), &mAbort)) { // error message already reported by rotate() return false; } pOutArgList->setPlugInArgValue("Rotated Element", pRotated.get()); if (display) { SpatialDataWindow* pWindow = static_cast<SpatialDataWindow*>( Service<DesktopServices>()->createWindow(pRotated->getName(), SPATIAL_DATA_WINDOW)); SpatialDataView* pView = (pWindow == NULL) ? NULL : pWindow->getSpatialDataView(); if (pView == NULL) { Service<DesktopServices>()->deleteWindow(pWindow); progress.report("Unable to create view.", 0, ERRORS, true); return false; } pView->setPrimaryRasterElement(pRotated.get()); RasterLayer* pLayer = NULL; { // scope UndoLock lock(pView); pLayer = static_cast<RasterLayer*>(pView->createLayer(RASTER, pRotated.get())); } if (pLayer == NULL) { //#pragma message(__FILE__ "(" STRING(__LINE__) ") : warning : This would be cleaner with a WindowResource. If one " \ // "becomes available, use it instead. (tclarke)") Service<DesktopServices>()->deleteWindow(pWindow); progress.report("Unable to create layer.", 0, ERRORS, true); return false; } pOutArgList->setPlugInArgValue("View", pView); } pRotated.release(); progress.report("Rotation complete.", 100, NORMAL); progress.upALevel(); return true; }