or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

appengine-integration.mdcore-admin.mdfile-admin.mdforms.mdgeoalchemy-integration.mdhelpers-utilities.mdindex.mdmodel-views.mdmongoengine-integration.mdredis-integration.mdsqlalchemy-integration.md

geoalchemy-integration.mddocs/

0

# GeoAlchemy Integration

1

2

Geographic data support for SQLAlchemy models through GeoAlchemy2 integration. Provides interactive map-based form fields and geographic data formatting for spatial database administration.

3

4

## Capabilities

5

6

### Geographic Model View

7

8

Enhanced SQLAlchemy model view with geographic data support and interactive map widgets.

9

10

```python { .api }

11

from flask_admin.contrib.geoa import ModelView

12

13

class ModelView(SQLAModelView):

14

"""

15

GeoAlchemy2-enabled model view for geographic data administration.

16

17

Attributes:

18

- model_form_converter: AdminModelConverter for geographic field conversion

19

- column_type_formatters: Geographic data formatters

20

- tile_layer_url: Optional custom tile layer URL for maps

21

- tile_layer_attribution: Attribution text for map tiles

22

"""

23

24

model_form_converter = AdminModelConverter

25

column_type_formatters = DEFAULT_FORMATTERS

26

tile_layer_url = None

27

tile_layer_attribution = None

28

```

29

30

### GeoJSON Form Field

31

32

Interactive map-based form field for editing geographic data with coordinate system transformations.

33

34

```python { .api }

35

from flask_admin.contrib.geoa.fields import GeoJSONField

36

37

class GeoJSONField(JSONField):

38

def __init__(

39

self,

40

label=None,

41

validators=None,

42

geometry_type="GEOMETRY",

43

srid='-1',

44

session=None,

45

tile_layer_url=None,

46

tile_layer_attribution=None,

47

**kwargs

48

):

49

"""

50

Interactive geographic data field with map widget.

51

52

Parameters:

53

- label: str, Field label

54

- validators: list, Form validators

55

- geometry_type: str, Geographic geometry type (POINT, POLYGON, etc.)

56

- srid: str, Spatial Reference System Identifier

57

- session: SQLAlchemy session for coordinate transformations

58

- tile_layer_url: str, Custom map tile layer URL

59

- tile_layer_attribution: str, Map attribution text

60

"""

61

```

62

63

### Leaflet Map Widget

64

65

Interactive Leaflet-based map widget for geographic data visualization and editing.

66

67

```python { .api }

68

from flask_admin.contrib.geoa.widgets import LeafletWidget

69

70

class LeafletWidget(TextArea):

71

def __init__(

72

self,

73

width='auto',

74

height=350,

75

center=None,

76

zoom=None,

77

min_zoom=None,

78

max_zoom=None,

79

max_bounds=None,

80

tile_layer_url=None,

81

tile_layer_attribution=None

82

):

83

"""

84

Leaflet map widget for geographic data editing.

85

86

Parameters:

87

- width: Map width (auto or pixel value)

88

- height: int, Map height in pixels

89

- center: tuple, Map center coordinates (lat, lng)

90

- zoom: int, Initial zoom level

91

- min_zoom: int, Minimum zoom level

92

- max_zoom: int, Maximum zoom level

93

- max_bounds: Bounding box for map limits

94

- tile_layer_url: str, Custom tile server URL

95

- tile_layer_attribution: str, Attribution text

96

"""

97

```

98

99

### Form Converter

100

101

Automatic conversion of GeoAlchemy2 geographic column types to appropriate form fields.

102

103

```python { .api }

104

from flask_admin.contrib.geoa.form import AdminModelConverter

105

106

class AdminModelConverter(SQLAAdminConverter):

107

@converts('Geography', 'Geometry')

108

def convert_geom(self, column, field_args, **extra):

109

"""

110

Convert GeoAlchemy2 geographic columns to GeoJSON form fields.

111

112

Parameters:

113

- column: GeoAlchemy2 column definition

114

- field_args: dict, Field configuration arguments

115

- extra: Additional conversion parameters

116

117

Returns:

118

GeoJSONField: Configured geographic form field

119

"""

120

```

121

122

### Geographic Data Formatting

123

124

Type formatters for displaying geographic data in list and detail views.

125

126

```python { .api }

127

from flask_admin.contrib.geoa.typefmt import DEFAULT_FORMATTERS

128

129

# Geographic data type formatters

130

DEFAULT_FORMATTERS = {

131

# Automatic formatting for GeoAlchemy2 geometric types

132

# Converts geometric data to human-readable formats

133

}

134

```

135

136

## Usage Examples

137

138

### Basic Geographic Model View

139

140

```python

141

from flask import Flask

142

from flask_admin import Admin

143

from flask_admin.contrib.geoa import ModelView

144

from flask_sqlalchemy import SQLAlchemy

145

from geoalchemy2 import Geometry

146

147

app = Flask(__name__)

148

db = SQLAlchemy(app)

149

admin = Admin(app)

150

151

# Geographic model

152

class Location(db.Model):

153

id = db.Column(db.Integer, primary_key=True)

154

name = db.Column(db.String(100))

155

coordinates = db.Column(Geometry('POINT'))

156

157

# Add geographic model view

158

admin.add_view(ModelView(Location, db.session, name='Locations'))

159

```

160

161

### Custom Map Configuration

162

163

```python

164

class LocationView(ModelView):

165

# Custom tile layer (e.g., OpenStreetMap)

166

tile_layer_url = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'

167

tile_layer_attribution = '© OpenStreetMap contributors'

168

169

# Form field customization

170

form_overrides = {

171

'coordinates': GeoJSONField

172

}

173

174

form_args = {

175

'coordinates': {

176

'geometry_type': 'POINT',

177

'srid': '4326', # WGS84

178

'tile_layer_url': tile_layer_url,

179

'tile_layer_attribution': tile_layer_attribution

180

}

181

}

182

183

admin.add_view(LocationView(Location, db.session))

184

```

185

186

### Advanced Geographic Features

187

188

```python

189

class AdvancedGeoView(ModelView):

190

# Custom map widget configuration

191

form_widget_args = {

192

'boundary': {

193

'data-geometry-type': 'POLYGON',

194

'data-zoom': 10,

195

'data-lat': 40.7128,

196

'data-lng': -74.0060,

197

'data-min-zoom': 5,

198

'data-max-zoom': 18

199

}

200

}

201

202

# Geographic column formatting

203

column_formatters = {

204

'coordinates': lambda v, c, m, p: f"Point({m.coordinates.x}, {m.coordinates.y})" if m.coordinates else "No location"

205

}

206

207

# Model with multiple geographic columns

208

class Region(db.Model):

209

id = db.Column(db.Integer, primary_key=True)

210

name = db.Column(db.String(100))

211

center_point = db.Column(Geometry('POINT'))

212

boundary = db.Column(Geometry('POLYGON'))

213

214

admin.add_view(AdvancedGeoView(Region, db.session, name='Regions'))

215

```

216

217

## Requirements

218

219

The GeoAlchemy integration requires additional dependencies:

220

221

```bash

222

pip install geoalchemy2 shapely

223

```

224

225

These packages provide:

226

- **GeoAlchemy2**: SQLAlchemy extension for geographic data

227

- **Shapely**: Geometric object manipulation and analysis