Commit a6303c05 authored by shinde's avatar shinde
Browse files

Rewrote the read_districts_function

parent bc179651
Pipeline #37299 passed with stage
in 2 minutes and 15 seconds
...@@ -95,9 +95,11 @@ class JapanDatabase(SpatialiteDatabase): ...@@ -95,9 +95,11 @@ class JapanDatabase(SpatialiteDatabase):
# Create table District # Create table District
sql_statement = "CREATE TABLE District (" sql_statement = "CREATE TABLE District ("
sql_statement += "id INTEGER PRIMARY KEY AUTOINCREMENT, " sql_statement += "id INTEGER PRIMARY KEY, "
sql_statement += "admin_id TEXT, " # admin_id is same as id and will be deleted later
sql_statement += "admin_name TEXT)" sql_statement += "admin_id INTEGER, "
sql_statement += "name TEXT, "
sql_statement += "area_size REAL)"
self.connection.execute(sql_statement) self.connection.execute(sql_statement)
sql_statement = "SELECT AddGeometryColumn('District', 'geom', 4326, " sql_statement = "SELECT AddGeometryColumn('District', 'geom', 4326, "
sql_statement += "'MULTIPOLYGON', 'XY')" sql_statement += "'MULTIPOLYGON', 'XY')"
...@@ -223,24 +225,6 @@ class JapanDatabase(SpatialiteDatabase): ...@@ -223,24 +225,6 @@ class JapanDatabase(SpatialiteDatabase):
self.connection.execute(sql_statement) self.connection.execute(sql_statement)
logger.debug("Table DwellingSizeType created") logger.debug("Table DwellingSizeType created")
def insert_district(self, admin_id, admin_name, geom):
"""
Inserts the ID, name, and geometry of a district to the District table.
Args:
admin_id (str):
ID of the administrative district
admin_name (str):
Name of the administrative district
geom (multipolygon):
Geometry (Spatialite) of the district boundary
"""
sql_statement = "INSERT INTO District "
sql_statement += "(admin_id, admin_name, geom) "
sql_statement += "VALUES ('%s', '%s', %s)" % (admin_id, admin_name, geom)
self.cursor.execute(sql_statement)
def insert_dwelling_number( def insert_dwelling_number(
self, self,
district_id, district_id,
...@@ -657,23 +641,49 @@ class JapanDatabase(SpatialiteDatabase): ...@@ -657,23 +641,49 @@ class JapanDatabase(SpatialiteDatabase):
File path to the boundary file File path to the boundary file
""" """
boundary_db = SpatialiteDatabase(district_boundary_filepath, self.spatialite_filepath) # Attach geopackage boundary database to the Japan database
boundary_db.connect(init_spatial_metadata=False) sql_statement = "SELECT EnableGpkgAmphibiousMode();"
self.connection.execute(sql_statement)
sql_statement = "SELECT key_code_ward, " sql_statement = "ATTACH '%s' AS BoundaryDatabase" % district_boundary_filepath
sql_statement += "CITY_NAME, " logger.debug(sql_statement)
sql_statement += "AsWKT(CastAutomagic(geom)) " self.connection.execute(sql_statement)
sql_statement += "FROM Boundary"
boundary_db.cursor.execute(sql_statement) # From the boundary database select admin_id, name and geometry
for row in boundary_db.cursor: # and insert into Districts table in the Japan database
self.insert_district( sql_statement = "INSERT INTO District (id, admin_id, name, geom) "
row[0], sql_statement += "SELECT DISTINCT key_code_ward, key_code_ward, CITY_NAME, "
row[1], sql_statement += (
"ST_Transform(CastToMultiPolygon(GeomFromText('%s', %d)), %d)" "ST_Transform(CastToMultiPolygon(GeomFromText(AsWKT(CastAutomagic(geom)), %d)), "
% (row[2], constants.JGD2000, constants.WGS84), "%d) " % (constants.JGD2000, constants.WGS84)
)
sql_statement += "FROM BoundaryDatabase.Boundary"
logger.debug(sql_statement)
self.cursor.execute(sql_statement)
self.connection.commit()
# Calculate the area for each district using Albers equal area transformation
# and update the Districts table
sql_statement = "SELECT id, ST_AsText(geom) FROM District"
self.cursor.execute(sql_statement)
districts = self.cursor.fetchall()
for district in districts:
district_geom = shapely.wkt.loads(district[1])
district_size = (
self.albers_area_calculation(district_geom, "epsg:%d" % constants.WGS84)
* 0.000001 # Convert from square meters to square kilometers
)
sql_statement = "UPDATE District SET area_size = %f WHERE id = %s" % (
district_size,
district[0],
) )
logger.debug("District %d with boundary added" % row[0]) logger.debug(sql_statement)
self.cursor.execute(sql_statement)
self.connection.commit() self.connection.commit()
# Detach geopackage boundary database from the Japan database
sql_statement = "DETACH DATABASE 'BoundaryDatabase'"
logger.debug(sql_statement)
self.connection.execute(sql_statement)
logger.info("Districts and boundaries added") logger.info("Districts and boundaries added")
def get_district_id(self, admin_id): def get_district_id(self, admin_id):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment