raster :: focal返回不正确的值

时间:2017-01-02 07:36:44

标签: r filter r-raster

我正在使用 raster package v2.5-8 中的焦点功能来获取3x3窗口中的最大值。我希望两个行/列的边缘返回NA,而返回的输出是9,9,9。这是对的吗?

示例:

     [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]    9    9    9
[3,]   NA   NA   NA

输出:

       [,1] [,2] [,3]
[1,]   NA   NA   NA
[2,]   NA    9   NA
[3,]   NA   NA   NA

预期输出:

<?php

namespace AppBundle\Controller;

use Blameable\Fixture\Document\User;
use FOS\RestBundle\View\View;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController as BaseCustomerController;
use Sylius\Component\Resource\ResourceActions;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Sylius\Bundle\UserBundle\Security\UserLogin as UserLogin;

class CustomerController extends BaseCustomerController
{
    /**
     * @param Request $request
     *
     * @return Response
     */
    public function createAction(Request $request)
    {
        $configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

    $this->isGrantedOr403($configuration, ResourceActions::CREATE);
    $newResource = $this->newResourceFactory->create($configuration, $this->factory);

    $form = $this->resourceFormFactory->create($configuration, $newResource);

    if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
        $newResource = $form->getData();

        $event = $this->eventDispatcher->dispatchPreEvent(ResourceActions::CREATE, $configuration, $newResource);

        if ($event->isStopped() && !$configuration->isHtmlRequest()) {
            throw new HttpException($event->getErrorCode(), $event->getMessage());
        }
        if ($event->isStopped()) {
            $this->flashHelper->addFlashFromEvent($configuration, $event);

            return $this->redirectHandler->redirectToIndex($configuration, $newResource);
        }

        if ($configuration->hasStateMachine()) {
            $this->stateMachine->apply($configuration, $newResource);
        }
        $newResource->getUser()->enable();
        $this->repository->add($newResource);
        $this->get('sylius.security.user_login')->login($newResource->getUser());
        $this->eventDispatcher->dispatchPostEvent(ResourceActions::CREATE, $configuration, $newResource);


        if (!$configuration->isHtmlRequest()) {
            return $this->viewHandler->handle($configuration, View::create($newResource, Response::HTTP_CREATED));
        }

        $this->flashHelper->addSuccessFlash($configuration, ResourceActions::CREATE, $newResource);

        return $this->redirectHandler->redirectToResource($configuration, $newResource);
    }

    if (!$configuration->isHtmlRequest()) {
        return $this->viewHandler->handle($configuration, View::create($form, Response::HTTP_BAD_REQUEST));
    }

    $view = View::create()
        ->setData([
            'configuration' => $configuration,
            'metadata' => $this->metadata,
            'resource' => $newResource,
            $this->metadata->getName() => $newResource,
            'form' => $form->createView(),
        ])
        ->setTemplate($configuration->getTemplate(ResourceActions::CREATE . '.html'))
    ;

    return $this->viewHandler->handle($configuration, $view);
}


}

1 个答案:

答案 0 :(得分:2)

你得到这个结果是因为地球的“左”和“右”两侧(经度= -180或180)是相同的位置。

library(raster)
r <- raster(nrows=3, ncols=3)
r[] <- 1:ncell(r)
as.matrix(r)

##     [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

rf <- focal(r, w=matrix(1,nrow=3,ncol=3), fun=max) 
as.matrix(rf)

##     [,1] [,2] [,3]
## [1,]   NA   NA   NA
## [2,]    9    9    9
## [3,]   NA   NA   NA

默认的CRS是lonlat

crs(r)

## CRS arguments:
## +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

使用平面CRS,您可以获得预期的结果:

crs(r) <- "+proj=utm +zone=1 +datum=WGS84"
rf2 <- focal(r, w=matrix(1,nrow=3,ncol=3), fun=max) 
as.matrix(rf2)

##     [,1] [,2] [,3]
## [1,]   NA   NA   NA
## [2,]   NA    9   NA
## [3,]   NA   NA   NA