【问题标题】:How can I send GeoJSON polygon data into typeorm (NestJS)?如何将 GeoJSON 多边形数据发送到 typeorm (NestJS)?
【发布时间】:2022-01-04 12:53:29
【问题描述】:

我想通过 POST 请求将 GeoJSON 多边形数据发送到 PostgresSQL。

因此,我尝试接收 Position[][] 类型并将其转换为 Polygon 类型,然后由 Postman 发送 POST 请求以进行 API 测试,但 我收到错误消息:“ QueryFailedError:无法在 GeoJSON 字符串中找到“坐标”

有我的代码:

  • 实体

     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。如果有人有解决方案,请帮助我。

【问题讨论】:

    标签: postgresql nestjs postgis geojson typeorm


    【解决方案1】:

    我已经解决了这个问题。在这种情况下,您需要将 Dto 中的类型定义为数字。维度必须与 Position 类型相同,并在创建 Polygon 对象时在坐标中增加一维。

    例如,Polygon 类型需要接收 Position[][] 中的坐标。因此,您需要在 Dto 中将多边形定义为 number[][] 类型,并在创建 Polygon 对象时将坐标定义为 [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
           }
       }
      

    【讨论】:

      猜你喜欢
      • 2017-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 2021-09-01
      • 2017-03-15
      • 2022-12-22
      • 1970-01-01
      相关资源
      最近更新 更多