Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geo/geotransform: implement ST_Transform #49783

Merged
merged 1 commit into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ BUILD_TAGGED_RELEASE =
## Override for .buildinfo/tag
BUILDINFO_TAG :=

$(go-targets): bin/.bootstrap $(BUILDINFO) $(CGO_FLAGS_FILES) $(PROTOBUF_TARGETS)
$(go-targets): bin/.bootstrap $(BUILDINFO) $(CGO_FLAGS_FILES) $(PROTOBUF_TARGETS) $(LIBPROJ)
$(go-targets): $(SQLPARSER_TARGETS) $(EXECGEN_TARGETS) $(OPTGEN_TARGETS)
$(go-targets): override LINKFLAGS += \
-X "github.com/cockroachdb/cockroach/pkg/build.tag=$(if $(BUILDINFO_TAG),$(BUILDINFO_TAG),$(shell cat .buildinfo/tag))" \
Expand Down Expand Up @@ -1715,7 +1715,7 @@ logictest-bins := bin/logictest bin/logictestopt bin/logictestccl
# Additional dependencies for binaries that depend on generated code.
#
# TODO(benesch): Derive this automatically. This is getting out of hand.
bin/workload bin/docgen bin/execgen bin/roachtest $(logictest-bins): $(SQLPARSER_TARGETS) $(PROTOBUF_TARGETS)
bin/workload bin/docgen bin/execgen bin/roachtest $(logictest-bins): $(LIBPROJ) $(CGO_FLAGS_FILES) $(SQLPARSER_TARGETS) $(PROTOBUF_TARGETS)
bin/workload bin/roachtest $(logictest-bins): $(EXECGEN_TARGETS)
bin/roachtest $(logictest-bins): $(C_LIBS_CCL) $(CGO_FLAGS_FILES) $(OPTGEN_TARGETS)

