<jupyter_start><jupyter_text>Create grid points This notebook creates a grid of points across the study area to predict biophony based on the developed model.  import statements<jupyter_code>from geo.models import Boundary
import numpy
import pandas
import geopandas
import pyproj
from shapely.geometry import Point, Polygon
from geoalchemy2 import Geometry, WKTElement
from sqlalchemy import create_engine<jupyter_output><empty_output><jupyter_text>variable declarations<jupyter_code>database = create_engine('postgresql://user:password@host:port/database')
radius = 500
spacing = 200
width = 15000
height = 12000
point_crs = pyproj.Proj(init='EPSG:31254')  # MGI / Austria GK West
boundary = Boundary.objects.get(name = "study area").geometry.envelope<jupyter_output><empty_output><jupyter_text>create grid<jupyter_code>coords = numpy.array(boundary.coords[0])
bbox = dict()
bbox['left'] = coords[:, 0].min() - ((coords[:, 0].max() - coords[:, 0].min()) / 2)
bbox['bottom'] = coords[:, 1].min() - ((coords[:, 1].max() - coords[:, 1].min()) / 2)
bbox['right'] = bbox['left'] + width
bbox['top'] = bbox['bottom'] + height

rows = int(numpy.ceil((bbox['top'] - bbox['bottom']) / spacing))
columns = int(numpy.ceil((bbox['right'] - bbox['left']) / spacing))

grid = numpy.empty(shape=(rows, columns), dtype=numpy.ndarray)
x_start = bbox['left']
y_start = bbox['bottom']
for row in range(rows):
    for column in range(columns):
        grid[row, column] = [x_start + (spacing * column), y_start + (spacing * row)]<jupyter_output><empty_output><jupyter_text>create points<jupyter_code># create the points across the grid
points = [Point(p[0], p[1]) for p in grid.ravel()]
# create a dataframe with the point index
ids = pandas.DataFrame({'id':numpy.arange(rows * columns), }).set_index('id')
# create a geodataframe with the index and the points
grid_points = geopandas.GeoDataFrame(ids, crs=point_crs, geometry=points)
# prepare the geometry for the database
grid_points['geom'] = grid_points['geometry'].apply(lambda x: WKTElement(x.wkt, srid=31254))
# drop the geometry column as it is now duplicative
grid_points.drop('geometry', 1, inplace=True)<jupyter_output><empty_output><jupyter_text>save grid points to the database<jupyter_code>grid_points.to_sql(name='geo_grid_points', 
                   con=database, 
                   if_exists='replace', 
                   dtype={'geom':Geometry('POINT', srid=31254)})<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14408163265306123, 'fraction_numerical': 0.017959183673469388, 'mean_word_length': 5.75206611570248, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '148541', 'license': 'MIT', 'n_tokens_mistral': 870, 'n_tokens_neox': 816, 'n_words': 197, 'path': 'utilities/Create grid points.ipynb', 'repo_name': 'jacobdein/alpine-soundscapes', 'size': '2450'}