Преобразовать функции Geojson в Java Object и сохранить в PostgreSQL (Postgres), используя Spring BootJAVA

Программисты JAVA общаются здесь
Anonymous
Преобразовать функции Geojson в Java Object и сохранить в PostgreSQL (Postgres), используя Spring Boot

Сообщение Anonymous »

Итак, я пытаюсь отобразить свои функции Geojson с объектом Java и сохранить их в базе данных PostGIS.package com.nxquar.pinpoint.service.locationGeoJsonService;

import com.fasterxml.jackson.databind.JsonNode;
import com.nxquar.pinpoint.Model.Building;
import com.nxquar.pinpoint.Model.Floor;
import com.nxquar.pinpoint.Model.Room;
import com.nxquar.pinpoint.Repository.BuildingRepository;
import com.nxquar.pinpoint.Repository.FloorRepository;
import com.nxquar.pinpoint.Repository.RoomRepository;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.locationtech.jts.geom.*;

@Service
public class LocationParsingService {

@Autowired
private BuildingRepository buildingRepository;

@Autowired
private FloorRepository floorRepository;

@Autowired
private RoomRepository roomRepository;

private final GeometryFactory geometryFactory = new GeometryFactory();

public Building processGeoJsonBuilding(JsonNode geoJson) {
// Create new building
Building building = new Building();
buildingRepository.save(building);

// Group features by floor
Map featuresByFloor = new HashMap();
JsonNode features = geoJson.get("features");

for (JsonNode feature : features) {
int floorLevel = feature.get("properties").get("floor").asInt();
featuresByFloor.computeIfAbsent(floorLevel, k -> new ArrayList()).add(feature);
}

// Process each floor
for (Map.Entry entry : featuresByFloor.entrySet()) {
Floor floor = new Floor();
floor.setLevel(entry.getKey());
floor.setBuilding(building);
floorRepository.save(floor);

// Process rooms on this floor
for (JsonNode feature : entry.getValue()) {
Room room = new Room();
JsonNode properties = feature.get("properties");

room.setName(properties.get("name").asText());
room.setType(properties.get("type").asText());
room.setFloorLevel(properties.get("floor").asInt());
room.setFloor(floor);

// Convert GeoJSON geometry to JTS Geometry
Geometry geometry = parseGeometry(feature.get("geometry"));
room.setGeometry(geometry);

roomRepository.save(room);
}
}

return building;
}

private Geometry parseGeometry(JsonNode geometryNode) {
String type = geometryNode.get("type").asText();
JsonNode coordinatesNode = geometryNode.get("coordinates");

Geometry geom;
switch (type) {
case "Point":
geom = createPoint(coordinatesNode);
break;
case "MultiPoint":
geom = createMultiPoint(coordinatesNode);
break;
case "LineString":
geom = createLineString(coordinatesNode);
break;
case "MultiLineString":
geom = createMultiLineString(coordinatesNode);
break;
case "Polygon":
geom = createPolygon(coordinatesNode);
break;
case "MultiPolygon":
geom = createMultiPolygon(coordinatesNode);
break;
case "GeometryCollection":
geom = createGeometryCollection(geometryNode);
break;
default:
throw new IllegalArgumentException("Unsupported geometry type: " + type);
}

geom.setSRID(4326); //
application.properties

spring.application.name=pinpoint

spring.datasource.url=jdbc:postgresql://localhost:5432/pinpoint
spring.datasource.username=postgres
spring.datasource.password=12345678
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

# Use standard PostgreSQL dialect; hibernate-spatial auto-detects PostGIS types
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

spring.servlet.multipart.enabled=true
spring.servlet.multipart.max-file-size=200MB
< /code>
контроллер < /p>
package com.nxquar.pinpoint.controller;

import com.nxquar.pinpoint.Model.Building;
import com.nxquar.pinpoint.service.locationGeoJsonService.LocationParsingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@RequestMapping("/api/auth/buildings")
public class BuildingController {

@Autowired
private LocationParsingService buildingService;

@PostMapping(
path = "/upload",
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity uploadBuildingGeoJson(@RequestParam("file") MultipartFile file) {
try {
ObjectMapper mapper = new ObjectMapper();
JsonNode geoJson = mapper.readTree(file.getInputStream());
Building building = buildingService.processGeoJsonBuilding(geoJson);
return ResponseEntity.ok(building);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}
}

< /code>
мои объекты < /p>
package com.nxquar.pinpoint.Model;

import com.nxquar.pinpoint.Model.Users.Institute;
import jakarta.persistence.*;

import java.util.List;
import java.util.UUID;

@Entity
public class Building {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;

private String name;

@ManyToOne
private Institute institute;

@OneToMany(mappedBy = "building")
private List floors;
}package com.nxquar.pinpoint.Model;

import jakarta.persistence.*;
import lombok.Data;
import org.locationtech.jts.geom.Geometry;

import java.util.List;
import java.util.UUID;

@Entity
@Data
public class Floor {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;

private int level;

@ManyToOne
private Building building;
@OneToMany(mappedBy = "floor", cascade = CascadeType.ALL)
private List rooms;
}package com.nxquar.pinpoint.Model;

import jakarta.persistence.*;
import lombok.Data;
import org.locationtech.jts.geom.Geometry;

import java.util.List;
import java.util.UUID;

@Entity
@Data
public class Floor {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;

private int level;

@ManyToOne
private Building building;
@OneToMany(mappedBy = "floor", cascade = CascadeType.ALL)
private List rooms;
}
< /code>
Образец Geojson < /p>
{
"type": "FeatureCollection",
"name": "LECTURE THEATER",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 1, "type": "Lecture theatre", "name": "lecture theater 1", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.82662800915881, 31.635955996183803 ], [ 74.826717040367484, 31.635986189887031 ], [ 74.82677234387593, 31.635856969214199 ], [ 74.826684180659527, 31.635826669896456 ], [ 74.82662800915881, 31.635955996183803 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 2, "type": "Lecture theatre", "name": "lecture theater 2", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.826956265241208, 31.636119501303959 ], [ 74.82693989738668, 31.636037788369439 ], [ 74.826784154770991, 31.636057424817622 ], [ 74.82679779464975, 31.636140193456864 ], [ 74.826956265241208, 31.636119501303959 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 3, "type": "Lecture theatre", "name": "lecture theater 3", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.82700140083999, 31.635954175059801 ], [ 74.827099731966001, 31.635883547091954 ], [ 74.826974741076953, 31.635757071164974 ], [ 74.826878393933328, 31.635824532052276 ], [ 74.82700140083999, 31.635954175059801 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 4, "type": "room", "name": "room1", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.827003012825614, 31.636053201925886 ], [ 74.827060052318629, 31.636073260659906 ], [ 74.827073568198486, 31.636041166683398 ], [ 74.82701590871099, 31.636021952521105 ], [ 74.827003012825614, 31.636053201925886 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 5, "type": "room", "name": "room2", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.826758239001265, 31.635751475815333 ], [ 74.826767414919701, 31.635803206393668 ], [ 74.826805606580237, 31.635799194635567 ], [ 74.826795686668405, 31.635747252909713 ], [ 74.826758239001265, 31.635751475815333 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 6, "type": "stairs", "name": "stairs 1", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.826806970568086, 31.635792965852893 ], [ 74.826840574269411, 31.635788320658769 ], [ 74.826831522349863, 31.635741551986811 ], [ 74.826798166646341, 31.635746302755923 ], [ 74.826806970568086, 31.635792965852893 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 7, "type": "stairs", "name": "stairs 2", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.827074560189686, 31.636038844092564 ], [ 74.827086340084989, 31.636011395287397 ], [ 74.827034632544567, 31.635993870276792 ], [ 74.82702257917822, 31.636021887702984 ], [ 74.827074560189686, 31.636038844092564 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 8, "type": "labotary", "name": "labotary1", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.826878889928864, 31.635789270812126 ], [ 74.826868722019228, 31.635736801217462 ], [ 74.82683437432452, 31.635741129696214 ], [ 74.826844418235254, 31.635792965852893 ], [ 74.826878889928864, 31.635789270812126 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 9, "type": "labotary", "name": "labotary2", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.827100227961509, 31.635980568158097 ], [ 74.827041824480617, 31.635960667762667 ], [ 74.827029300591931, 31.635989383451243 ], [ 74.827087332076132, 31.636009231054345 ], [ 74.827100227961509, 31.635980568158097 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 10, "type": "Corridor", "name": "lecture theater corridor", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.826781922790687, 31.636055471730238 ], [ 74.826943617353493, 31.636033934979949 ], [ 74.826953041269718, 31.6360892548579 ], [ 74.826992224921455, 31.63608503196761 ], [ 74.826985280983166, 31.636053782573534 ], [ 74.826998672864136, 31.636052304561382 ], [ 74.82701355273187, 31.636019365998983 ], [ 74.827022232654727, 31.636021477445635 ], [ 74.827034136548917, 31.635994239780068 ], [ 74.827025952621668, 31.6359908614644 ], [ 74.827038848507044, 31.635959823183477 ], [ 74.827022480652545, 31.635942087018297 ], [ 74.827001648837694, 31.635958134025046 ], [ 74.826873681975101, 31.635823634685565 ], [ 74.826895505781124, 31.63580864338136 ], [ 74.826879633922189, 31.635791329477719 ], [ 74.82684218625505, 31.635795552381346 ], [ 74.826840698268271, 31.635788795735461 ], [ 74.826806722570268, 31.63579322978438 ], [ 74.826809450546008, 31.63580167559126 ], [ 74.826768034914124, 31.635805898494414 ], [ 74.826763570953815, 31.635817722622217 ], [ 74.826729347258009, 31.635804842768643 ], [ 74.826717195366015, 31.635835669956201 ], [ 74.826776466839178, 31.635855728737145 ], [ 74.826751667059625, 31.635914215894758 ], [ 74.826719179348345, 31.635987694293341 ], [ 74.826781922790687, 31.636055471730238 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 11, "type": "room", "name": "room 3", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.827011072753905, 31.636112480751049 ], [ 74.827044552456329, 31.63610952472861 ], [ 74.827058688330681, 31.636075108174676 ], [ 74.827019008683365, 31.636061594923035 ], [ 74.827000904844283, 31.636063917513308 ], [ 74.827011072753905, 31.636112480751049 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 12, "type": "room", "name": "room4", "floor": 1 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.826755635024298, 31.635751634174294 ], [ 74.826714591389134, 31.635756279370252 ], [ 74.826702687494929, 31.635782778097301 ], [ 74.826756627015484, 31.635801570018675 ], [ 74.826762454963685, 31.635788267872474 ], [ 74.826755635024298, 31.635751634174294 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 13, "type": "Lecture theatre", "name": "lecture theater 1", "floor": 2 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.82662800915881, 31.635955996183803 ], [ 74.826717040367484, 31.635986189887031 ], [ 74.82677234387593, 31.635856969214199 ], [ 74.826684180659527, 31.635826669896456 ], [ 74.82662800915881, 31.635955996183803 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 14, "type": "Lecture theatre", "name": "lecture theater 2", "floor": 2 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.826956265241208, 31.636119501303959 ], [ 74.82693989738668, 31.636037788369439 ], [ 74.826784154770991, 31.636057424817622 ], [ 74.82679779464975, 31.636140193456864 ], [ 74.826956265241208, 31.636119501303959 ] ] ] ] } },
{ "type": "Feature", "properties": { "id": 15, "type": "Lecture theatre", "name": "lecture theater 3", "floor": 2 }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 74.82700140083999, 31.635954175059801 ], [ 74.827099731966001, 31.635883547091954 ], [ 74.826974741076953, 31.635757071164974 ], [ 74.826878393933328, 31.635824532052276 ], [ 74.82700140083999, 31.635954175059801 ] ] ] ] } },


Когда я отправляю запрос от почты http: // localhost: 8080/api/auth/buildings/upload
с body-> form-data-> key ("file"), значение (file_name.geojson), давая мне 400 ошибок

, значение (file_geojson).error Received file: lecture_theater.geojson
File content type: application/geo+json
File size: 23328
Hibernate:
insert
into
building
(institute_id, name, id)
values
(?, ?, ?)
Hibernate:
insert
into
floor
(building_id, level, id)
values
(?, ?, ?)
Hibernate:
insert
into
room
(floor_id, floor_level, geometry, name, type, id)
values
(?, ?, ?, ?, ?, ?)
2025-06-08T16:21:49.192+05:30 WARN 12892 --- [pinpoint] [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: XX000
2025-06-08T16:21:49.193+05:30 ERROR 12892 --- [pinpoint] [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: Invalid endian flag value encountered.```


Подробнее здесь: https://stackoverflow.com/questions/796 ... sing-sprin

Вернуться в «JAVA»