Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions dataFromMongoDB
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# main.py
from fastapi import FastAPI, HTTPException
import motor.motor_asyncio
import uvicorn

app = FastAPI()

# Connect to MongoDB (adjust the connection string as needed)
client = motor.motor_asyncio.AsyncIOMotorClient("mongodb://localhost:27017")
db = client.news # use your database name

@app.get("/geojson")
async def get_geojson():
try:
# Retrieve all documents from the articles collection
cursor = db.articles.find({})
articles = await cursor.to_list(length=1000)

# Convert each document to a GeoJSON Feature
features = []
for article in articles:
geo = article.get("geoJson")
if not geo:
continue # Skip if no geoJson field is present

feature = {
"type": "Feature",
"geometry": geo.get("geometry"),
"properties": {
"title": article.get("title"),
"description": article.get("description"),
"url": article.get("url"),
"publishedAt": article.get("publishedAt"),
#"city": article.get("city"),
#"name": geo.get("properties", {}).get("name")
}
}
features.append(feature)

# Assemble the FeatureCollection
feature_collection = {
"type": "FeatureCollection",
"features": features
}

return feature_collection
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))

if __name__ == "__main__":
# Run the server on port 3040 (or change as needed)
uvicorn.run("main:app", host="0.0.0.0", port=3040, reload=True)