如何将 GeoJSON 多边形数据发送到 typeorm (NestJS)?
How can I send GeoJSON polygon data into typeorm (NestJS)?
我想通过 POST 请求将 GeoJSON 多边形数据发送到 PostgresSQL。
因此,我尝试接收 Position[][] 类型并将其转换为 Polygon 类型,然后通过 Postman 发送 POST 请求以进行 API test 但 我收到一个错误:“QueryFailedError:无法在 GeoJSON 字符串中找到 'coordinates'”。
有我的代码:
实体
import { Column, Entity, Index, PrimaryGeneratedColumn } from "typeorm";
import { Polygon } from "geojson";
@Entity({ name: 'parcels' })
export class Parcel {
@PrimaryGeneratedColumn('uuid')
id: string
@Index({ spatial: true })
@Column({
type: 'geography',
spatialFeatureType: 'Polygon',
srid:4326,
nullable: true
})
polygon: Polygon
}
Dto
import { IsOptional } from "class-validator";
import { Position } from "geojson";
export class CreateParcelPointDto {
@IsOptional()
position?: Position[][]
}
控制器
import { Body, Controller, Post } from '@nestjs/common';
import { CreateParcelPointDto } from './dto/create-parcel-point.dto';
import { Parcel } from './parcel.entity';
import { ParcelService } from './parcel.service';
@Controller('parcels')
export class ParcelController {
constructor(private parcelService: ParcelService) {}
@Post()
async createParcelPoint(
@Body()
createParcelPointDto: CreateParcelPointDto
): Promise<Parcel> {
return this.parcelService.createParcelPoint(createParcelPointDto)
}
}
服务
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Polygon } from 'geojson';
import { CreateParcelPointDto } from './dto/create-parcel-point.dto';
import { ParcelRepository } from './parcel.repository';
import { Parcel } from './parcel.entity';
@Injectable()
export class ParcelService {
constructor(
@InjectRepository(ParcelRepository)
private parcelRepository: ParcelRepository
) {}
async createParcelPoint(createParcelPointDto: CreateParcelPointDto): Promise<Parcel> {
const { position } = createParcelPointDto
const polygon: Polygon = {
type: 'Polygon',
coordinates: position
}
const parcel = this.parcelRepository.create({
polygon,
})
await this.parcelRepository.save(parcel)
return parcel
}
}
POST请求JSON
{
"polygon" : [
[ 102.016680916961207, 14.876721809875564 ],
[ 102.016926580451127, 14.876676829236565 ],
[ 102.016936960598585, 14.876688939408604 ],
[ 102.017125533277465, 14.876656068941644 ],
[ 102.017130723351187, 14.876638768695875 ],
[ 102.017360816619913, 14.876598978130607 ],
[ 102.017243174948689, 14.87595713901259 ],
[ 102.017000971507926, 14.876002119651588 ],
[ 102.016994051409625, 14.875983089381243 ],
[ 102.016789908509551, 14.876022879946511 ],
[ 102.016786448460394, 14.876047100290586 ],
[ 102.016559815240825, 14.876090350905008 ],
[ 102.016680916961207, 14.876721809875564 ]
],
}
我不知道如何通过 POST 请求处理 GeoJSON 类型到 typeorm 中。如果有人有解决办法,请帮助我。
我已经解决了这个问题。在这种情况下,您需要将 Dto 中的类型定义为数字。维度必须与位置类型相同,并且在创建多边形对象时在坐标上增加一维。
例如,Polygon类型需要接收Position[][]中的坐标。因此,创建Polygon对象时需要在Dto中定义多边形类型为number[][],坐标定义为[polygon]。
代码示例:
Dto
import { IsOptional } from "class-validator";
export class CreateParcelPointDto {
@IsOptional()
polygon?: number[][]
}
服务
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Polygon } from 'geojson';
import { CreateParcelPointDto } from './dto/create-parcel-point.dto';
import { ParcelRepository } from './parcel.repository';
import { Parcel } from './parcel.entity';
@Injectable()
export class ParcelService {
constructor(
@InjectRepository(ParcelRepository)
private parcelRepository: ParcelRepository
) {}
async createParcelPoint(createParcelPointDto: CreateParcelPointDto): Promise<Parcel> {
const {
polygon,
} = createParcelPointDto
const polygonCreated: Polygon = {
type: 'Polygon',
coordinates: [polygon] //Need one more dimension here.
}
const parcel = this.parcelRepository.create({
polygon: polygonCreated,
})
await this.parcelRepository.save(parcel)
return parcel
}
}
我想通过 POST 请求将 GeoJSON 多边形数据发送到 PostgresSQL。
因此,我尝试接收 Position[][] 类型并将其转换为 Polygon 类型,然后通过 Postman 发送 POST 请求以进行 API test 但 我收到一个错误:“QueryFailedError:无法在 GeoJSON 字符串中找到 'coordinates'”。
有我的代码:
实体
import { Column, Entity, Index, PrimaryGeneratedColumn } from "typeorm"; import { Polygon } from "geojson"; @Entity({ name: 'parcels' }) export class Parcel { @PrimaryGeneratedColumn('uuid') id: string @Index({ spatial: true }) @Column({ type: 'geography', spatialFeatureType: 'Polygon', srid:4326, nullable: true }) polygon: Polygon }
Dto
import { IsOptional } from "class-validator"; import { Position } from "geojson"; export class CreateParcelPointDto { @IsOptional() position?: Position[][] }
控制器
import { Body, Controller, Post } from '@nestjs/common'; import { CreateParcelPointDto } from './dto/create-parcel-point.dto'; import { Parcel } from './parcel.entity'; import { ParcelService } from './parcel.service'; @Controller('parcels') export class ParcelController { constructor(private parcelService: ParcelService) {} @Post() async createParcelPoint( @Body() createParcelPointDto: CreateParcelPointDto ): Promise<Parcel> { return this.parcelService.createParcelPoint(createParcelPointDto) } }
服务
import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Polygon } from 'geojson'; import { CreateParcelPointDto } from './dto/create-parcel-point.dto'; import { ParcelRepository } from './parcel.repository'; import { Parcel } from './parcel.entity'; @Injectable() export class ParcelService { constructor( @InjectRepository(ParcelRepository) private parcelRepository: ParcelRepository ) {} async createParcelPoint(createParcelPointDto: CreateParcelPointDto): Promise<Parcel> { const { position } = createParcelPointDto const polygon: Polygon = { type: 'Polygon', coordinates: position } const parcel = this.parcelRepository.create({ polygon, }) await this.parcelRepository.save(parcel) return parcel } }
POST请求JSON
{ "polygon" : [ [ 102.016680916961207, 14.876721809875564 ], [ 102.016926580451127, 14.876676829236565 ], [ 102.016936960598585, 14.876688939408604 ], [ 102.017125533277465, 14.876656068941644 ], [ 102.017130723351187, 14.876638768695875 ], [ 102.017360816619913, 14.876598978130607 ], [ 102.017243174948689, 14.87595713901259 ], [ 102.017000971507926, 14.876002119651588 ], [ 102.016994051409625, 14.875983089381243 ], [ 102.016789908509551, 14.876022879946511 ], [ 102.016786448460394, 14.876047100290586 ], [ 102.016559815240825, 14.876090350905008 ], [ 102.016680916961207, 14.876721809875564 ] ], }
我不知道如何通过 POST 请求处理 GeoJSON 类型到 typeorm 中。如果有人有解决办法,请帮助我。
我已经解决了这个问题。在这种情况下,您需要将 Dto 中的类型定义为数字。维度必须与位置类型相同,并且在创建多边形对象时在坐标上增加一维。
例如,Polygon类型需要接收Position[][]中的坐标。因此,创建Polygon对象时需要在Dto中定义多边形类型为number[][],坐标定义为[polygon]。
代码示例:
Dto
import { IsOptional } from "class-validator"; export class CreateParcelPointDto { @IsOptional() polygon?: number[][] }
服务
import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Polygon } from 'geojson'; import { CreateParcelPointDto } from './dto/create-parcel-point.dto'; import { ParcelRepository } from './parcel.repository'; import { Parcel } from './parcel.entity'; @Injectable() export class ParcelService { constructor( @InjectRepository(ParcelRepository) private parcelRepository: ParcelRepository ) {} async createParcelPoint(createParcelPointDto: CreateParcelPointDto): Promise<Parcel> { const { polygon, } = createParcelPointDto const polygonCreated: Polygon = { type: 'Polygon', coordinates: [polygon] //Need one more dimension here. } const parcel = this.parcelRepository.create({ polygon: polygonCreated, }) await this.parcelRepository.save(parcel) return parcel } }