Return the rows from database which matches the given longitude and lattitude against the geometry column : postgress with postgis enabled

first of all , I have dataset like this in json

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
87.62457877000008,
27.362144082000043
],
...
]
]
},
"properties": {
"STATE_CODE": 1,
"DISTRICT": "TAPLEJUNG",
"GaPa_NaPa": "Aathrai Tribeni",
"Type_GN": "Gaunpalika",
"Province": "1"
}
},
...
]
}
</code>
<code>{ "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [ 87.62457877000008, 27.362144082000043 ], ... ] ] }, "properties": { "STATE_CODE": 1, "DISTRICT": "TAPLEJUNG", "GaPa_NaPa": "Aathrai Tribeni", "Type_GN": "Gaunpalika", "Province": "1" } }, ... ] } </code>
{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            87.62457877000008,
                            27.362144082000043
                        ],
                        ...
                    ]
                ]
            },
            "properties": {
                "STATE_CODE": 1,
                "DISTRICT": "TAPLEJUNG",
                "GaPa_NaPa": "Aathrai Tribeni",
                "Type_GN": "Gaunpalika",
                "Province": "1"
            }
        },
        ...
    ]
}

I dumped the data using this function:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const knex = require("./connection");
const fs = require("fs");
const insertData = async () => {
console.log("Inserting municipality data");
const jsonData = JSON.parse(
fs.readFileSync("utils/municipality.json", "utf8")
);
const dataToInsert = jsonData.features.map((data) => {
const { properties, geometry } = data;
let {
STATE_CODE,
DISTRICT = "",
GaPa_NaPa,
Type_GN,
Province,
} = properties;
// Ensure DISTRICT is a string
DISTRICT = DISTRICT.toString();
Province = Province.toString();
GaPa_NaPa = GaPa_NaPa.toString();
return {
state_code: STATE_CODE,
district: DISTRICT,
gapa_napa: GaPa_NaPa,
type_gn: Type_GN,
province: Province,
geometry: knex.raw(
`ST_SetSRID(ST_GeomFromGeoJSON('${JSON.stringify(geometry)}'), 4326)`
),
};
});
try {
const tableExists = await knex.schema.hasTable("tbl_municipality");
if (tableExists) {
await knex("tbl_municipality").insert(dataToInsert);
console.log("Municipality data inserted");
} else {
console.log("tbl_municipality table does not exist");
}
} catch (err) {
console.error("Error inserting municipality data", err);
}
};
</code>
<code>const knex = require("./connection"); const fs = require("fs"); const insertData = async () => { console.log("Inserting municipality data"); const jsonData = JSON.parse( fs.readFileSync("utils/municipality.json", "utf8") ); const dataToInsert = jsonData.features.map((data) => { const { properties, geometry } = data; let { STATE_CODE, DISTRICT = "", GaPa_NaPa, Type_GN, Province, } = properties; // Ensure DISTRICT is a string DISTRICT = DISTRICT.toString(); Province = Province.toString(); GaPa_NaPa = GaPa_NaPa.toString(); return { state_code: STATE_CODE, district: DISTRICT, gapa_napa: GaPa_NaPa, type_gn: Type_GN, province: Province, geometry: knex.raw( `ST_SetSRID(ST_GeomFromGeoJSON('${JSON.stringify(geometry)}'), 4326)` ), }; }); try { const tableExists = await knex.schema.hasTable("tbl_municipality"); if (tableExists) { await knex("tbl_municipality").insert(dataToInsert); console.log("Municipality data inserted"); } else { console.log("tbl_municipality table does not exist"); } } catch (err) { console.error("Error inserting municipality data", err); } }; </code>
const knex = require("./connection");
const fs = require("fs");

const insertData = async () => {
  console.log("Inserting municipality data");
  const jsonData = JSON.parse(
    fs.readFileSync("utils/municipality.json", "utf8")
  );

  const dataToInsert = jsonData.features.map((data) => {
    const { properties, geometry } = data;
    let {
      STATE_CODE,
      DISTRICT = "",
      GaPa_NaPa,
      Type_GN,
      Province,
    } = properties;

    // Ensure DISTRICT is a string
    DISTRICT = DISTRICT.toString();
    Province = Province.toString();
    GaPa_NaPa = GaPa_NaPa.toString();

    return {
      state_code: STATE_CODE,
      district: DISTRICT,
      gapa_napa: GaPa_NaPa,
      type_gn: Type_GN,
      province: Province,
      geometry: knex.raw(
        `ST_SetSRID(ST_GeomFromGeoJSON('${JSON.stringify(geometry)}'), 4326)`
      ),
    };
  });

  try {
    const tableExists = await knex.schema.hasTable("tbl_municipality");
    if (tableExists) {
      await knex("tbl_municipality").insert(dataToInsert);
      console.log("Municipality data inserted");
    } else {
      console.log("tbl_municipality table does not exist");
    }
  } catch (err) {
    console.error("Error inserting municipality data", err);
  }
};

but i want to implement the getrequest based on lat and long

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
const getMunicipalityData = async function (req, res, next) {
let { lat, long } = req.query;
if (lat === undefined || long === undefined) {
lat = 27;
long = 87;
}
console.log(lat, long);
const rawQuery = `
SELECT *
FROM tbl_municipality
WHERE ST_Intersects(
geometry,
ST_Buffer(ST_SetSRID(ST_MakePoint(?, ?), 4326), 01)
)
;
try {
const result = await knex.raw(rawQuery, [long, lat]);
const data = result.rows;
console.log("Query result:", data);
res.status(200).json({ success: true, data });
} catch (error) {
console.error("Error querying the database:", error);
res.status(500).json({ success: false, message: "Database query failed" });
}
};
</code>
<code> const getMunicipalityData = async function (req, res, next) { let { lat, long } = req.query; if (lat === undefined || long === undefined) { lat = 27; long = 87; } console.log(lat, long); const rawQuery = ` SELECT * FROM tbl_municipality WHERE ST_Intersects( geometry, ST_Buffer(ST_SetSRID(ST_MakePoint(?, ?), 4326), 01) ) ; try { const result = await knex.raw(rawQuery, [long, lat]); const data = result.rows; console.log("Query result:", data); res.status(200).json({ success: true, data }); } catch (error) { console.error("Error querying the database:", error); res.status(500).json({ success: false, message: "Database query failed" }); } }; </code>


const getMunicipalityData = async function (req, res, next) {
  let { lat, long } = req.query;
  if (lat === undefined || long === undefined) {
    lat = 27;
    long = 87;
  }

  console.log(lat, long);
  const rawQuery = `
     SELECT *
    FROM tbl_municipality
    WHERE ST_Intersects(
      geometry,
      ST_Buffer(ST_SetSRID(ST_MakePoint(?, ?), 4326), 01)
    )
  ;

  try {
    const result = await knex.raw(rawQuery, [long, lat]);
    const data = result.rows;
    console.log("Query result:", data);
    res.status(200).json({ success: true, data });
  } catch (error) {
    console.error("Error querying the database:", error);
    res.status(500).json({ success: false, message: "Database query failed" });
  }
};

problem is : even i query the databased based on longitutue and lattitude from original json data, it returns empty list

i want the data which it corresponds to the given longitude and lattitude

New contributor

Samman Amgain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật