Skip to content

Commit

Permalink
Fixed Frontend requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuhoi committed Jul 21, 2023
1 parent 955fcd7 commit 1ade2ab
Show file tree
Hide file tree
Showing 9 changed files with 30,980 additions and 244 deletions.
61 changes: 26 additions & 35 deletions backend/core/init_data_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,36 @@
DATA_DIR = pathlib.Path(__file__).parent.parent.joinpath("data")


async def __spb_example_init_data():
with open(DATA_DIR.joinpath("saint_pet.geojson"), "r") as f:
async def init_geo_data():
with open(DATA_DIR.joinpath("geo_data.geojson"), "r") as f:
data = json.load(f)

districts = []
for dist in data["features"]:
props = dist["properties"]
districts.append(
District(
title=props["district"],
properties=District_property(
population=props["population"],
area=props["area"],
),
geom=dist["geometry"],
)
)
city = City(
title="Санкт-Петербург",
properties=City_property(population=5600044, area=1439),
districts=districts,
blocks=[],
geom=gsa.shape.from_shape(shapely.geometry.MultiPolygon(), srid=4326),
)
async with async_session() as db:
db.add(city)
await db.commit()
for city in data["features"]:
districts = []
for dist in city["districts"]["features"]:
dist_props = dist["properties"]
dist_title = dist_props.pop("title")
districts.append(
District(
title=dist_title,
properties=District_property(**dist_props),
geom=dist["geometry"],
)
)
city_props = city["properties"]
city_title = city_props.pop("title")
city_row = City(
title=city_title,
properties=City_property(**city_props),
districts=districts,
blocks=[],
geom=city["geometry"],
)
db.add(city_row)
await db.commit()
print("Successful init data!")

# for i, dist in enumerate(data["features"]):
# if dist["geometry"]["type"] == "Polygon":
# poly = shapely.geometry.shape(dist["geometry"])
# mpoly = shapely.geometry.MultiPolygon([poly])
# gjson = geopandas.GeoSeries([mpoly]).__geo_interface__
# data["features"][i]["geometry"] = gjson["features"][0]["geometry"]
# print(data)
# with open(DATA_DIR.joinpath("saint_pet.geojson"), "w") as f:
# json.dump(data, f, ensure_ascii=False)


async def init_test_admin_user():
async with async_session() as db:
Expand All @@ -72,7 +63,7 @@ async def init_data():
if (await get_count(db, User)) == 0:
await init_test_admin_user()
if (await get_count(db, City)) == 0:
await __spb_example_init_data()
await init_geo_data()


async def _init_all_db():
Expand Down
44 changes: 32 additions & 12 deletions backend/core/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,22 @@ def get_blocks_geojson(city_id: int):
sa.func.json_build_object(
"title",
Block.title,
"population",
Block_property.population,
"area",
Block_property.area,
"common_area",
Block_property.common_area,
"beers_per_square_km",
Block_property.beers_per_square_km,
"shop_numbers",
Block_property.shop_numbers,
"green_area",
Block_property.green_area,
"station_numbers",
Block_property.station_numbers,
"avg_altitude_apartments",
Block_property.avg_altitude_apartments,
"garage_area",
Block_property.garage_area,
"retail_area",
Block_property.retail_area,
),
"geometry",
sa.func.ST_AsGeoJSON(Block.geom).cast(sa.JSON),
Expand Down Expand Up @@ -109,10 +121,22 @@ def get_districts_geojson(city_id: int):
sa.func.json_build_object(
"title",
District.title,
"population",
District_property.population,
"area",
District_property.area,
"common_area",
District_property.common_area,
"beers_per_square_km",
District_property.beers_per_square_km,
"shop_numbers",
District_property.shop_numbers,
"green_area",
District_property.green_area,
"station_numbers",
District_property.station_numbers,
"avg_altitude_apartments",
District_property.avg_altitude_apartments,
"garage_area",
District_property.garage_area,
"retail_area",
District_property.retail_area,
),
"geometry",
sa.func.ST_AsGeoJSON(District.geom).cast(sa.JSON),
Expand Down Expand Up @@ -142,10 +166,6 @@ def get_cities_geojson():
sa.func.json_build_object(
"title",
City.title,
"population",
City_property.population,
"area",
City_property.area,
),
"geometry",
sa.func.ST_AsGeoJSON(City.geom).cast(sa.JSON),
Expand Down
26 changes: 18 additions & 8 deletions backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ class District_property(Base):
primary_key=True,
index=True,
)
population = sa.Column(sa.Integer, nullable=True)
area = sa.Column(sa.Float, nullable=False)
common_area = sa.Column(sa.Float, nullable=False)
beers_per_square_km = sa.Column(sa.Float)
shop_numbers = sa.Column(sa.Integer)
green_area = sa.Column(sa.Float)
station_numbers = sa.Column(sa.Integer)
avg_altitude_apartments = sa.Column(sa.Float)
garage_area = sa.Column(sa.Float)
retail_area = sa.Column(sa.Float)

@property
def title(self) -> str:
Expand All @@ -69,8 +75,14 @@ class Block_property(Base):
primary_key=True,
index=True,
)
population = sa.Column(sa.Integer, nullable=True)
area = sa.Column(sa.Float, nullable=False)
common_area = sa.Column(sa.Float, nullable=False)
beers_per_square_km = sa.Column(sa.Float)
shop_numbers = sa.Column(sa.Integer)
green_area = sa.Column(sa.Float)
station_numbers = sa.Column(sa.Integer)
avg_altitude_apartments = sa.Column(sa.Float)
garage_area = sa.Column(sa.Float)
retail_area = sa.Column(sa.Float)

@property
def title(self) -> str:
Expand All @@ -85,8 +97,6 @@ class City_property(Base):
city_id = sa.Column(
sa.ForeignKey("cities.id", ondelete="CASCADE"), primary_key=True, index=True
)
population = sa.Column(sa.Integer, nullable=True)
area = sa.Column(sa.Float, nullable=False)

@property
def title(self) -> str:
Expand All @@ -100,7 +110,7 @@ class District(Base):
__tablename__ = "districts"

id = sa.Column(sa.Integer, primary_key=True, index=True)
title = sa.Column(sa.String(120), nullable=False)
title = sa.Column(sa.String, nullable=False)
city_id = sa.Column(sa.ForeignKey("cities.id", ondelete="CASCADE"))
geom = sa.Column(
gsa.Geometry(geometry_type="MULTIPOLYGON", srid=4326), nullable=False
Expand All @@ -116,7 +126,7 @@ class District(Base):
class Block(Base):
__tablename__ = "blocks"
id = sa.Column(sa.Integer, primary_key=True, index=True)
title = sa.Column(sa.String(120), nullable=False)
title = sa.Column(sa.String, nullable=False)
city_id = sa.Column(sa.ForeignKey("cities.id", ondelete="CASCADE"))
geom = sa.Column(
gsa.Geometry(geometry_type="MULTIPOLYGON", srid=4326), nullable=False
Expand Down
19 changes: 16 additions & 3 deletions backend/core/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,26 @@ class LoginResponse(BaseModel):

class Properties(BaseModel):
# title: str
population: int = Field(ge=0, description="Население")
area: float = Field(gt=0, description="Площадь(км^2)")
common_area: float = Field(gt=0, description="Общая площадь(км^2)")
beers_per_square_km: float = Field(
ge=0, description="Количестов пивных магазинов на км^2"
)
shop_numbers: int = Field(ge=0, description="Количество магазинов")
green_area: float = Field(ge=0, description="Площадь озелененной территории")
station_numbers: int = Field(ge=0, description="Количество остановок")
avg_altitude_apartments: float | None = Field(description="Высотность зданий")
garage_area: float = Field(ge=0, description="Площадь гаражных территорий")
retail_area: float = Field(ge=0, description="Площадь рынков и овощебаз")

class Config:
from_attributes = True


class PropertiesCity(BaseModel):
class Config:
from_attributes = True


class Geometry(BaseModel):
type: str
coordinates: list
Expand Down Expand Up @@ -121,7 +134,7 @@ class Config:

class City(BaseModel):
title: str = Field(description="Название города")
properties: Properties
properties: PropertiesCity
districts: list[DistrictList] | None
blocks: list[BlockList] | None
geometry: Geometry
Expand Down
Loading

0 comments on commit 1ade2ab

Please sign in to comment.