0
# Spatial Data Types
1
2
Neo4j's spatial data types provide support for geographic and geometric data with point representations, coordinate systems, and spatial operations for location-aware applications.
3
4
## Capabilities
5
6
### Point Interface
7
8
Represents a point in space with coordinates and coordinate reference system (CRS) information.
9
10
```java { .api }
11
/**
12
* Represents a point in space with coordinates and CRS
13
*/
14
public interface Point {
15
16
/**
17
* Get the coordinate reference system for this point
18
* @return CRS information
19
*/
20
CRS getCRS();
21
22
/**
23
* Get the coordinates of this point
24
* @return List of coordinate values
25
*/
26
List<Coordinate> getCoordinates();
27
28
/**
29
* Get the spatial dimension of this point
30
* @return Number of dimensions (2D, 3D, etc.)
31
*/
32
int getDimension();
33
34
/**
35
* Get the SRID (Spatial Reference Identifier)
36
* @return SRID value
37
*/
38
int getSRID();
39
}
40
```
41
42
### Coordinate Interface
43
44
Represents a single coordinate value within a point.
45
46
```java { .api }
47
/**
48
* Represents a coordinate value
49
*/
50
public interface Coordinate {
51
52
/**
53
* Get the coordinate value
54
* @return Coordinate value as double
55
*/
56
double getCoordinate();
57
}
58
```
59
60
### Coordinate Reference System (CRS)
61
62
Defines the coordinate reference system for spatial data.
63
64
```java { .api }
65
/**
66
* Coordinate Reference System definition
67
*/
68
public interface CRS {
69
70
/**
71
* Get the CRS code (e.g., 4326 for WGS-84)
72
* @return CRS code
73
*/
74
int getCode();
75
76
/**
77
* Get the CRS type (e.g., "link", "name")
78
* @return CRS type string
79
*/
80
String getType();
81
82
/**
83
* Get additional CRS properties
84
* @return Map of CRS properties
85
*/
86
Map<String, Object> getProperties();
87
88
/**
89
* Get the coordinate system URL or identifier
90
* @return CRS href or name
91
*/
92
String getHref();
93
}
94
```
95
96
### Geometry Interface
97
98
Base interface for geometric objects (extends spatial functionality).
99
100
```java { .api }
101
/**
102
* Base interface for geometric objects
103
*/
104
public interface Geometry {
105
106
/**
107
* Get the geometry type (e.g., "Point", "LineString", "Polygon")
108
* @return Geometry type string
109
*/
110
String getGeometryType();
111
112
/**
113
* Get the coordinate reference system
114
* @return CRS information
115
*/
116
CRS getCRS();
117
118
/**
119
* Check if this geometry is empty
120
* @return true if geometry contains no points
121
*/
122
boolean isEmpty();
123
}
124
```
125
126
**Usage Examples:**
127
128
```java
129
import org.neo4j.graphdb.spatial.Point;
130
import org.neo4j.graphdb.spatial.CRS;
131
import org.neo4j.graphdb.spatial.Coordinate;
132
133
// Creating spatial queries with points
134
try (Transaction tx = graphDb.beginTx()) {
135
// Create a node with a point property (latitude/longitude)
136
Node location = tx.createNode(label("Location"));
137
location.setProperty("name", "New York City");
138
139
// Point will be created by Neo4j spatial functions in Cypher
140
String createPointQuery = """
141
MATCH (loc:Location {name: 'New York City'})
142
SET loc.coordinates = point({latitude: 40.7128, longitude: -74.0060})
143
RETURN loc.coordinates as point
144
""";
145
146
Result result = tx.execute(createPointQuery);
147
if (result.hasNext()) {
148
Map<String, Object> record = result.next();
149
Point point = (Point) record.get("point");
150
151
// Access point properties
152
System.out.println("CRS Code: " + point.getCRS().getCode());
153
System.out.println("Coordinates: " + point.getCoordinates());
154
System.out.println("Dimensions: " + point.getDimension());
155
}
156
157
tx.commit();
158
}
159
160
// Spatial distance queries
161
try (Transaction tx = graphDb.beginTx()) {
162
String spatialQuery = """
163
MATCH (loc:Location)
164
WHERE distance(loc.coordinates, point({latitude: 40.7589, longitude: -73.9851})) < 5000
165
RETURN loc.name, loc.coordinates,
166
distance(loc.coordinates, point({latitude: 40.7589, longitude: -73.9851})) as distance
167
ORDER BY distance
168
""";
169
170
Result spatialResults = tx.execute(spatialQuery);
171
spatialResults.forEachRemaining(record -> {
172
String name = (String) record.get("loc.name");
173
Point coordinates = (Point) record.get("loc.coordinates");
174
Double distance = (Double) record.get("distance");
175
176
System.out.printf("Location: %s, Distance: %.2f meters%n", name, distance);
177
});
178
179
tx.commit();
180
}
181
```
182
183
### Common Spatial Operations
184
185
Neo4j provides built-in spatial functions for working with points:
186
187
- `point({latitude: lat, longitude: lon})` - Create a WGS-84 point
188
- `point({x: x, y: y})` - Create a Cartesian point
189
- `point({x: x, y: y, crs: 'cartesian-3d', z: z})` - Create a 3D Cartesian point
190
- `distance(point1, point2)` - Calculate distance between two points
191
- `point.latitude`, `point.longitude`, `point.x`, `point.y` - Access coordinate components
192
193
### Coordinate Reference Systems
194
195
Common CRS codes used in Neo4j:
196
197
- **4326** (WGS-84): Geographic coordinate system using latitude/longitude
198
- **4979** (WGS-84 3D): 3D geographic coordinate system
199
- **7203** (Cartesian): 2D Cartesian coordinate system
200
- **9157** (Cartesian 3D): 3D Cartesian coordinate system