Expand Down
12 changes: 12 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,18 @@ has no relationship with the commit order of concurrent transactions.</p>
<p>This function utilizes the GEOS module.</p>
<p>This function will automatically use any available index.</p>
</span></td></tr>
<tr><td><a name="st_transform"></a><code>st_transform(geometry: geometry, from_proj_text: <a href="string.html">string</a>, srid: <a href="int.html">int</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Transforms a geometry into the coordinate reference system assuming the from_proj_text to the new to_proj_text by projecting its coordinates. The supplied SRID is set on the new geometry.</p>
<p>This function utilizes the PROJ library for coordinate projections.</p>
</span></td></tr>
<tr><td><a name="st_transform"></a><code>st_transform(geometry: geometry, from_proj_text: <a href="string.html">string</a>, to_proj_text: <a href="string.html">string</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Transforms a geometry into the coordinate reference system assuming the from_proj_text to the new to_proj_text by projecting its coordinates.</p>
<p>This function utilizes the PROJ library for coordinate projections.</p>
</span></td></tr>
<tr><td><a name="st_transform"></a><code>st_transform(geometry: geometry, srid: <a href="int.html">int</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Transforms a geometry into the given SRID coordinate reference system by projecting its coordinates.</p>
<p>This function utilizes the PROJ library for coordinate projections.</p>
</span></td></tr>
<tr><td><a name="st_transform"></a><code>st_transform(geometry: geometry, to_proj_text: <a href="string.html">string</a>) &rarr; geometry</code></td><td><span class="funcdesc"><p>Transforms a geometry into the coordinate reference system referenced by the projection text by projecting its coordinates.</p>
<p>This function utilizes the PROJ library for coordinate projections.</p>
</span></td></tr>
<tr><td><a name="st_within"></a><code>st_within(geometry_a: geometry, geometry_b: geometry) &rarr; <a href="bool.html">bool</a></code></td><td><span class="funcdesc"><p>Returns true if geometry_a is completely inside geometry_b.</p>
<p>This function utilizes the GEOS module.</p>
<p>This function will automatically use any available index.</p>
Expand Down
109 changes: 109 additions & 0 deletions pkg/geo/geoproj/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
---
Language: Cpp
AccessModifierOffset: -1
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|isl|json)/)'
Priority: 3
- Regex: '^".*"$'
Priority: 4
- Regex: '.*'
Priority: 5
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentWidth: 2
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...

65 changes: 46 additions & 19 deletions pkg/geo/geoproj/geoproj.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,55 @@ package geoproj
// #cgo windows LDFLAGS: -lshlwapi -lrpcrt4
//
// #include "proj.h"
// #include <proj_api.h>
import "C"
import "unsafe"
import (
"unsafe"

// ProjPJ is the projPJ wrapper around the PROJ library's projPJ object.
type ProjPJ struct {
projPJ C.projPJ
}
"github.com/cockroachdb/cockroach/pkg/geo/geoprojbase"
"github.com/cockroachdb/errors"
)

// maxArrayLen is the maximum safe length for this architecture.
const maxArrayLen = 1<<31 - 1

// IsLatLng returns whether the underlying ProjPJ is a latlng system.
// TODO(otan): store this metadata in the projPJ struct.
func (p *ProjPJ) IsLatLng() bool {
return C.pj_is_latlong(p.projPJ) != 0
func cStatusToUnsafeGoBytes(s C.CR_PROJ_Status) []byte {
if s.data == nil {
return nil
}
// Interpret the C pointer as a pointer to a Go array, then slice.
return (*[maxArrayLen]byte)(unsafe.Pointer(s.data))[:s.len:s.len]
}

// NewProjPJFromText initializes a ProjPJ from text.
// TODO(otan): thread through thread contexts and retrieve error messages.
// TODO(otan): use slice management mechanisms.
// TODO(otan): free after creation.
func NewProjPJFromText(proj4text string) (*ProjPJ, error) {
str := C.CString(proj4text)
defer C.free(unsafe.Pointer(str))
projPJ := C.pj_init_plus(str)
return &ProjPJ{projPJ: projPJ}, nil
// Project projects the given xCoords, yCoords and zCoords from one
// coordinate system to another using proj4text.
// Array elements are edited in place.
func Project(
from geoprojbase.Proj4Text,
to geoprojbase.Proj4Text,
xCoords []float64,
yCoords []float64,
zCoords []float64,
) error {
if len(xCoords) != len(yCoords) || len(xCoords) != len(zCoords) {
return errors.Newf(
"len(xCoords) != len(yCoords) != len(zCoords): %d != %d != %d",
len(xCoords),
len(yCoords),
len(zCoords),
)
}
if len(xCoords) == 0 {
return nil
}
if err := cStatusToUnsafeGoBytes(C.CR_PROJ_Transform(
(*C.char)(unsafe.Pointer(&from.Bytes()[0])),
(*C.char)(unsafe.Pointer(&to.Bytes()[0])),
C.long(len(xCoords)),
(*C.double)(unsafe.Pointer(&xCoords[0])),
(*C.double)(unsafe.Pointer(&yCoords[0])),
(*C.double)(unsafe.Pointer(&zCoords[0])),
)); err != nil {
return errors.Newf("error from PROJ: %s", string(err))
}
return nil
}
61 changes: 57 additions & 4 deletions pkg/geo/geoproj/geoproj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,64 @@ package geoproj
import (
"testing"

"github.com/cockroachdb/cockroach/pkg/geo/geoprojbase"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNewProjPJFromText(t *testing.T) {
pjProj, err := NewProjPJFromText("+proj=longlat +datum=WGS84 +no_defs")
require.NoError(t, err)
require.True(t, pjProj.IsLatLng())
func TestProject(t *testing.T) {
testCases := []struct {
desc string

from geoprojbase.Proj4Text
to geoprojbase.Proj4Text
xCoords []float64
yCoords []float64
zCoords []float64

expectedXCoords []float64
expectedYCoords []float64
// Ignore Z Coord for now; it usually has a garbage value.
}{
{
desc: "SRID 4326 to 3857",
from: geoprojbase.MakeProj4Text("+proj=longlat +datum=WGS84 +no_defs"),
to: geoprojbase.MakeProj4Text("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"),
xCoords: []float64{1},
yCoords: []float64{1},
zCoords: []float64{0},
expectedXCoords: []float64{111319.490793274},
expectedYCoords: []float64{111325.142866385},
},
{
desc: "SRID 3857 to 4326",
from: geoprojbase.MakeProj4Text("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"),
to: geoprojbase.MakeProj4Text("+proj=longlat +datum=WGS84 +no_defs"),
xCoords: []float64{1},
yCoords: []float64{1},
zCoords: []float64{0},
expectedXCoords: []float64{0.000008983152841},
expectedYCoords: []float64{0.000008983152841},
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
err := Project(tc.from, tc.to, tc.xCoords, tc.yCoords, tc.zCoords)
require.NoError(t, err)
assert.InEpsilonSlicef(t, tc.expectedXCoords, tc.xCoords, 1e-10, "expected: %#v, found %#v", tc.expectedXCoords, tc.xCoords)
assert.InEpsilonSlicef(t, tc.expectedYCoords, tc.yCoords, 1e-10, "expected: %#v, found %#v", tc.expectedYCoords, tc.yCoords)
})
}

t.Run("test error handling", func(t *testing.T) {
err := Project(
geoprojbase.MakeProj4Text("+bad"),
geoprojbase.MakeProj4Text("+bad"),
[]float64{1},
[]float64{2},
[]float64{3},
)
require.Error(t, err)
})
}
58 changes: 58 additions & 0 deletions pkg/geo/geoproj/proj.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,62 @@
// licenses/APL.txt.

#include "proj.h"
#include <cstring>
#include <proj_api.h>
#include <stdlib.h>
#include <string>

const char* DEFAULT_ERROR_MSG = "PROJ could not parse proj4text";

namespace {
CR_PROJ_Status CR_PROJ_ErrorFromErrorCode(int code) {
char* err = pj_strerrno(code);
if (err == nullptr) {
err = (char*)DEFAULT_ERROR_MSG;
}
return {.data = err, .len = strlen(err)};
}

} // namespace

CR_PROJ_Status CR_PROJ_Transform(char* fromSpec, char* toSpec, long point_count, double* x,
double* y, double* z) {
CR_PROJ_Status err = {.data = NULL, .len = 0};
auto ctx = pj_ctx_alloc();
auto fromPJ = pj_init_plus_ctx(ctx, fromSpec);
if (fromPJ == nullptr) {
err = CR_PROJ_ErrorFromErrorCode(pj_ctx_get_errno(ctx));
pj_ctx_free(ctx);
return err;
}
auto toPJ = pj_init_plus_ctx(ctx, toSpec);
if (toPJ == nullptr) {
err = CR_PROJ_ErrorFromErrorCode(pj_ctx_get_errno(ctx));
pj_ctx_free(ctx);
return err;
}
// If we have a latlng from, transform to radians.
if (pj_is_latlong(fromPJ)) {
for (auto i = 0; i < point_count; i++) {
x[i] = x[i] * DEG_TO_RAD;
y[i] = y[i] * DEG_TO_RAD;
}
}
pj_transform(fromPJ, toPJ, point_count, 0, x, y, z);
int errCode = pj_ctx_get_errno(ctx);
if (errCode != 0) {
err = CR_PROJ_ErrorFromErrorCode(errCode);
pj_ctx_free(ctx);
return err;
}

// If we have a latlng to, transform to degrees.
if (pj_is_latlong(toPJ)) {
for (auto i = 0; i < point_count; i++) {
x[i] = x[i] * RAD_TO_DEG;
y[i] = y[i] * RAD_TO_DEG;
}
}
pj_ctx_free(ctx);
return err;
}
25 changes: 25 additions & 0 deletions pkg/geo/geoproj/proj.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,28 @@
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif

// CR_PROJ_Slice contains data that does not need to be freed.
// // It can be either a Go or C pointer (which indicates who allocated the
// memory).
typedef struct {
char* data;
size_t len;
} CR_PROJ_Slice;

typedef CR_PROJ_Slice CR_PROJ_Status;

// CR_PROJ_Transform converts the given x/y/z coordinates to a new project specification.
// Note points (x[i], y[i], z[i]) are in the range 0 <= i < point_coint.
CR_PROJ_Status CR_PROJ_Transform(char* fromSpec, char* toSpec, long point_count, double* x,
double* y, double* z);

#ifdef __cplusplus
} // extern "C"
#endif
Loading