Generating data models

After configuring GeoDjango, a new available function in manage.py is available, ogrinspect, which automatically generates data table models with geometry columns that can be placed in models.py. By inspecting or reading the shapefile data using OGR, Django's built-in functionality creates a Python class data model and a field mapping dictionary that maps between the names of shapefile fields and database columns. 

For this section, we will use the shapefiles downloaded in Chapter 11, Flask and GeoAlchemy2. They are also available in the code package. Copy the four shapefiles (and all of the associated files) into a folder called data within the arenas application folder:

Open a command prompt, and change the directory to the project folder. The data folder with the four shapefiles (Arenas_NBA.shp, US_States.shp, US_County_Boundaries.shp, and Congressional_Districts.shp) will be inspected to generate data models using manage.py. The results are copied to models.py. From these models, the database tables will be generated, and then the tables will be populated using the field mapping dictionaries:

C:Projectschapter12>python manage.py ogrinspect arenasdataArenas_NBA.shp Arenas --srid=4326 --mapping

This command will produce a data model with a geometry column and a 4326 SRID. The field mapping dictionary, generated by the --mapping option, is a Python dictionary that maps between the keys (data model column names) and the values (shapefile field names). This is a part of the output:

Copy the output, including the import line, data model, and the field mapping dictionary into arenasmodels.py. Copy the import line over the data model class definition over the auto-generated import line in models.py.

Copying from the command line is easy when the Quick Edit option is turned on in the command prompt defaults. Once it is on, select text by dragging the mouse. Push Enter when the text block has been selected.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset