You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
900 B
29 lines
900 B
from django.core.management.base import BaseCommand
|
|
from service.models import Polygon
|
|
import shapefile
|
|
from django.contrib.gis.geos import Polygon as GeoPolygon
|
|
from tqdm import tqdm
|
|
|
|
|
|
def import_poly(file_path):
|
|
Polygon.objects.all().delete()
|
|
poly_models = []
|
|
shape = shapefile.Reader(file_path)
|
|
shape_records = list(shape.shapeRecords())
|
|
for i in tqdm(shape_records):
|
|
polygon = i.shape.__geo_interface__
|
|
data = i.__geo_interface__['properties']
|
|
geom = GeoPolygon(polygon['coordinates'][0], srid=4326)
|
|
poly_models.append(Polygon(geometry=geom, **data))
|
|
Polygon.objects.bulk_create(poly_models, batch_size=10000)
|
|
|
|
|
|
class Command(BaseCommand):
|
|
requires_system_checks = False
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument('-f', '--file', type=str)
|
|
|
|
def handle(self, file, *args, **options):
|
|
import_poly(file)
|