'OnValidation callback:
If a WKT is defined: validate the format,
calculate the LonLat of the Centroid, and set bounds
Else if a LonLat is defined: calculate the WKT for the Point.'

@staticmethod
def wkt_centroid(form):

    form_vars = form.vars
    if (form_vars.get('gis_feature_type', None) == '1'):
        lat = form_vars.get('lat', None)
        lon = form_vars.get('lon', None)
        if (((lon is None) and (lat is None)) or ((lon == '') and (lat == ''))):
            return
        elif ((lat is None) or (lat == '')):
            form.errors['lat'] = current.messages.lat_empty
        elif ((lon is None) or (lon == '')):
            form.errors['lon'] = current.messages.lon_empty
        else:
            form_vars.wkt = ('POINT(%(lon)s    %(lat)s)' % form_vars)
            radius = form_vars.get('radius', None)
            if radius:
                bbox = GIS.get_bounds_from_radius(lat, lon, radius)
                form_vars.lat_min = bbox['lat_min']
                form_vars.lon_min = bbox['lon_min']
                form_vars.lat_max = bbox['lat_max']
                form_vars.lon_max = bbox['lon_max']
            else:
                if (('lon_min' not in form_vars) or (form_vars.lon_min is None)):
                    form_vars.lon_min = lon
                if (('lon_max' not in form_vars) or (form_vars.lon_max is None)):
                    form_vars.lon_max = lon
                if (('lat_min' not in form_vars) or (form_vars.lat_min is None)):
                    form_vars.lat_min = lat
                if (('lat_max' not in form_vars) or (form_vars.lat_max is None)):
                    form_vars.lat_max = lat
    else:
        wkt = form_vars.get('wkt', None)
        if wkt:
            if (wkt[0] == '{'):
                from shapely.geometry import shape as shape_loads
                try:
                    js = json.load(wkt)
                    shape = shape_loads(js)
                except:
                    form.errors['wkt'] = current.messages.invalid_wkt
                    return
                else:
                    form_vars.wkt = shape.wkt
            else:
                from shapely.wkt import loads as wkt_loads
                try:
                    shape = wkt_loads(wkt)
                except:
                    try:
                        linestring = ('LINESTRING%s' % wkt[8:(-1)])
                        shape = wkt_loads(linestring)
                        form_vars.wkt = linestring
                    except:
                        form.errors['wkt'] = current.messages.invalid_wkt
                        return
                if shape.has_z:
                    form_vars.wkt = shape.wkt
                    current.session.warning = current.T('Only    2D    geometry    stored    as    PostGIS    cannot    handle    3D    geometries')
            gis_feature_type = shape.type
            if (gis_feature_type == 'Point'):
                form_vars.gis_feature_type = 1
            elif (gis_feature_type == 'LineString'):
                form_vars.gis_feature_type = 2
            elif (gis_feature_type == 'Polygon'):
                form_vars.gis_feature_type = 3
            elif (gis_feature_type == 'MultiPoint'):
                form_vars.gis_feature_type = 4
            elif (gis_feature_type == 'MultiLineString'):
                form_vars.gis_feature_type = 5
            elif (gis_feature_type == 'MultiPolygon'):
                form_vars.gis_feature_type = 6
            elif (gis_feature_type == 'GeometryCollection'):
                form_vars.gis_feature_type = 7
            try:
                centroid_point = shape.centroid
                form_vars.lon = centroid_point.x
                form_vars.lat = centroid_point.y
                bounds = shape.bounds
                if ((gis_feature_type != 'Point') or ('lon_min' not in form_vars) or (form_vars.lon_min is None) or (form_vars.lon_min == form_vars.lon_max)):
                    form_vars.lon_min = bounds[0]
                    form_vars.lat_min = bounds[1]
                    form_vars.lon_max = bounds[2]
                    form_vars.lat_max = bounds[3]
            except:
                form.errors.gis_feature_type = current.messages.centroid_error
        else:
            lat = form_vars.get('lat', None)
            lon = form_vars.get('lon', None)
            if (((lon is None) and (lat is None)) or ((lon == '') and (lat == ''))):
                return
            else:
                form_vars.gis_feature_type = '1'
                if ((lat is None) or (lat == '')):
                    form.errors['lat'] = current.messages.lat_empty
                elif ((lon is None) or (lon == '')):
                    form.errors['lon'] = current.messages.lon_empty
                else:
                    form_vars.wkt = ('POINT(%(lon)s    %(lat)s)' % form_vars)
                    if (('lon_min' not in form_vars) or (form_vars.lon_min is None)):
                        form_vars.lon_min = lon
                    if (('lon_max' not in form_vars) or (form_vars.lon_max is None)):
                        form_vars.lon_max = lon
                    if (('lat_min' not in form_vars) or (form_vars.lat_min is None)):
                        form_vars.lat_min = lat
                    if (('lat_max' not in form_vars) or (form_vars.lat_max is None)):
                        form_vars.lat_max = lat
    if current.deployment_settings.get_gis_spatialdb():
        form_vars.the_geom = form_vars.wkt
