From b35014e77cf12e42ea260a52051b7f2563e586e0 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 4 Jul 2009 13:00:48 +0200 Subject: [PATCH] Added GtkGisGeometry::draw. Added GtkGis::zoom_to_max. Fixed some bugs. Now the widget draws points, lines and poligons. --- src/geometry.c | 60 ++++++++++++++-- src/geometry.h | 6 ++ src/geometryline.c | 87 +++++++++++++++++++++-- src/geometryline.h | 2 +- src/geometrypoint.c | 42 +++++++++-- src/geometrypolygon.c | 55 +++++++++++++-- src/geometrypolygon.h | 2 +- src/gtkgis.c | 157 +++++++++++++++++++++++++++++++++++------- src/gtkgis.h | 6 +- src/layer.c | 32 +++++++-- src/layer.h | 3 +- src/layersource.c | 135 +++++++++++++++++------------------- tests/from_xml.c | 2 + tests/test1.gtkgis | 8 +-- 14 files changed, 462 insertions(+), 135 deletions(-) diff --git a/src/geometry.c b/src/geometry.c index 78a3f0e..31f8aef 100644 --- a/src/geometry.c +++ b/src/geometry.c @@ -40,12 +40,13 @@ static void gtk_gis_geometry_get_property (GObject *object, GParamSpec *pspec); -/*#define GTK_GIS_GEOMETRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_GTK_GIS_GEOMETRY, GtkGisGeometryPrivate)) +#define GTK_GIS_GEOMETRY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_GTK_GIS_GEOMETRY, GtkGisGeometryPrivate)) typedef struct _GtkGisGeometryPrivate GtkGisGeometryPrivate; struct _GtkGisGeometryPrivate { - };*/ + gboolean editable; + }; G_DEFINE_TYPE (GtkGisGeometry, gtk_gis_geometry, G_TYPE_OBJECT) @@ -54,7 +55,7 @@ gtk_gis_geometry_class_init (GtkGisGeometryClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); - /*g_type_class_add_private (object_class, sizeof (GtkGisGeometryPrivate));*/ + g_type_class_add_private (object_class, sizeof (GtkGisGeometryPrivate)); object_class->set_property = gtk_gis_geometry_set_property; object_class->get_property = gtk_gis_geometry_get_property; @@ -63,7 +64,54 @@ gtk_gis_geometry_class_init (GtkGisGeometryClass *klass) static void gtk_gis_geometry_init (GtkGisGeometry *gtk_gis_geometry) { - /*GtkGisGeometryPrivate *priv = GTK_GIS_GEOMETRY_GET_PRIVATE (gtk_gis_geometry);*/ + GtkGisGeometryPrivate *priv = GTK_GIS_GEOMETRY_GET_PRIVATE (gtk_gis_geometry); + + priv->editable = FALSE; +} + +/** + * gtk_gis_geometry_get_editable: + * @geometry: + * + */ +gboolean +gtk_gis_geometry_get_editable (GtkGisGeometry *geometry) +{ + GtkGisGeometryPrivate *priv = GTK_GIS_GEOMETRY_GET_PRIVATE (geometry); + + return priv->editable; +} + +/** + * gtk_gis_geometry_set_editable: + * @geometry: + * @editable: + * + */ +void +gtk_gis_geometry_set_editable (GtkGisGeometry *geometry, gboolean editable) +{ + GtkGisGeometryPrivate *priv = GTK_GIS_GEOMETRY_GET_PRIVATE (geometry); + + priv->editable = editable; +} + +/** + * gtk_gis_geometry_draw: + * @geometry: + * + */ +GooCanvasItem +*gtk_gis_geometry_draw (GtkGisGeometry *geometry) +{ + GooCanvasItem *item = NULL; + + if (IS_GTK_GIS_GEOMETRY (geometry) && GTK_GIS_GEOMETRY_GET_CLASS (geometry)->draw != NULL) + { + item = GTK_GIS_GEOMETRY_GET_CLASS (geometry)->draw (geometry); + } + + return item; } static void @@ -71,7 +119,7 @@ gtk_gis_geometry_set_property (GObject *object, guint property_id, const GValue { GtkGisGeometry *gtk_gis_geometry = GTK_GIS_GEOMETRY (object); - /*GtkGisGeometryPrivate *priv = GTK_GIS_GEOMETRY_GET_PRIVATE (gtk_gis_geometry);*/ + GtkGisGeometryPrivate *priv = GTK_GIS_GEOMETRY_GET_PRIVATE (gtk_gis_geometry); switch (property_id) { @@ -86,7 +134,7 @@ gtk_gis_geometry_get_property (GObject *object, guint property_id, GValue *value { GtkGisGeometry *gtk_gis_geometry = GTK_GIS_GEOMETRY (object); - /*GtkGisGeometryPrivate *priv = GTK_GIS_GEOMETRY_GET_PRIVATE (gtk_gis_geometry);*/ + GtkGisGeometryPrivate *priv = GTK_GIS_GEOMETRY_GET_PRIVATE (gtk_gis_geometry); switch (property_id) { diff --git a/src/geometry.h b/src/geometry.h index ec59947..07606a5 100644 --- a/src/geometry.h +++ b/src/geometry.h @@ -23,6 +23,8 @@ #include #include +#include + G_BEGIN_DECLS @@ -45,6 +47,8 @@ struct _GtkGisGeometry struct _GtkGisGeometryClass { GObjectClass parent_class; + + GooCanvasItem *(*draw) (GtkGisGeometry *geometry); }; GType gtk_gis_geometry_get_type (void) G_GNUC_CONST; @@ -60,6 +64,8 @@ typedef struct gboolean gtk_gis_geometry_get_editable (GtkGisGeometry *geometry); void gtk_gis_geometry_set_editable (GtkGisGeometry *geometry, gboolean editable); +GooCanvasItem *gtk_gis_geometry_draw (GtkGisGeometry *geometry); + G_END_DECLS diff --git a/src/geometryline.c b/src/geometryline.c index c2cd977..0c39964 100644 --- a/src/geometryline.c +++ b/src/geometryline.c @@ -30,6 +30,10 @@ static void gtk_gis_geometry_line_class_init (GtkGisGeometryLineClass *klass); static void gtk_gis_geometry_line_init (GtkGisGeometryLine *gtk_gis_geometry_line); +static gchar *convert_comma_to_dot (gchar *str); + +static GooCanvasItem *gtk_gis_geometry_line_draw (GtkGisGeometry *line); + static void gtk_gis_geometry_line_set_property (GObject *object, guint property_id, const GValue *value, @@ -45,7 +49,7 @@ static void gtk_gis_geometry_line_get_property (GObject *object, typedef struct _GtkGisGeometryLinePrivate GtkGisGeometryLinePrivate; struct _GtkGisGeometryLinePrivate { - GPtrArray *points; + GSList *points; }; G_DEFINE_TYPE (GtkGisGeometryLine, gtk_gis_geometry_line, TYPE_GTK_GIS_GEOMETRY) @@ -54,11 +58,14 @@ static void gtk_gis_geometry_line_class_init (GtkGisGeometryLineClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkGisGeometryClass *geometry_class = GTK_GIS_GEOMETRY_CLASS (klass); g_type_class_add_private (object_class, sizeof (GtkGisGeometryLinePrivate)); object_class->set_property = gtk_gis_geometry_line_set_property; object_class->get_property = gtk_gis_geometry_line_get_property; + + geometry_class->draw = gtk_gis_geometry_line_draw; } static void @@ -66,7 +73,7 @@ gtk_gis_geometry_line_init (GtkGisGeometryLine *gtk_gis_geometry_line) { GtkGisGeometryLinePrivate *priv = GTK_GIS_GEOMETRY_LINE_GET_PRIVATE (gtk_gis_geometry_line); - priv->points = g_ptr_array_new (); + priv->points = NULL; } /** @@ -87,7 +94,7 @@ GtkGisGeometry } /** - * gtk_gis_geometry_line_new_from_array: + * gtk_gis_geometry_line_new_from_list: * @points: * * Creates a new #GtkGisGeometryLine object. @@ -95,7 +102,7 @@ GtkGisGeometry * Returns: the newly created #GtkGisGeometryLine object. */ GtkGisGeometry -*gtk_gis_geometry_line_new_from_array (GPtrArray *points) +*gtk_gis_geometry_line_new_from_list (GSList *points) { GtkGisGeometry *line = NULL; GtkGisGeometryLinePrivate *priv; @@ -104,7 +111,7 @@ GtkGisGeometry priv = GTK_GIS_GEOMETRY_LINE_GET_PRIVATE (GTK_GIS_GEOMETRY_LINE (line)); - priv->points = points; + priv->points = g_slist_copy (points); return line; } @@ -126,7 +133,7 @@ gtk_gis_geometry_line_add_point (GtkGisGeometryLine *line, GtkGisPoint point) gpoint->x = point.x; gpoint->y = point.y; - g_ptr_array_add (priv->points, gpoint); + priv->points = g_slist_append (priv->points, gpoint); } /** @@ -148,6 +155,74 @@ gtk_gis_geometry_line_add_point_from_coordinates (GtkGisGeometryLine *line, gtk_gis_geometry_line_add_point (line, gpoint); } +static gchar +*convert_comma_to_dot (gchar *str) +{ + /* convert , in . */ + gint len; + gint i; + gchar *ret = NULL; + + len = strlen (str); + + if (len > 0) + { + ret = g_malloc (len + 1); + strcpy (ret, str); + } + + for (i = 0; i < len; i++) + { + if (ret[i] == ',') + { + ret[i] = '.'; + } + } + + return ret; +} + +static GooCanvasItem +*gtk_gis_geometry_line_draw (GtkGisGeometry *line) +{ + GtkGisGeometryLinePrivate *priv; + GooCanvasItem *item = NULL; + GtkGisPoint *point; + + gchar *path; + + priv = GTK_GIS_GEOMETRY_LINE_GET_PRIVATE (GTK_GIS_GEOMETRY_LINE (line)); + + if (priv->points != NULL) + { + GSList *cur; + + cur = priv->points; + + point = (GtkGisPoint *)cur->data; + path = g_strdup_printf ("M %f %f", point->x, point->y); + + cur = g_slist_next (cur); + while (cur != NULL) + { + point = (GtkGisPoint *)cur->data; + path = g_strconcat (path, + g_strdup_printf (" L %f %f", point->x, point->y), + NULL); + + cur = g_slist_next (cur); + } + + path = convert_comma_to_dot (path); + item = goo_canvas_path_new (NULL, + path, + "line-width", 1.0, + NULL); + } + + return item; +} + static void gtk_gis_geometry_line_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { diff --git a/src/geometryline.h b/src/geometryline.h index 97d3d52..e967a9b 100644 --- a/src/geometryline.h +++ b/src/geometryline.h @@ -52,7 +52,7 @@ GType gtk_gis_geometry_line_get_type (void) G_GNUC_CONST; GtkGisGeometry *gtk_gis_geometry_line_new (void); -GtkGisGeometry *gtk_gis_geometry_line_new_from_array (GPtrArray *points); +GtkGisGeometry *gtk_gis_geometry_line_new_from_list (GSList *points); void gtk_gis_geometry_line_add_point (GtkGisGeometryLine *line, GtkGisPoint point); void gtk_gis_geometry_line_add_point_from_coordinates (GtkGisGeometryLine *line, diff --git a/src/geometrypoint.c b/src/geometrypoint.c index 8647b29..1a16c87 100644 --- a/src/geometrypoint.c +++ b/src/geometrypoint.c @@ -30,6 +30,8 @@ static void gtk_gis_geometry_point_class_init (GtkGisGeometryPointClass *klass); static void gtk_gis_geometry_point_init (GtkGisGeometryPoint *gtk_gis_geometry_point); +static GooCanvasItem *gtk_gis_geometry_point_draw (GtkGisGeometry *point); + static void gtk_gis_geometry_point_set_property (GObject *object, guint property_id, const GValue *value, @@ -45,7 +47,7 @@ static void gtk_gis_geometry_point_get_property (GObject *object, typedef struct _GtkGisGeometryPointPrivate GtkGisGeometryPointPrivate; struct _GtkGisGeometryPointPrivate { - GtkGisPoint point; + GtkGisPoint *point; }; G_DEFINE_TYPE (GtkGisGeometryPoint, gtk_gis_geometry_point, TYPE_GTK_GIS_GEOMETRY) @@ -54,17 +56,22 @@ static void gtk_gis_geometry_point_class_init (GtkGisGeometryPointClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkGisGeometryClass *geometry_class = GTK_GIS_GEOMETRY_CLASS (klass); g_type_class_add_private (object_class, sizeof (GtkGisGeometryPointPrivate)); object_class->set_property = gtk_gis_geometry_point_set_property; object_class->get_property = gtk_gis_geometry_point_get_property; + + geometry_class->draw = gtk_gis_geometry_point_draw; } static void gtk_gis_geometry_point_init (GtkGisGeometryPoint *gtk_gis_geometry_point) { GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (gtk_gis_geometry_point); + + priv->point = NULL; } /** @@ -85,8 +92,10 @@ GtkGisGeometry priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (GTK_GIS_GEOMETRY_POINT (gpoint)); - priv->point.x = point.x; - priv->point.y = point.y; + priv->point = g_malloc0 (sizeof (GtkGisPoint)); + + priv->point->x = point.x; + priv->point->y = point.y; return gpoint; } @@ -106,8 +115,8 @@ GtkGisPoint gpoint = g_malloc0 (sizeof (GtkGisPoint)); - gpoint->x = priv->point.x; - gpoint->y = priv->point.y; + gpoint->x = priv->point->x; + gpoint->y = priv->point->y; return gpoint; } @@ -122,7 +131,7 @@ gtk_gis_geometry_point_get_x (GtkGisGeometryPoint *point) { GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (point); - return priv->point.x; + return priv->point->x; } /** @@ -135,7 +144,26 @@ gtk_gis_geometry_point_get_y (GtkGisGeometryPoint *point) { GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (point); - return priv->point.y; + return priv->point->y; +} + +static +GooCanvasItem *gtk_gis_geometry_point_draw (GtkGisGeometry *point) +{ + GooCanvasItem *item = NULL; + + GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (GTK_GIS_GEOMETRY_POINT (point)); + + if (priv->point != NULL) + { + item = goo_canvas_rect_new (NULL, + priv->point->x, priv->point->y, + 1, 1, + "line-width", 1.0, + NULL); + } + + return item; } static void diff --git a/src/geometrypolygon.c b/src/geometrypolygon.c index 01bc588..27af4ab 100644 --- a/src/geometrypolygon.c +++ b/src/geometrypolygon.c @@ -30,6 +30,8 @@ static void gtk_gis_geometry_polygon_class_init (GtkGisGeometryPolygonClass *klass); static void gtk_gis_geometry_polygon_init (GtkGisGeometryPolygon *gtk_gis_geometry_polygon); +static GooCanvasItem *gtk_gis_geometry_polygon_draw (GtkGisGeometry *polygon); + static void gtk_gis_geometry_polygon_set_property (GObject *object, guint property_id, const GValue *value, @@ -45,7 +47,7 @@ static void gtk_gis_geometry_polygon_get_property (GObject *object, typedef struct _GtkGisGeometryPolygonPrivate GtkGisGeometryPolygonPrivate; struct _GtkGisGeometryPolygonPrivate { - GList *lines; + GSList *lines; }; G_DEFINE_TYPE (GtkGisGeometryPolygon, gtk_gis_geometry_polygon, TYPE_GTK_GIS_GEOMETRY) @@ -54,11 +56,14 @@ static void gtk_gis_geometry_polygon_class_init (GtkGisGeometryPolygonClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkGisGeometryClass *geometry_class = GTK_GIS_GEOMETRY_CLASS (klass); g_type_class_add_private (object_class, sizeof (GtkGisGeometryPolygonPrivate)); object_class->set_property = gtk_gis_geometry_polygon_set_property; object_class->get_property = gtk_gis_geometry_polygon_get_property; + + geometry_class->draw = gtk_gis_geometry_polygon_draw; } static void @@ -105,7 +110,7 @@ GtkGisGeometry } /** - * gtk_gis_geometry_polygon_new_from_array: + * gtk_gis_geometry_polygon_new_from_list: * @points: * * Creates a new #GtkGisGeometryPolygon object. @@ -113,12 +118,12 @@ GtkGisGeometry * Returns: the newly created #GtkGisGeometryPolygon object. */ GtkGisGeometry -*gtk_gis_geometry_polygon_new_from_array (GPtrArray *points) +*gtk_gis_geometry_polygon_new_from_list (GSList *points) { GtkGisGeometry *polygon = NULL; GtkGisGeometry *line = NULL; - line = gtk_gis_geometry_line_new_from_array (points); + line = gtk_gis_geometry_line_new_from_list (points); if (line != NULL) { @@ -141,7 +146,7 @@ gtk_gis_geometry_polygon_add_line (GtkGisGeometryPolygon *polygon, { GtkGisGeometryPolygonPrivate *priv = GTK_GIS_GEOMETRY_POLYGON_GET_PRIVATE (polygon); - priv->lines = g_list_append (priv->lines, line); + priv->lines = g_slist_append (priv->lines, line); } /** @@ -156,7 +161,45 @@ gtk_gis_geometry_polygon_remove_line (GtkGisGeometryPolygon *polygon, { GtkGisGeometryPolygonPrivate *priv = GTK_GIS_GEOMETRY_POLYGON_GET_PRIVATE (polygon); - priv->lines = g_list_remove (priv->lines, line); + priv->lines = g_slist_remove (priv->lines, line); +} + +static GooCanvasItem +*gtk_gis_geometry_polygon_draw (GtkGisGeometry *polygon) +{ + GtkGisGeometryPolygonPrivate *priv; + GooCanvasItem *polygon_item = NULL; + + gchar *path; + + priv = GTK_GIS_GEOMETRY_POLYGON_GET_PRIVATE (GTK_GIS_GEOMETRY_POLYGON (polygon)); + + if (priv->lines != NULL) + { + GSList *cur; + GtkGisGeometryLine *line; + GooCanvasItem *item = NULL; + + polygon_item = goo_canvas_group_new (NULL, NULL); + + cur = priv->lines; + + line = (GtkGisGeometryLine *)cur->data; + + while (cur != NULL) + { + item = gtk_gis_geometry_draw (GTK_GIS_GEOMETRY (line)); + + if (item != NULL) + { + goo_canvas_item_add_child (polygon_item, item, -1); + } + + cur = g_slist_next (cur); + } + } + + return polygon_item; } static void diff --git a/src/geometrypolygon.h b/src/geometrypolygon.h index 0b6e008..641ac47 100644 --- a/src/geometrypolygon.h +++ b/src/geometrypolygon.h @@ -52,7 +52,7 @@ GType gtk_gis_geometry_polygon_get_type (void) G_GNUC_CONST; GtkGisGeometry *gtk_gis_geometry_polygon_new (void); GtkGisGeometry *gtk_gis_geometry_polygon_new_from_line (GtkGisGeometryLine *line); -GtkGisGeometry *gtk_gis_geometry_polygon_new_from_array (GPtrArray *points); +GtkGisGeometry *gtk_gis_geometry_polygon_new_from_list (GSList *points); void gtk_gis_geometry_polygon_add_line (GtkGisGeometryPolygon *polygon, GtkGisGeometryLine *line); void gtk_gis_geometry_polygon_remove_line (GtkGisGeometryPolygon *polygon, GtkGisGeometryLine *line); diff --git a/src/gtkgis.c b/src/gtkgis.c index 526cde8..8143477 100644 --- a/src/gtkgis.c +++ b/src/gtkgis.c @@ -38,6 +38,10 @@ static void gtk_gis_destroy (GtkObject *object); static void gtk_gis_add (GtkContainer *container, GtkWidget *widget); +static void gtk_gis_forall (GtkContainer *container, + gboolean include_internals, + GtkCallback callback, + gpointer callback_data); static void gtk_gis_set_property (GObject *object, guint property_id, @@ -64,10 +68,13 @@ struct _GtkGisPrivate GtkWidget *canvas; GooCanvasItem *canvas_root; - GList *layers_groups; - GList *layers; + GSList *layers_groups; + GSList *layers; - GtkGisLayerExtent extent; + GtkGisLayerExtent *extent; + + gdouble scale_x; + gdouble scale_y; }; G_DEFINE_TYPE (GtkGis, gtk_gis, GTK_TYPE_BIN) @@ -91,6 +98,7 @@ gtk_gis_class_init (GtkGisClass *klass) widget_class->size_allocate = gtk_gis_size_allocate; container_class->add = gtk_gis_add; + container_class->forall = gtk_gis_forall; } static void @@ -105,6 +113,14 @@ gtk_gis_init (GtkGis *gtk_gis) gtk_container_add (GTK_CONTAINER (priv->scroll_win), priv->canvas); priv->canvas_root = goo_canvas_get_root_item (GOO_CANVAS (priv->canvas)); + + priv->layers_groups = NULL; + priv->layers = NULL; + + priv->extent = NULL; + + priv->scale_x = 0.0; + priv->scale_y = 0.0; } /** @@ -241,6 +257,8 @@ GtkWidget g_warning ("The file is not a valid gtkgis file."); } + gtk_gis_draw (GTK_GIS (gtkgis)); + return gtkgis; } @@ -287,32 +305,43 @@ gtk_gis_add_layer (GtkGis *gtkgis, GtkGisLayer *layer) priv = GTK_GIS_GET_PRIVATE (gtkgis); - priv->layers = g_list_append (priv->layers, layer); + priv->layers = g_slist_append (priv->layers, layer); extent = gtk_gis_layer_get_extent (layer); - if (extent.min_x < priv->extent.min_x) - { - priv->extent.min_x = extent.min_x; - } - if (extent.min_y < priv->extent.min_y) - { - priv->extent.min_y = extent.min_y; - } - if (extent.max_x > priv->extent.max_x) + if (priv->extent == NULL) { - priv->extent.max_x = extent.max_x; + priv->extent = g_malloc (sizeof (GtkGisLayerExtent)); + priv->extent->min_x = extent.min_x; + priv->extent->max_x = extent.max_x; + priv->extent->min_y = extent.min_y; + priv->extent->max_y = extent.max_y; } - if (extent.max_y > priv->extent.max_y) + else { - priv->extent.max_y = extent.max_y; + if (extent.min_x < priv->extent->min_x) + { + priv->extent->min_x = extent.min_x; + } + if (extent.min_y < priv->extent->min_y) + { + priv->extent->min_y = extent.min_y; + } + if (extent.max_x > priv->extent->max_x) + { + priv->extent->max_x = extent.max_x; + } + if (extent.max_y > priv->extent->max_y) + { + priv->extent->max_y = extent.max_y; + } } goo_canvas_set_bounds (GOO_CANVAS (priv->canvas), - priv->extent.min_x, - priv->extent.min_y, - priv->extent.max_x, - priv->extent.max_y); + priv->extent->min_x, + priv->extent->min_y, + priv->extent->max_x, + priv->extent->max_y); } /** @@ -325,7 +354,73 @@ gtk_gis_remove_layer (GtkGis *gtkgis, GtkGisLayer *layer) { GtkGisPrivate *priv = GTK_GIS_GET_PRIVATE (gtkgis); - priv->layers = g_list_remove (priv->layers, layer); + priv->layers = g_slist_remove (priv->layers, layer); +} + +/** + * gtk_gis_draw: + * @gtkgis: + * + */ +void +gtk_gis_draw (GtkGis *gtkgis) +{ + GtkGisPrivate *priv; + GSList *cur; + + priv = GTK_GIS_GET_PRIVATE (gtkgis); + + cur = priv->layers; + while (cur != NULL) + { + gtk_gis_layer_draw ((GtkGisLayer *)cur->data, priv->canvas_root); + + cur = g_slist_next (cur); + } +} + +/** + * gtk_gis_zoom_to_max: + * @gtkgis: + * + */ +void +gtk_gis_zoom_to_max (GtkGis *gtkgis) +{ + GtkGisPrivate *priv; + GtkRequisition requisition; + + priv = GTK_GIS_GET_PRIVATE (gtkgis); + +g_fprintf(stderr,"%f %f %f %f\n", priv->extent->min_x, priv->extent->max_x, +priv->extent->min_y, priv->extent->max_y); + + gtk_gis_size_request (GTK_WIDGET (gtkgis), &requisition); +g_fprintf(stderr,"%d %d\n", requisition.width, requisition.height); + /*priv->scale_x = requisition.width / (priv->extent->max_x - priv->extent->min_x); + priv->scale_y = requisition.height / (priv->extent->max_y - priv->extent->min_y);*/ + priv->scale_x = 640 / (priv->extent->max_x - priv->extent->min_x); + priv->scale_y = 600 / (priv->extent->max_y - priv->extent->min_y); +g_fprintf(stderr,"%f %f\n", priv->scale_x, priv->scale_y); + g_object_set (priv->canvas, + "scale-x", priv->scale_x, + "scale-y", priv->scale_y, + NULL); +} + +/** + * gtk_gis_zoom_to_layer: + * @gtkgis: + * @layer: + * + */ +void +gtk_gis_zoom_to_layer (GtkGis *gtkgis, GtkGisLayer *layer) +{ + GtkGisPrivate *priv; + + priv = GTK_GIS_GET_PRIVATE (gtkgis); + } static void @@ -336,11 +431,11 @@ gtk_gis_destroy (GtkObject *object) /* TO DO */ /* free each layers_groups before */ - g_list_free (priv->layers_groups); + g_slist_free (priv->layers_groups); /* TO DO */ /* free each layers before */ - g_list_free (priv->layers); + g_slist_free (priv->layers); GTK_OBJECT_CLASS (gtk_gis_parent_class)->destroy (object); } @@ -361,6 +456,21 @@ gtk_gis_add (GtkContainer *container, gtk_widget_set_parent (child, GTK_WIDGET (bin)); } +static void +gtk_gis_forall (GtkContainer *container, + gboolean include_internals, + GtkCallback callback, + gpointer callback_data) +{ + g_return_if_fail (IS_GTK_GIS (container)); + g_return_if_fail (callback != NULL); + + GTK_CONTAINER_CLASS (gtk_gis_parent_class)->forall (container, + include_internals, + callback, + callback_data); +} + static void gtk_gis_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { @@ -450,7 +560,6 @@ gtk_gis_size_allocate (GtkWidget *widget, child_allocation.y = relative_allocation.y + allocation->y; child_allocation.width = relative_allocation.width; child_allocation.height = relative_allocation.height; - gtk_widget_size_allocate (bin->child, &child_allocation); } } diff --git a/src/gtkgis.h b/src/gtkgis.h index 9bb0e7b..3e19dc1 100644 --- a/src/gtkgis.h +++ b/src/gtkgis.h @@ -66,8 +66,12 @@ void gtk_gis_remove_group (GtkGis *gtkgis, GtkGisLayersGroup *group); void gtk_gis_add_layer (GtkGis *gtkgis, GtkGisLayer *layer); void gtk_gis_remove_layer (GtkGis *gtkgis, GtkGisLayer *layer); -void gtk_gis_set_legend_visible (GtkGis *gtkgis, gboolean visible); +void gtk_gis_draw (GtkGis *gtkgis); + +void gtk_gis_zoom_to_max (GtkGis *gtkgis); +void gtk_gis_zoom_to_layer (GtkGis *gtkgis, GtkGisLayer *layer); +void gtk_gis_set_legend_visible (GtkGis *gtkgis, gboolean visible); GtkWidget *gtk_gis_get_legend (GtkGis *gtkgis); diff --git a/src/layer.c b/src/layer.c index 66c88f8..53fbc4b 100644 --- a/src/layer.c +++ b/src/layer.c @@ -51,6 +51,7 @@ struct _GtkGisLayerPrivate GtkGisLayerSource *source; + GooCanvasItem *layer_item; GList *geometries; gboolean editable; @@ -266,17 +267,36 @@ gtk_gis_layer_set_editable (GtkGisLayer *layer, /** * gtk_gis_layer_draw: * @layer: - * @canvas_root: + * @root: * */ void -gtk_gis_layer_draw (GtkGisLayer *layer, - GooCanvasItem *canvas_root) +gtk_gis_layer_draw (GtkGisLayer *layer, GooCanvasItem *root) { - /*if (IS_GTK_GIS_LAYER_OBJECT (layer) && GTK_GIS_LAYER_GET_CLASS (layer)->draw != NULL) + GtkGisLayerPrivate *priv; + + GList *cur; + + GooCanvasItem *item; + + priv = GTK_GIS_LAYER_GET_PRIVATE (layer); + + g_return_if_fail (priv->geometries != NULL); + + priv->layer_item = goo_canvas_group_new (root, NULL); + + cur = g_list_first (priv->geometries); + while (cur != NULL) { - GTK_GIS_LAYER_GET_CLASS (layer)->draw (layer, canvas_root); - }*/ + item = gtk_gis_geometry_draw ((GtkGisGeometry *)cur->data); + + if (item != NULL) + { + goo_canvas_item_add_child (priv->layer_item, item, -1); + } + + cur = g_list_next (cur); + } } static void diff --git a/src/layer.h b/src/layer.h index a89b7c4..4adfb8a 100644 --- a/src/layer.h +++ b/src/layer.h @@ -82,8 +82,7 @@ gboolean gtk_gis_layer_get_editable (GtkGisLayer *layer); void gtk_gis_layer_set_editable (GtkGisLayer *layer, gboolean editable); -void gtk_gis_layer_draw (GtkGisLayer *layer, - GooCanvasItem *canvas_root); +void gtk_gis_layer_draw (GtkGisLayer *layer, GooCanvasItem *root); G_END_DECLS diff --git a/src/layersource.c b/src/layersource.c index b40d53c..7ffdfc0 100644 --- a/src/layersource.c +++ b/src/layersource.c @@ -101,103 +101,96 @@ GList { GList *geometries = NULL; + OGRLayerH layer; + + OGRFeatureH feature; + OGRGeometryH poGeometry; + GtkGisGeometry *geometry; + GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (source); - if (priv->datasource == NULL) - { - /* TO DO */ - } - else + g_return_if_fail (priv->datasource != NULL); + + layer = OGR_DS_GetLayerByName (priv->datasource, name); + + g_return_if_fail (layer != NULL); + + OGR_L_ResetReading (layer); + + while ((feature = OGR_L_GetNextFeature (layer)) != NULL) { - OGRLayerH layer; - OGRFeatureDefnH layerDefn; - OGRFeatureH feature; - OGRGeometryH poGeometry; - GtkGisGeometry *geometry; + poGeometry = OGR_F_GetGeometryRef (feature); - layer = OGR_DS_GetLayerByName (priv->datasource, name); + if (poGeometry != NULL + && wkbFlatten (OGR_G_GetGeometryType (poGeometry)) == wkbPoint) + { + GtkGisPoint gpoint; - layerDefn = OGR_L_GetLayerDefn (layer); + gpoint.x = OGR_G_GetX (poGeometry, 0); + gpoint.y = OGR_G_GetY (poGeometry, 0); - while ((feature = OGR_L_GetNextFeature (layer)) != NULL) + geometry = gtk_gis_geometry_point_new (gpoint); + } + else if (poGeometry != NULL + && wkbFlatten (OGR_G_GetGeometryType (poGeometry)) == wkbLineString) { - poGeometry = OGR_F_GetGeometryRef (feature); + gint points; + gint point; + GSList *gpoints = NULL; - if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType (poGeometry)) == wkbPoint) + GtkGisPoint *gpoint; + + points = OGR_G_GetPointCount (poGeometry); + + for (point = 0; point < points; point++) { - GtkGisPoint gpoint; + gpoint = g_malloc0 (sizeof (GtkGisPoint)); - gpoint.x = OGR_G_GetX (poGeometry, 0); - gpoint.y = OGR_G_GetY (poGeometry, 0); + gpoint->x = OGR_G_GetX (poGeometry, point); + gpoint->y = OGR_G_GetY (poGeometry, point); - geometry = gtk_gis_geometry_point_new (gpoint); + gpoints = g_slist_append (gpoints, gpoint); } - else if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType (poGeometry)) == wkbLineString) + + geometry = gtk_gis_geometry_line_new_from_list (gpoints); + } + else if (poGeometry != NULL + && wkbFlatten (OGR_G_GetGeometryType (poGeometry)) == wkbPolygon) + { + gint num_geometries; + gint i_geometry; + + OGRGeometryH poGeometryIns; + + num_geometries = OGR_G_GetGeometryCount (poGeometry); + + for (i_geometry = 0; i_geometry < num_geometries; i_geometry++) { gint points; gint point; - GPtrArray *gar_points; + GSList *gpoints = NULL; GtkGisPoint *gpoint; - gar_points = g_ptr_array_new (); + poGeometryIns = OGR_G_GetGeometryRef (poGeometry, i_geometry); - points = OGR_G_GetPointCount (poGeometry); + points = OGR_G_GetPointCount (poGeometryIns); for (point = 0; point < points; point++) { gpoint = g_malloc0 (sizeof (GtkGisPoint)); - gpoint->x = OGR_G_GetX (poGeometry, point); - gpoint->y = OGR_G_GetY (poGeometry, point); + gpoint->x = OGR_G_GetX (poGeometryIns, point); + gpoint->y = OGR_G_GetY (poGeometryIns, point); - g_ptr_array_add (gar_points, gpoint); + gpoints = g_slist_append (gpoints, gpoint); } - geometry = gtk_gis_geometry_line_new_from_array (gar_points); + geometry = gtk_gis_geometry_polygon_new_from_list (gpoints); } - else if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType (poGeometry)) == wkbPolygon) - { - gint num_geometries; - gint i_geometry; - - OGRGeometryH poGeometryIns; - - num_geometries = OGR_G_GetGeometryCount (poGeometry); - - for (i_geometry = 0; i_geometry < num_geometries; i_geometry++) - { - gint points; - gint point; - GPtrArray *gar_points; - - GtkGisPoint *gpoint; - - poGeometryIns = OGR_G_GetGeometryRef (poGeometry, i_geometry); - - gar_points = g_ptr_array_new (); - - points = OGR_G_GetPointCount (poGeometry); - - for (point = 0; point < points; point++) - { - gpoint = g_malloc0 (sizeof (GtkGisPoint)); - - gpoint->x = OGR_G_GetX (poGeometry, point); - gpoint->y = OGR_G_GetY (poGeometry, point); - - g_ptr_array_add (gar_points, gpoint); - } - - geometry = gtk_gis_geometry_polygon_new_from_array (gar_points); - } - } - - geometries = g_list_append (geometries, geometry); } + + geometries = g_list_append (geometries, geometry); } return geometries; @@ -231,17 +224,17 @@ gtk_gis_layer_source_get_layer_geometry_type (GtkGisLayerSource *source, poGeometry = OGR_F_GetGeometryRef (feature); if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType(poGeometry)) == wkbPoint) + && wkbFlatten (OGR_G_GetGeometryType(poGeometry)) == wkbPoint) { type = LAYER_GEOMETRY_TYPE_POINT; } else if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType(poGeometry)) == wkbLineString) + && wkbFlatten (OGR_G_GetGeometryType(poGeometry)) == wkbLineString) { type = LAYER_GEOMETRY_TYPE_LINE; } else if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType(poGeometry)) == wkbPolygon) + && wkbFlatten (OGR_G_GetGeometryType(poGeometry)) == wkbPolygon) { type = LAYER_GEOMETRY_TYPE_POLYGON; } diff --git a/tests/from_xml.c b/tests/from_xml.c index afa64c7..76e5091 100644 --- a/tests/from_xml.c +++ b/tests/from_xml.c @@ -20,6 +20,8 @@ main (int argc, char *argv[]) gtk_widget_show_all (window); + gtk_gis_zoom_to_max (GTK_GIS (gtkgis)); + gtk_main (); return 0; diff --git a/tests/test1.gtkgis b/tests/test1.gtkgis index eb25c76..f1bd6d9 100644 --- a/tests/test1.gtkgis +++ b/tests/test1.gtkgis @@ -1,16 +1,16 @@ - + @@ -18,7 +18,7 @@ - +