Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

postgresql geojson points distance and typeorm

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { TestLocation } from 'src/model/testlocation.entity';
import { getManager, QueryBuilder, Repository } from 'typeorm';
import { Geometry, Point } from 'geojson';
@Injectable()
export class LocationService {
  constructor(
    @InjectRepository(TestLocation) private readonly repo: Repository<TestLocation>,
  ) {}

  public async getAll() {
    return await this.repo.find();
  }
  
  public async create(location:TestLocation){
    const pointObject :Point= {
      type: "Point",
      coordinates: [location.long,location.lat]
  };
  location.location = pointObject;
  return await this.repo.save(location)
  }

  public async getRange(lat:number,long:number,range:number = 1000) {
    let origin = {
      type: "Point",
      coordinates: [long, lat]
    };
   let  locations = await this.repo
        .createQueryBuilder('t_test_location')
        .select(['t_test_location.city AS city','ST_Distance(location, ST_SetSRID(ST_GeomFromGeoJSON(:origin), ST_SRID(location)))/1000 AS distance' ])
        .where("ST_DWithin(location, ST_SetSRID(ST_GeomFromGeoJSON(:origin), ST_SRID(location)) ,:range)")
        .orderBy("distance","ASC")
        .setParameters({
           // stringify GeoJSON
          origin: JSON.stringify(origin),
          range:range*1000 //KM conversion
        })
       .getRawMany();
    return locations;
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript array of mixed type 
Typescript :: output products from collection 
Typescript :: no database host was found that meets the requirements for this server. 
Typescript :: typescript get string value of enum 
Typescript :: function which calculates the number of tweets that were posted per day. 
Typescript :: From the three types of styling, which one is the most useful in terms of website optimization? 
Typescript :: does photons travel with suitcases? 
Typescript :: input adresse ville automatique 
Typescript :: develop an algorithm that prints 2 numbers so that one is a multiple of the other 
Typescript :: how to use the pokeapi with javascript 
Typescript :: how to get the corners of 2 points on a matrix 
Typescript :: typescript dynamic array key 
Typescript :: laravel orm fetures 
Typescript :: typescript timeout browser 
Typescript :: language 
Typescript :: check if an element exists laravel 
Typescript :: The Apple I had a built-in video terminal, sockets for __________ kilobytes of onboard random access memory (RAM), a keyboard, and a cassette board meant to work with regular cassette recorders. 
Typescript :: ReturnType FunctionName(FormalParameterList) { Statements ; return ReturnValue; } 
Typescript :: acces arrey lements without comma 
Typescript :: vector with N equal entries R 
Typescript :: how to keep the state of my widgets after scrolling? flutter 
Typescript :: calculate checksum typescript 
Typescript :: flutter firestore collection snapshots queries tutorial 
Typescript :: whats the next sonic game 
Typescript :: how to read web page in type script 
Typescript :: Error: "Filesystem" plugin is not implemented on android 
Typescript :: install beats on rasberry 
Typescript :: testing tools vs testing techniques 
Typescript :: typescript list 
Typescript :: type char typescript 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =