You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Spring Data Neo4j 4.1.2保存地理空间点数据?

Saving Geospatial Points with Spring Data Neo4j 4.1.2

Got it, I know how frustrating it is to track down working examples for older versions like SDN 4.1.2—most docs now focus on newer releases, but let’s walk through exactly how to save geospatial point data here.

Step 1: Define Your Entity with a Geospatial Point

First, you’ll need to use Neo4j’s built-in Point type from org.neo4j.spatial.Point. SDN 4.1.2 supports this type out of the box, just make sure you import the correct class. Here’s an example Place entity:

import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Property;
import org.neo4j.spatial.Point;

@NodeEntity
public class Place {

    @GraphId
    private Long id;
    
    private String name;
    
    @Property(name = "location")
    private Point location;

    // Constructors, getters, setters
    public Place() {}

    public Place(String name, Point location) {
        this.name = name;
        this.location = location;
    }

    // Add getters and setters for all fields
}

Step 2: Create a Point Instance

To make a Point object, you can use Neo4j’s PointFactory or parse a WKT string. A critical gotcha: for the standard WGS84 GPS system (SRID 4326), coordinates are longitude first, then latitude—don’t mix this up!

Option 1: Using PointFactory

import org.neo4j.spatial.PointFactory;
import org.neo4j.spatial.SRID;

// Create a point for New York City (lon: -74.0060, lat: 40.7128)
Point nycPoint = PointFactory.defaultFactory().point(-74.0060, 40.7128, SRID.WGS84);

Option 2: Parsing WKT String

import org.neo4j.spatial.Point;

Point nycPoint = Point.valueOf("POINT(-74.0060 40.7128)");
// SRID defaults to 4326 here, which is perfect for GPS coordinates

Step 3: Save the Entity with SDN

Next, define a repository interface extending Neo4jRepository:

import org.springframework.data.neo4j.repository.Neo4jRepository;

public interface PlaceRepository extends Neo4jRepository<Place, Long> {
}

Inject this repository into your service/controller and save the entity:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PlaceService {

    private final PlaceRepository placeRepository;

    @Autowired
    public PlaceService(PlaceRepository placeRepository) {
        this.placeRepository = placeRepository;
    }

    public Place savePlace(String name, Point location) {
        Place place = new Place(name, location);
        return placeRepository.save(place);
    }
}

Key Notes to Avoid Headaches

  • Coordinate Order: Stick to (longitude, latitude) for SRID 4326—Neo4j follows WKT standards, which is opposite of what some mapping tools use.
  • Dependency Compatibility: Make sure your spring-data-neo4j is exactly 4.1.2, and pair it with a compatible Neo4j OGM driver (version 2.0.5 works well for this SDN release).
  • Querying Bonus: If you need to query points later (e.g., find places within a radius), use Cypher’s spatial functions in your repository:
    import org.springframework.data.neo4j.annotation.Query;
    import java.util.List;
    
    public interface PlaceRepository extends Neo4jRepository<Place, Long> {
    
        @Query("MATCH (p:Place) WHERE distance(p.location, point({longitude}, {latitude}, 4326)) < {distance} RETURN p")
        List<Place> findPlacesWithinDistance(double longitude, double latitude, double distance);
    }
    

That should get you up and running with saving geospatial points in SDN 4.1.2. Let me know if you hit any roadblocks!

内容的提问来源于stack exchange,提问作者vijayg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:43:58