'Helper routine for constructing the SQL to select the geographic
column.  Takes into account if the geographic field is in a
ForeignKey relation to the current model.'

def _geocol_select(self, geo_field, field_name):

    opts = self.model._meta
    if (not (geo_field in opts.fields)):
        self.query.add_select_related([field_name])
        compiler = self.query.get_compiler(self.db)
        compiler.pre_sql_setup()
        for ((rel_table, rel_col), field) in self.query.related_select_cols:
            if (field == geo_field):
                return compiler._field_column(geo_field, rel_table)
        raise ValueError(('%r    not    in    self.query.related_select_cols' % geo_field))
    elif (not (geo_field in opts.local_fields)):
        (tmp_fld, parent_model, direct, m2m) = opts.get_field_by_name(geo_field.name)
        return self.query.get_compiler(self.db)._field_column(geo_field, parent_model._meta.db_table)
    else:
        return self.query.get_compiler(self.db)._field_column(geo_field)
