From 7f02fa4703e178c5cd43efbe9e7933151d031f51 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 28 Jun 2009 17:06:48 +0200 Subject: [PATCH] Continued base classes. Implemented geometry classes. Moved LayerSourceShp::get_geometry_type and LayerSourceShp::get_geometries funtions to LayerSource. --- .gitignore | 1 + src/geometry.h | 7 ++ src/geometryline.c | 76 +++++--------- src/geometryline.h | 10 +- src/geometrypoint.c | 71 +++++++++---- src/geometrypoint.h | 8 +- src/geometrypolygon.c | 106 ++++++++++++------- src/geometrypolygon.h | 13 ++- src/geometryraster.c | 18 ++-- src/geometryraster.h | 2 +- src/gtkgis.c | 199 ++++++++++++++++++++++------------- src/gtkgis.h | 12 +-- src/layer.c | 21 +++- src/layer.h | 2 + src/layersgroup.c | 8 +- src/layersource.c | 221 ++++++++++++++++++++++++++++++++------- src/layersource.h | 34 ++++-- src/layersourcepostgis.c | 20 ++-- src/layersourcepostgis.h | 8 +- src/layersourceshp.c | 170 ++++-------------------------- src/layersourceshp.h | 7 +- tests/Makefile.am | 8 +- tests/from_xml.c | 19 ++++ tests/test1.gtkgis | 12 +-- 24 files changed, 613 insertions(+), 440 deletions(-) diff --git a/.gitignore b/.gitignore index 095b9a4..0cdb086 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ src/.deps/ src/.libs/ stamp-h1 tests/.deps +tests/.libs tests/from_xml # temporarily diff --git a/src/geometry.h b/src/geometry.h index f0c7567..ec59947 100644 --- a/src/geometry.h +++ b/src/geometry.h @@ -50,6 +50,13 @@ struct _GtkGisGeometryClass GType gtk_gis_geometry_get_type (void) G_GNUC_CONST; +typedef struct + { + gdouble x; + gdouble y; + } GtkGisPoint; + + gboolean gtk_gis_geometry_get_editable (GtkGisGeometry *geometry); void gtk_gis_geometry_set_editable (GtkGisGeometry *geometry, gboolean editable); diff --git a/src/geometryline.c b/src/geometryline.c index 3cc42a2..11fdeba 100644 --- a/src/geometryline.c +++ b/src/geometryline.c @@ -48,33 +48,7 @@ struct _GtkGisGeometryLinePrivate GPtrArray *points; }; -GType -gtk_gis_geometry_line_get_type (void) -{ - static GType gtk_gis_geometry_line_type = 0; - - if (!gtk_gis_geometry_line_type) - { - static const GTypeInfo gtk_gis_geometry_line_info = - { - sizeof (GtkGisGeometryLineClass), - NULL, /* base_init */ - NULL, /* base_finalize */ - (GClassInitFunc) gtk_gis_geometry_line_class_init, - NULL, /* class_finalize */ - NULL, /* class_data */ - sizeof (GtkGisGeometryLine), - 0, /* n_preallocs */ - (GInstanceInitFunc) gtk_gis_geometry_line_init, - NULL - }; - - gtk_gis_geometry_line_type = g_type_register_static (G_TYPE_OBJECT, "GtkGisGeometryLine", - >k_gis_geometry_line_info, 0); - } - - return gtk_gis_geometry_line_type; -} +G_DEFINE_TYPE (GtkGisGeometryLine, gtk_gis_geometry_line, TYPE_GTK_GIS_GEOMETRY) static void gtk_gis_geometry_line_class_init (GtkGisGeometryLineClass *klass) @@ -102,12 +76,12 @@ gtk_gis_geometry_line_init (GtkGisGeometryLine *gtk_gis_geometry_line) * * Returns: the newly created #GtkGisGeometryLine object. */ -GtkGisGeometryLine +GtkGisGeometry *gtk_gis_geometry_line_new () { - GtkGisGeometryLine *gtk_gis_geometry_line; + GtkGisGeometry *gtk_gis_geometry_line; - gtk_gis_geometry_line = GTK_GIS_GEOMETRY_LINE (g_object_new (gtk_gis_geometry_line_get_type (), NULL)); + gtk_gis_geometry_line = g_object_new (gtk_gis_geometry_line_get_type (), NULL); return gtk_gis_geometry_line; } @@ -120,32 +94,39 @@ GtkGisGeometryLine * * Returns: the newly created #GtkGisGeometryLine object. */ -GtkGisGeometryLine +GtkGisGeometry *gtk_gis_geometry_line_new_from_array (GPtrArray *points) { - GtkGisGeometryLine *gtk_gis_geometry_line; + GtkGisGeometry *line; GtkGisGeometryLinePrivate *priv; - gtk_gis_geometry_line = gtk_gis_geometry_line_new (); + line = gtk_gis_geometry_line_new (); - priv = GTK_GIS_GEOMETRY_LINE_GET_PRIVATE (gtk_gis_geometry_line); + priv = GTK_GIS_GEOMETRY_LINE_GET_PRIVATE (GTK_GIS_GEOMETRY_LINE (line)); priv->points = points; - return gtk_gis_geometry_line; + return line; } /** - * gtk_gis_geomtry_line_add_point: + * gtk_gis_geometry_line_add_point: * @line: * @point: */ void -gtk_gis_geomtry_line_add_point (GtkGisGeometryLine *line, GtkGisGeometryPoint *point) +gtk_gis_geometry_line_add_point (GtkGisGeometryLine *line, GtkGisPoint point) { GtkGisGeometryLinePrivate *priv = GTK_GIS_GEOMETRY_LINE_GET_PRIVATE (line); - g_ptr_array_add (priv->points, point); + GtkGisPoint *gpoint; + + gpoint = g_malloc0 (sizeof (GtkGisPoint)); + + gpoint->x = point.x; + gpoint->y = point.y; + + g_ptr_array_add (priv->points, gpoint); } /** @@ -156,22 +137,23 @@ gtk_gis_geomtry_line_add_point (GtkGisGeometryLine *line, GtkGisGeometryPoint *p * */ void -gtk_gis_geomtry_line_add_point_from_coordinates (GtkGisGeometryLine *line, - gdouble x, gdouble y) +gtk_gis_geometry_line_add_point_from_coordinates (GtkGisGeometryLine *line, + gdouble x, gdouble y) { - GtkGisGeometryPoint *point; + GtkGisPoint gpoint; - point = gtk_gis_geometry_point_new (x, y); + gpoint.x = x; + gpoint.y = y; - gtk_gis_geometry_line_add_point (line, point); + gtk_gis_geometry_line_add_point (line, gpoint); } static void gtk_gis_geometry_line_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { - GtkGisGeometryLine *gtk_gis_geometry_line = GTK_GIS_GEOMETRY_LINE (object); + GtkGisGeometryLine *line = GTK_GIS_GEOMETRY_LINE (object); - GtkGisGeometryLinePrivate *priv = GTK_GIS_GEOMETRY_LINE_GET_PRIVATE (gtk_gis_geometry_line); + GtkGisGeometryLinePrivate *priv = GTK_GIS_GEOMETRY_LINE_GET_PRIVATE (line); switch (property_id) { @@ -184,9 +166,9 @@ gtk_gis_geometry_line_set_property (GObject *object, guint property_id, const GV static void gtk_gis_geometry_line_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { - GtkGisGeometryLine *gtk_gis_geometry_line = GTK_GIS_GEOMETRY_LINE (object); + GtkGisGeometryLine *line = GTK_GIS_GEOMETRY_LINE (object); - GtkGisGeometryLinePrivate *priv = GTK_GIS_GEOMETRY_LINE_GET_PRIVATE (gtk_gis_geometry_line); + GtkGisGeometryLinePrivate *priv = GTK_GIS_GEOMETRY_LINE_GET_PRIVATE (line); switch (property_id) { diff --git a/src/geometryline.h b/src/geometryline.h index 63a1f10..97d3d52 100644 --- a/src/geometryline.h +++ b/src/geometryline.h @@ -50,13 +50,13 @@ struct _GtkGisGeometryLineClass GType gtk_gis_geometry_line_get_type (void) G_GNUC_CONST; -GtkGisGeometryLine *gtk_gis_geometry_line_new (void); +GtkGisGeometry *gtk_gis_geometry_line_new (void); -GtkGisGeometryLine *gtk_gis_geometry_line_new_from_array (GPtrArray *points); +GtkGisGeometry *gtk_gis_geometry_line_new_from_array (GPtrArray *points); -void gtk_gis_geomtry_line_add_point (GtkGisGeometryLine *line, GtkGisGeometryPoint *point); -void gtk_gis_geomtry_line_add_point_from_coordinates (GtkGisGeometryLine *line, - gdouble x, gdouble y); +void gtk_gis_geometry_line_add_point (GtkGisGeometryLine *line, GtkGisPoint point); +void gtk_gis_geometry_line_add_point_from_coordinates (GtkGisGeometryLine *line, + gdouble x, gdouble y); diff --git a/src/geometrypoint.c b/src/geometrypoint.c index f9b9611..f352267 100644 --- a/src/geometrypoint.c +++ b/src/geometrypoint.c @@ -45,8 +45,7 @@ static void gtk_gis_geometry_point_get_property (GObject *object, typedef struct _GtkGisGeometryPointPrivate GtkGisGeometryPointPrivate; struct _GtkGisGeometryPointPrivate { - gdouble x; - gdouble y; + GtkGisPoint point; }; GType @@ -70,7 +69,7 @@ gtk_gis_geometry_point_get_type (void) NULL }; - gtk_gis_geometry_point_type = g_type_register_static (G_TYPE_OBJECT, "GtkGisGeometryPoint", + gtk_gis_geometry_point_type = g_type_register_static (TYPE_GTK_GIS_GEOMETRY, "GtkGisGeometryPoint", >k_gis_geometry_point_info, 0); } @@ -96,51 +95,81 @@ gtk_gis_geometry_point_init (GtkGisGeometryPoint *gtk_gis_geometry_point) /** * gtk_gis_geometry_point_new: - * @x: - * @y: + * @point: * * Creates a new #GtkGisGeometryPoint object. * * Returns: the newly created #GtkGisGeometryPoint object. */ -GtkGisGeometryPoint -*gtk_gis_geometry_point_new (gdouble x, gdouble y) +GtkGisGeometry +*gtk_gis_geometry_point_new (GtkGisPoint point) { - GtkGisGeometryPoint *gtk_gis_geometry_point; + GtkGisGeometry *gpoint; GtkGisGeometryPointPrivate *priv; - gtk_gis_geometry_point = GTK_GIS_GEOMETRY_POINT (g_object_new (gtk_gis_geometry_point_get_type (), NULL)); + gpoint = g_object_new (gtk_gis_geometry_point_get_type (), NULL); - priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (gtk_gis_geometry_point); + priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (GTK_GIS_GEOMETRY_POINT (gpoint)); - priv->x = x; - priv->y = y; + priv->point.x = point.x; + priv->point.y = point.y; - return gtk_gis_geometry_point; + return gpoint; } +/** + * gtk_gis_geometry_get_point: + * @point: + * + * Returns: + */ +GtkGisPoint +*gtk_gis_geometry_get_point (GtkGisGeometryPoint *point) +{ + GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (point); + + GtkGisPoint *gpoint; + + gpoint = g_malloc0 (sizeof (GtkGisPoint)); + + gpoint->x = priv->point.x; + gpoint->y = priv->point.y; + + return gpoint; +} + +/** + * gtk_gis_geometry_point_get_x: + * @point: + * + */ gdouble -gtk_gis_geometry_get_x (GtkGisGeometryPoint *point) +gtk_gis_geometry_point_get_x (GtkGisGeometryPoint *point) { GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (point); - return priv->x; + return priv->point.x; } +/** + * gtk_gis_geometry_point_get_y: + * @point: + * + */ gdouble -gtk_gis_geometry_get_y (GtkGisGeometryPoint *point) +gtk_gis_geometry_point_get_y (GtkGisGeometryPoint *point) { GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (point); - return priv->y; + return priv->point.y; } static void gtk_gis_geometry_point_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { - GtkGisGeometryPoint *gtk_gis_geometry_point = GTK_GIS_GEOMETRY_POINT (object); + GtkGisGeometryPoint *point = GTK_GIS_GEOMETRY_POINT (object); - GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (gtk_gis_geometry_point); + GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (point); switch (property_id) { @@ -153,9 +182,9 @@ gtk_gis_geometry_point_set_property (GObject *object, guint property_id, const G static void gtk_gis_geometry_point_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { - GtkGisGeometryPoint *gtk_gis_geometry_point = GTK_GIS_GEOMETRY_POINT (object); + GtkGisGeometryPoint *point = GTK_GIS_GEOMETRY_POINT (object); - GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (gtk_gis_geometry_point); + GtkGisGeometryPointPrivate *priv = GTK_GIS_GEOMETRY_POINT_GET_PRIVATE (point); switch (property_id) { diff --git a/src/geometrypoint.h b/src/geometrypoint.h index 12e899b..63add2a 100644 --- a/src/geometrypoint.h +++ b/src/geometrypoint.h @@ -49,10 +49,12 @@ struct _GtkGisGeometryPointClass GType gtk_gis_geometry_point_get_type (void) G_GNUC_CONST; -GtkGisGeometryPoint *gtk_gis_geometry_point_new (gdouble x, gdouble y); +GtkGisGeometry *gtk_gis_geometry_point_new (GtkGisPoint point); -gdouble gtk_gis_geometry_get_x (GtkGisGeometryPoint *point); -gdouble gtk_gis_geometry_get_y (GtkGisGeometryPoint *point); +GtkGisPoint *gtk_gis_geometry_get_point (GtkGisGeometryPoint *point); + +gdouble gtk_gis_geometry_point_get_x (GtkGisGeometryPoint *point); +gdouble gtk_gis_geometry_point_get_y (GtkGisGeometryPoint *point); G_END_DECLS diff --git a/src/geometrypolygon.c b/src/geometrypolygon.c index 4dad0b2..6d79aa8 100644 --- a/src/geometrypolygon.c +++ b/src/geometrypolygon.c @@ -45,35 +45,10 @@ static void gtk_gis_geometry_polygon_get_property (GObject *object, typedef struct _GtkGisGeometryPolygonPrivate GtkGisGeometryPolygonPrivate; struct _GtkGisGeometryPolygonPrivate { + GList *lines; }; -GType -gtk_gis_geometry_polygon_get_type (void) -{ - static GType gtk_gis_geometry_polygon_type = 0; - - if (!gtk_gis_geometry_polygon_type) - { - static const GTypeInfo gtk_gis_geometry_polygon_info = - { - sizeof (GtkGisGeometryPolygonClass), - NULL, /* base_init */ - NULL, /* base_finalize */ - (GClassInitFunc) gtk_gis_geometry_polygon_class_init, - NULL, /* class_finalize */ - NULL, /* class_data */ - sizeof (GtkGisGeometryPolygon), - 0, /* n_preallocs */ - (GInstanceInitFunc) gtk_gis_geometry_polygon_init, - NULL - }; - - gtk_gis_geometry_polygon_type = g_type_register_static (G_TYPE_OBJECT, "GtkGisGeometryPolygon", - >k_gis_geometry_polygon_info, 0); - } - - return gtk_gis_geometry_polygon_type; -} +G_DEFINE_TYPE (GtkGisGeometryPolygon, gtk_gis_geometry_polygon, TYPE_GTK_GIS_GEOMETRY) static void gtk_gis_geometry_polygon_class_init (GtkGisGeometryPolygonClass *klass) @@ -99,14 +74,34 @@ gtk_gis_geometry_polygon_init (GtkGisGeometryPolygon *gtk_gis_geometry_polygon) * * Returns: the newly created #GtkGisGeometryPolygon object. */ -GtkGisGeometryPolygon +GtkGisGeometry *gtk_gis_geometry_polygon_new () { - GtkGisGeometryPolygon *gtk_gis_geometry_polygon; + GtkGisGeometry *polygon; + + polygon = g_object_new (gtk_gis_geometry_polygon_get_type (), NULL); + + return polygon; +} + +/** + * gtk_gis_geometry_polygon_new_from_line: + * @line: a #GtkGisGeometryLine object + * + * Creates a new #GtkGisGeometryPolygon object. + * + * Returns: the newly created #GtkGisGeometryPolygon object. + */ +GtkGisGeometry +*gtk_gis_geometry_polygon_new_from_line (GtkGisGeometryLine *line) +{ + GtkGisGeometry *polygon; + + polygon = gtk_gis_geometry_polygon_new (); - gtk_gis_geometry_polygon = GTK_GIS_GEOMETRY_POLYGON (g_object_new (gtk_gis_geometry_polygon_get_type (), NULL)); + gtk_gis_geometry_polygon_add_line (GTK_GIS_GEOMETRY_POLYGON (polygon), line); - return gtk_gis_geometry_polygon; + return polygon; } /** @@ -117,22 +112,55 @@ GtkGisGeometryPolygon * * Returns: the newly created #GtkGisGeometryPolygon object. */ -GtkGisGeometryPolygon +GtkGisGeometry *gtk_gis_geometry_polygon_new_from_array (GPtrArray *points) { - GtkGisGeometryPolygon *gtk_gis_geometry_polygon; + GtkGisGeometry *polygon; + GtkGisGeometry *line; - gtk_gis_geometry_polygon = gtk_gis_geometry_line_new_from_array (points); + line = gtk_gis_geometry_line_new_from_array (points); - return gtk_gis_geometry_polygon; + gtk_gis_geometry_polygon_add_line (GTK_GIS_GEOMETRY_POLYGON (polygon), GTK_GIS_GEOMETRY_LINE (line)); + + return polygon; +} + +/** + * gtk_gis_geometry_polygon_add_line: + * @polygon: + * @line: + * + */ +void +gtk_gis_geometry_polygon_add_line (GtkGisGeometryPolygon *polygon, + GtkGisGeometryLine *line) +{ + GtkGisGeometryPolygonPrivate *priv = GTK_GIS_GEOMETRY_POLYGON_GET_PRIVATE (polygon); + + priv->lines = g_list_append (priv->lines, line); +} + +/** + * gtk_gis_geometry_polygon_remove_line: + * @polygon: + * @line: + * + */ +void +gtk_gis_geometry_polygon_remove_line (GtkGisGeometryPolygon *polygon, + GtkGisGeometryLine *line) +{ + GtkGisGeometryPolygonPrivate *priv = GTK_GIS_GEOMETRY_POLYGON_GET_PRIVATE (polygon); + + priv->lines = g_list_remove (priv->lines, line); } static void gtk_gis_geometry_polygon_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { - GtkGisGeometryPolygon *gtk_gis_geometry_polygon = GTK_GIS_GEOMETRY_POLYGON (object); + GtkGisGeometryPolygon *polygon = GTK_GIS_GEOMETRY_POLYGON (object); - GtkGisGeometryPolygonPrivate *priv = GTK_GIS_GEOMETRY_POLYGON_GET_PRIVATE (gtk_gis_geometry_polygon); + GtkGisGeometryPolygonPrivate *priv = GTK_GIS_GEOMETRY_POLYGON_GET_PRIVATE (polygon); switch (property_id) { @@ -145,9 +173,9 @@ gtk_gis_geometry_polygon_set_property (GObject *object, guint property_id, const static void gtk_gis_geometry_polygon_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { - GtkGisGeometryPolygon *gtk_gis_geometry_polygon = GTK_GIS_GEOMETRY_POLYGON (object); + GtkGisGeometryPolygon *polygon = GTK_GIS_GEOMETRY_POLYGON (object); - GtkGisGeometryPolygonPrivate *priv = GTK_GIS_GEOMETRY_POLYGON_GET_PRIVATE (gtk_gis_geometry_polygon); + GtkGisGeometryPolygonPrivate *priv = GTK_GIS_GEOMETRY_POLYGON_GET_PRIVATE (polygon); switch (property_id) { diff --git a/src/geometrypolygon.h b/src/geometrypolygon.h index 2309170..a1ecbd8 100644 --- a/src/geometrypolygon.h +++ b/src/geometrypolygon.h @@ -20,6 +20,7 @@ #ifndef __GTK_GIS_GEOMETRY_POLYGON_H__ #define __GTK_GIS_GEOMETRY_POLYGON_H__ +#include "geometry.h" #include "geometryline.h" G_BEGIN_DECLS @@ -38,20 +39,24 @@ typedef struct _GtkGisGeometryPolygonClass GtkGisGeometryPolygonClass; struct _GtkGisGeometryPolygon { - GtkGisGeometryLine parent; + GtkGisGeometry parent; }; struct _GtkGisGeometryPolygonClass { - GtkGisGeometryLineClass parent_class; + GtkGisGeometryClass parent_class; }; GType gtk_gis_geometry_polygon_get_type (void) G_GNUC_CONST; -GtkGisGeometryPolygon *gtk_gis_geometry_polygon_new (void); +GtkGisGeometry *gtk_gis_geometry_polygon_new (void); -GtkGisGeometryPolygon *gtk_gis_geometry_polygon_new_from_array (GPtrArray *points); +GtkGisGeometry *gtk_gis_geometry_polygon_new_from_line (GtkGisGeometryLine *line); +GtkGisGeometry *gtk_gis_geometry_polygon_new_from_array (GPtrArray *points); + +void gtk_gis_geometry_polygon_add_line (GtkGisGeometryPolygon *polygon, GtkGisGeometryLine *line); +void gtk_gis_geometry_polygon_remove_line (GtkGisGeometryPolygon *polygon, GtkGisGeometryLine *line); G_END_DECLS diff --git a/src/geometryraster.c b/src/geometryraster.c index 4fe3800..9979a0c 100644 --- a/src/geometryraster.c +++ b/src/geometryraster.c @@ -68,7 +68,7 @@ gtk_gis_geometry_raster_get_type (void) NULL }; - gtk_gis_geometry_raster_type = g_type_register_static (G_TYPE_OBJECT, "GtkGisGeometryRaster", + gtk_gis_geometry_raster_type = g_type_register_static (TYPE_GTK_GIS_GEOMETRY, "GtkGisGeometryRaster", >k_gis_geometry_raster_info, 0); } @@ -99,22 +99,22 @@ gtk_gis_geometry_raster_init (GtkGisGeometryRaster *gtk_gis_geometry_raster) * * Returns: the newly created #GtkGisGeometryRaster object. */ -GtkGisGeometryRaster +GtkGisGeometry *gtk_gis_geometry_raster_new () { - GtkGisGeometryRaster *gtk_gis_geometry_raster; + GtkGisGeometry *raster; - gtk_gis_geometry_raster = GTK_GIS_GEOMETRY_RASTER (g_object_new (gtk_gis_geometry_raster_get_type (), NULL)); + raster = g_object_new (gtk_gis_geometry_raster_get_type (), NULL); - return gtk_gis_geometry_raster; + return raster; } static void gtk_gis_geometry_raster_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { - GtkGisGeometryRaster *gtk_gis_geometry_raster = GTK_GIS_GEOMETRY_RASTER (object); + GtkGisGeometryRaster *raster = GTK_GIS_GEOMETRY_RASTER (object); - GtkGisGeometryRasterPrivate *priv = GTK_GIS_GEOMETRY_RASTER_GET_PRIVATE (gtk_gis_geometry_raster); + GtkGisGeometryRasterPrivate *priv = GTK_GIS_GEOMETRY_RASTER_GET_PRIVATE (raster); switch (property_id) { @@ -127,9 +127,9 @@ gtk_gis_geometry_raster_set_property (GObject *object, guint property_id, const static void gtk_gis_geometry_raster_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { - GtkGisGeometryRaster *gtk_gis_geometry_raster = GTK_GIS_GEOMETRY_RASTER (object); + GtkGisGeometryRaster *raster = GTK_GIS_GEOMETRY_RASTER (object); - GtkGisGeometryRasterPrivate *priv = GTK_GIS_GEOMETRY_RASTER_GET_PRIVATE (gtk_gis_geometry_raster); + GtkGisGeometryRasterPrivate *priv = GTK_GIS_GEOMETRY_RASTER_GET_PRIVATE (raster); switch (property_id) { diff --git a/src/geometryraster.h b/src/geometryraster.h index e8ac4e9..38a9683 100644 --- a/src/geometryraster.h +++ b/src/geometryraster.h @@ -49,7 +49,7 @@ struct _GtkGisGeometryRasterClass GType gtk_gis_geometry_raster_get_type (void) G_GNUC_CONST; -GtkGisGeometryRaster *gtk_gis_geometry_raster_new (void); +GtkGisGeometry *gtk_gis_geometry_raster_new (void); G_END_DECLS diff --git a/src/gtkgis.c b/src/gtkgis.c index cc0f842..0740a39 100644 --- a/src/gtkgis.c +++ b/src/gtkgis.c @@ -26,10 +26,17 @@ #endif #include "gtkgis.h" +#include "layer.h" +#include "layersource.h" +#include "layersourceshp.h" +#include "layersourcepostgis.h" static void gtk_gis_class_init (GtkGisClass *klass); static void gtk_gis_init (GtkGis *gtk_gis); +static void gtk_gis_add (GtkContainer *container, + GtkWidget *widget); + static void gtk_gis_set_property (GObject *object, guint property_id, const GValue *value, @@ -45,56 +52,46 @@ static void gtk_gis_get_property (GObject *object, typedef struct _GtkGisPrivate GtkGisPrivate; struct _GtkGisPrivate { + GtkWidget *scroll_win; + GtkWidget *canvas; GooCanvasItem *canvas_root; GList *layer_groups; GList *layers; - }; -GType -gtk_gis_get_type (void) -{ - static GType gtk_gis_type = 0; - - if (!gtk_gis_type) - { - static const GTypeInfo gtk_gis_info = - { - sizeof (GtkGisClass), - NULL, /* base_init */ - NULL, /* base_finalize */ - (GClassInitFunc) gtk_gis_class_init, - NULL, /* class_finalize */ - NULL, /* class_data */ - sizeof (GtkGis), - 0, /* n_preallocs */ - (GInstanceInitFunc) gtk_gis_init, - NULL - }; - - gtk_gis_type = g_type_register_static (G_TYPE_OBJECT, "GtkGis", - >k_gis_info, 0); - } + GtkGisLayerExtent extent; + }; - return gtk_gis_type; -} +G_DEFINE_TYPE (GtkGis, gtk_gis, GTK_TYPE_BIN) static void gtk_gis_class_init (GtkGisClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = (GtkWidgetClass*) klass; + GtkContainerClass *container_class = (GtkContainerClass*) klass; g_type_class_add_private (object_class, sizeof (GtkGisPrivate)); object_class->set_property = gtk_gis_set_property; object_class->get_property = gtk_gis_get_property; + + container_class->add = gtk_gis_add; } static void gtk_gis_init (GtkGis *gtk_gis) { GtkGisPrivate *priv = GTK_GIS_GET_PRIVATE (gtk_gis); + + priv->scroll_win = gtk_scrolled_window_new (NULL, NULL); + gtk_container_add (GTK_CONTAINER (gtk_gis), priv->scroll_win); + + priv->canvas = goo_canvas_new (); + gtk_container_add (GTK_CONTAINER (priv->scroll_win), priv->canvas); + + priv->canvas_root = goo_canvas_get_root_item (GOO_CANVAS (priv->canvas)); } /** @@ -104,12 +101,15 @@ gtk_gis_init (GtkGis *gtk_gis) * * Returns: the newly created #GtkGis object. */ -GtkGis +GtkWidget *gtk_gis_new () { - GtkGis *gtk_gis; + GtkWidget *gtk_gis; + GtkGisPrivate *priv; - gtk_gis = GTK_GIS (g_object_new (gtk_gis_get_type (), NULL)); + gtk_gis = g_object_new (gtk_gis_get_type (), NULL); + + priv = GTK_GIS_GET_PRIVATE (GTK_GIS (gtk_gis)); return gtk_gis; } @@ -120,10 +120,10 @@ GtkGis * * Returns: the newly created #GtkGis object. */ -GtkGis +GtkWidget *gtk_gis_new_from_xml (xmlDoc *xdoc) { - GtkGis *gtkgis = NULL; + GtkWidget *gtkgis = NULL; xmlNode *cur; cur = xmlDocGetRootElement (xdoc); @@ -135,7 +135,7 @@ GtkGis gtkgis = gtk_gis_new (); - priv = GTK_GIS_GET_PRIVATE (gtkgis); + priv = GTK_GIS_GET_PRIVATE (GTK_GIS (gtkgis)); cur = cur->children; while (cur != NULL) @@ -149,7 +149,7 @@ GtkGis xml_layer = cur; layer_name = xmlGetProp (cur, (const xmlChar *)"name"); - if (layer_name != NULL) + if (layer_name == NULL) { /* TO DO */ /* get layer source name */ @@ -157,55 +157,62 @@ GtkGis } cur = cur->children; - if (cur != NULL - && cur->type == XML_ELEMENT_NODE - && xmlStrcmp (cur->name, "source") == 0) + while (cur != NULL) { - xmlNode *xml_source; - - xml_source = cur; - - if (cur != NULL - && cur->type == XML_ELEMENT_NODE - && (xmlStrcmp (cur->name, "shape") == 0 - || xmlStrcmp (cur->name, "postgis") == 0)) + if (cur->type == XML_ELEMENT_NODE + && xmlStrcmp (cur->name, "source") == 0) { - xmlNode *xml_source_spec; - GtkGisLayer *layer = NULL; - GtkGisLayerSource *layer_source = NULL; + xmlNode *xml_source; - xml_source_spec = cur; + xml_source = cur; - if (xmlStrcmp (cur->name, "shape") == 0) + cur = cur->children; + while (cur != NULL) { - gchar *filename; - - filename = xmlGetProp (cur, (const xmlChar *)"filename"); - if (filename != NULL) + if (cur->type == XML_ELEMENT_NODE + && (xmlStrcmp (cur->name, "shape") == 0 + || xmlStrcmp (cur->name, "postgis") == 0)) { - layer_source = GTK_GIS_LAYER_SOURCE (gtk_gis_layer_source_shp_new (filename)); + xmlNode *xml_source_spec; + GtkGisLayer *layer = NULL; + GtkGisLayerSource *layer_source = NULL; + + xml_source_spec = cur; + + if (xmlStrcmp (cur->name, "shape") == 0) + { + gchar *filename; + + filename = xmlGetProp (cur, (const xmlChar *)"filename"); + if (filename != NULL) + { + layer_source = gtk_gis_layer_source_shp_new ((const gchar *)filename); + } + } + else if (xmlStrcmp (cur->name, "postgis") == 0) + { + layer_source = gtk_gis_layer_source_postgis_new (xmlGetProp (cur, (const xmlChar *)"host"), + atol (xmlGetProp (cur, (const xmlChar *)"port")), + xmlGetProp (cur, (const xmlChar *)"username"), + xmlGetProp (cur, (const xmlChar *)"password")); + } + + if (layer_source != NULL) + { + layer = gtk_gis_layer_new_from_source (layer_source, layer_name); + gtk_gis_add_layer (GTK_GIS (gtkgis), layer); + } + + cur = xml_source_spec; } + cur = cur->next; } - else if (xmlStrcmp (cur->name, "postgis") == 0) - { - /* TO DO */ - } - - if (layer_source != NULL) - { - layer = gtk_gis_layer_new_from_source (layer_source, layer_name); - gtk_gis_add_layer (gtkgis, layer); - } - - cur = xml_source_spec; + cur = xml_source; } - - cur = xml_source; + cur = cur->next; } - cur = xml_layer; } - cur = cur->next; } } @@ -230,10 +237,10 @@ GtkGis * * Returns: the newly created #GtkGis object. */ -GtkGis +GtkWidget *gtk_gis_new_from_file (const gchar *filename) { - GtkGis *gtkgis = NULL; + GtkWidget *gtkgis = NULL; xmlDoc *xdoc = xmlParseFile (filename); if (xdoc != NULL) @@ -262,9 +269,37 @@ void void gtk_gis_add_layer (GtkGis *gtkgis, GtkGisLayer *layer) { - GtkGisPrivate *priv = GTK_GIS_GET_PRIVATE (gtkgis); + GtkGisPrivate *priv; + GtkGisLayerExtent extent; + + priv = GTK_GIS_GET_PRIVATE (gtkgis); priv->layers = g_list_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) + { + 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); } /** @@ -280,6 +315,22 @@ gtk_gis_remove_layer (GtkGis *gtkgis, GtkGisLayer *layer) priv->layers = g_list_remove (priv->layers, layer); } +static void +gtk_gis_add (GtkContainer *container, + GtkWidget *child) +{ + GtkGis *gtkgis; + GtkBin *bin; + + bin = GTK_BIN (container); + g_return_if_fail (bin->child == NULL); + + gtkgis = GTK_GIS (container); + + bin->child = child; + gtk_widget_set_parent (child, GTK_WIDGET (bin)); +} + static void gtk_gis_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { diff --git a/src/gtkgis.h b/src/gtkgis.h index bc1a1f1..9bb0e7b 100644 --- a/src/gtkgis.h +++ b/src/gtkgis.h @@ -20,7 +20,7 @@ #ifndef __GTK_GIS_H__ #define __GTK_GIS_H__ -#include +#include #include #include "layersgroup.h" @@ -42,21 +42,21 @@ typedef struct _GtkGisClass GtkGisClass; struct _GtkGis { - GtkWidget parent; + GtkBin parent; }; struct _GtkGisClass { - GtkWidgetClass parent_class; + GtkBinClass parent_class; }; GType gtk_gis_get_type (void) G_GNUC_CONST; -GtkGis *gtk_gis_new (void); +GtkWidget *gtk_gis_new (void); -GtkGis *gtk_gis_new_from_xml (xmlDoc *xdoc); -GtkGis *gtk_gis_new_from_file (const gchar *filename); +GtkWidget *gtk_gis_new_from_xml (xmlDoc *xdoc); +GtkWidget *gtk_gis_new_from_file (const gchar *filename); void *gtk_gis_save_to_xml (GtkGis *gtkgis, const gchar *filename); diff --git a/src/layer.c b/src/layer.c index c7bc950..9f7b42d 100644 --- a/src/layer.c +++ b/src/layer.c @@ -153,7 +153,7 @@ GtkGisLayer layer = gtk_gis_layer_new (source, name, type); - gtk_gis_layer_source_fill_layer (source, layer); + gtk_gis_layer_fill_from_source (layer); return layer; } @@ -198,7 +198,7 @@ gtk_gis_layer_fill_from_source (GtkGisLayer *layer) { GtkGisLayerPrivate *priv = GTK_GIS_LAYER_GET_PRIVATE (layer); - gtk_gis_layer_source_fill_layer (priv->source, layer); + priv->geometries = gtk_gis_layer_source_get_geometries (priv->source, priv->name); } /** @@ -241,6 +241,23 @@ gtk_gis_layer_remove_geometry (GtkGisLayer *layer, GtkGisGeometry *geometry) priv->geometries = g_list_remove (priv->geometries, geometry); } +/** + * gtk_gis_layer_get_extent: + * @layer: + * + */ +GtkGisLayerExtent +gtk_gis_layer_get_extent (GtkGisLayer *layer) +{ + GtkGisLayerPrivate *priv = GTK_GIS_LAYER_GET_PRIVATE (layer); + + GtkGisLayerExtent extent; + + extent = gtk_gis_layer_source_get_layer_extent (priv->source, priv->name); + + return extent; +} + /** * gtk_gis_layer_get_editable: * @layer: diff --git a/src/layer.h b/src/layer.h index b39d6c8..5e9f3f6 100644 --- a/src/layer.h +++ b/src/layer.h @@ -76,6 +76,8 @@ void gtk_gis_layer_add_geometry (GtkGisLayer *layer, void gtk_gis_layer_remove_geometry (GtkGisLayer *layer, GtkGisGeometry *geometry); +GtkGisLayerExtent gtk_gis_layer_get_extent (GtkGisLayer *layer); + gboolean gtk_gis_layer_get_editable (GtkGisLayer *layer); void gtk_gis_layer_set_editable (GtkGisLayer *layer, gboolean editable); diff --git a/src/layersgroup.c b/src/layersgroup.c index 024540d..1a4b236 100644 --- a/src/layersgroup.c +++ b/src/layersgroup.c @@ -105,16 +105,16 @@ gtk_gis_layers_group_init (GtkGisLayersGroup *gtk_gis_layers_group) GtkGisLayersGroup *gtk_gis_layers_group_new (const gchar *name) { - GtkGisLayersGroup *gtk_gis_layers_group; + GtkGisLayersGroup *group; GtkGisLayersGroupPrivate *priv; - gtk_gis_layers_group = GTK_GIS_LAYERS_GROUP (g_object_new (gtk_gis_layers_group_get_type (), NULL)); + group = GTK_GIS_LAYERS_GROUP (g_object_new (gtk_gis_layers_group_get_type (), NULL)); - priv = GTK_GIS_LAYERS_GROUP_GET_PRIVATE (gtk_gis_layers_group); + priv = GTK_GIS_LAYERS_GROUP_GET_PRIVATE (group); priv->name = g_strdup (name); - return gtk_gis_layers_group; + return group; } /** diff --git a/src/layersource.c b/src/layersource.c index 571d269..91a88ab 100644 --- a/src/layersource.c +++ b/src/layersource.c @@ -24,10 +24,17 @@ #include #include "layersource.h" +#include "geometryline.h" +#include "geometrypoint.h" +#include "geometrypolygon.h" static void gtk_gis_layer_source_class_init (GtkGisLayerSourceClass *klass); static void gtk_gis_layer_source_init (GtkGisLayerSource *gtk_gis_layer_source); +static void +gtk_gis_layer_source_set_ogr_datasource (GtkGisLayerSource *source, + OGRDataSourceH datasource); + static void gtk_gis_layer_source_set_property (GObject *object, guint property_id, const GValue *value, @@ -46,33 +53,7 @@ struct _GtkGisLayerSourcePrivate OGRDataSourceH datasource; }; -GType -gtk_gis_layer_source_get_type (void) -{ - static GType gtk_gis_layer_source_type = 0; - - if (!gtk_gis_layer_source_type) - { - static const GTypeInfo gtk_gis_layer_source_info = - { - sizeof (GtkGisLayerSourceClass), - NULL, /* base_init */ - NULL, /* base_finalize */ - (GClassInitFunc) gtk_gis_layer_source_class_init, - NULL, /* class_finalize */ - NULL, /* class_data */ - sizeof (GtkGisLayerSource), - 0, /* n_preallocs */ - (GInstanceInitFunc) gtk_gis_layer_source_init, - NULL - }; - - gtk_gis_layer_source_type = g_type_register_static (G_TYPE_OBJECT, "GtkGisLayerSource", - >k_gis_layer_source_info, 0); - } - - return gtk_gis_layer_source_type; -} +G_DEFINE_TYPE (GtkGisLayerSource, gtk_gis_layer_source, G_TYPE_OBJECT) static void gtk_gis_layer_source_class_init (GtkGisLayerSourceClass *klass) @@ -81,6 +62,8 @@ gtk_gis_layer_source_class_init (GtkGisLayerSourceClass *klass) g_type_class_add_private (object_class, sizeof (GtkGisLayerSourcePrivate)); + klass->set_ogr_datasource = gtk_gis_layer_source_set_ogr_datasource; + object_class->set_property = gtk_gis_layer_source_set_property; object_class->get_property = gtk_gis_layer_source_get_property; } @@ -91,20 +74,129 @@ gtk_gis_layer_source_init (GtkGisLayerSource *gtk_gis_layer_source) GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (gtk_gis_layer_source); } +static void +gtk_gis_layer_source_set_ogr_datasource (GtkGisLayerSource *source, + OGRDataSourceH datasource) +{ + GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (source); + + priv->datasource = datasource; +} + + /** * gtk_gis_layer_source_get_geometries: * @source: + * @name: + * + * Loads the #GtkGisLayerSource from the disk. * + * Returns: . */ GList *gtk_gis_layer_source_get_geometries (GtkGisLayerSource *source, const gchar *name) { - GList *geometries; + GList *geometries = NULL; - if (IS_GTK_GIS_LAYER_SOURCE_OBJECT (source) && GTK_GIS_LAYER_SOURCE_GET_CLASS (source)->get_geometries != NULL) + GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (source); + + if (priv->datasource == NULL) + { + /* TO DO */ + } + else { - geometries = GTK_GIS_LAYER_SOURCE_GET_CLASS (source)->get_geometries (source, name); + OGRLayerH layer; + OGRFeatureDefnH layerDefn; + OGRFeatureH feature; + OGRGeometryH poGeometry; + GtkGisGeometry *geometry; + + layer = OGR_DS_GetLayerByName (priv->datasource, name); + + layerDefn = OGR_L_GetLayerDefn (layer); + + while ((feature = OGR_L_GetNextFeature (layer)) != NULL) + { + poGeometry = OGR_F_GetGeometryRef (feature); + + if (poGeometry != NULL + && wkbFlatten(OGR_G_GetGeometryType (poGeometry)) == wkbPoint) + { + GtkGisPoint gpoint; + + gpoint.x = OGR_G_GetX (poGeometry, 0); + gpoint.y = OGR_G_GetY (poGeometry, 0); + + geometry = gtk_gis_geometry_point_new (gpoint); + } + else if (poGeometry != NULL + && wkbFlatten(OGR_G_GetGeometryType (poGeometry)) == wkbLineString) + { + gint points; + gint point; + GPtrArray *gar_points; + + GtkGisPoint *gpoint; + + 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_line_new_from_array (gar_points); + } + 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); + } } return geometries; @@ -119,24 +211,79 @@ GList */ GtkGisLayerGeometryType gtk_gis_layer_source_get_layer_geometry_type (GtkGisLayerSource *source, - gchar *name) + const gchar *name) { GtkGisLayerGeometryType type = -1; - if (IS_GTK_GIS_LAYER_SOURCE_OBJECT (source) && GTK_GIS_LAYER_SOURCE_GET_CLASS (source)->get_layer_geometry_type != NULL) + OGRLayerH layer; + OGRFeatureH feature; + + GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (source); + + layer = OGR_DS_GetLayerByName (priv->datasource, name); + + feature = OGR_L_GetNextFeature (layer); + + if (feature != NULL) { - type = GTK_GIS_LAYER_SOURCE_GET_CLASS (source)->get_layer_geometry_type (source, name); + OGRGeometryH poGeometry; + + poGeometry = OGR_F_GetGeometryRef (feature); + if (poGeometry != NULL + && wkbFlatten(OGR_G_GetGeometryType(poGeometry)) == wkbPoint) + { + type = LAYER_GEOMETRY_TYPE_POINT; + } + else if (poGeometry != NULL + && wkbFlatten(OGR_G_GetGeometryType(poGeometry)) == wkbLineString) + { + type = LAYER_GEOMETRY_TYPE_LINE; + } + else if (poGeometry != NULL + && wkbFlatten(OGR_G_GetGeometryType(poGeometry)) == wkbPolygon) + { + type = LAYER_GEOMETRY_TYPE_POLYGON; + } } return type; } +/** + * gtk_gis_layer_source_get_layer_extent: + * @source: + * @name: + * + * Returns: + */ +GtkGisLayerExtent +gtk_gis_layer_source_get_layer_extent (GtkGisLayerSource *source, + const gchar *name) +{ + OGRLayerH layer; + OGREnvelope psExtent; + GtkGisLayerExtent extent; + + GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (source); + + layer = OGR_DS_GetLayerByName (priv->datasource, name); + + OGR_L_GetExtent (layer, &psExtent, 1); + + extent.min_x = psExtent.MinX; + extent.min_y = psExtent.MinY; + extent.max_x = psExtent.MaxX; + extent.max_y = psExtent.MaxY; + + return extent; +} + static void gtk_gis_layer_source_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { - GtkGisLayerSource *gtk_gis_layer_source = GTK_GIS_LAYER_SOURCE (object); + GtkGisLayerSource *source = GTK_GIS_LAYER_SOURCE (object); - GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (gtk_gis_layer_source); + GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (source); switch (property_id) { @@ -149,9 +296,9 @@ gtk_gis_layer_source_set_property (GObject *object, guint property_id, const GVa static void gtk_gis_layer_source_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { - GtkGisLayerSource *gtk_gis_layer_source = GTK_GIS_LAYER_SOURCE (object); + GtkGisLayerSource *source = GTK_GIS_LAYER_SOURCE (object); - GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (gtk_gis_layer_source); + GtkGisLayerSourcePrivate *priv = GTK_GIS_LAYER_SOURCE_GET_PRIVATE (source); switch (property_id) { diff --git a/src/layersource.h b/src/layersource.h index 7c91002..5a72e54 100644 --- a/src/layersource.h +++ b/src/layersource.h @@ -23,6 +23,9 @@ #include #include +#include +#include + #include "geometry.h" G_BEGIN_DECLS @@ -37,12 +40,20 @@ G_BEGIN_DECLS typedef enum -{ - LAYER_GEOMETRY_TYPE_POINT, - LAYER_GEOMETRY_TYPE_LINE, - LAYER_GEOMETRY_TYPE_POLYGON, - LAYER_GEOMETRY_TYPE_RASTER -} GtkGisLayerGeometryType; + { + LAYER_GEOMETRY_TYPE_POINT, + LAYER_GEOMETRY_TYPE_LINE, + LAYER_GEOMETRY_TYPE_POLYGON, + LAYER_GEOMETRY_TYPE_RASTER + } GtkGisLayerGeometryType; + +typedef struct + { + gdouble min_x; + gdouble min_y; + gdouble max_x; + gdouble max_y; + } GtkGisLayerExtent; typedef struct _GtkGisLayerSource GtkGisLayerSource; @@ -57,10 +68,8 @@ struct _GtkGisLayerSourceClass { GObjectClass parent_class; - GList *(*get_geometries) (GtkGisLayerSource *source, const gchar *name); - - GtkGisLayerGeometryType (*get_layer_geometry_type) (GtkGisLayerSource *source, - gchar *name); + void (*set_ogr_datasource) (GtkGisLayerSource *source, + OGRDataSourceH datasource); }; GType gtk_gis_layer_source_get_type (void) G_GNUC_CONST; @@ -70,7 +79,10 @@ GList *gtk_gis_layer_source_get_geometries (GtkGisLayerSource *source, const gchar *name); GtkGisLayerGeometryType gtk_gis_layer_source_get_layer_geometry_type (GtkGisLayerSource *source, - gchar *name); + const gchar *name); + +GtkGisLayerExtent gtk_gis_layer_source_get_layer_extent (GtkGisLayerSource *source, + const gchar *name); G_END_DECLS diff --git a/src/layersourcepostgis.c b/src/layersourcepostgis.c index b6afb77..b0ce113 100644 --- a/src/layersourcepostgis.c +++ b/src/layersourcepostgis.c @@ -67,7 +67,7 @@ gtk_gis_layer_source_postgis_get_type (void) NULL }; - gtk_gis_layer_source_postgis_type = g_type_register_static (G_TYPE_OBJECT, "GtkGisLayerSourcePostgis", + gtk_gis_layer_source_postgis_type = g_type_register_static (TYPE_GTK_GIS_LAYER_SOURCE, "GtkGisLayerSourcePostgis", >k_gis_layer_source_postgis_info, 0); } @@ -102,33 +102,33 @@ gtk_gis_layer_source_postgis_init (GtkGisLayerSourcePostgis *gtk_gis_layer_sourc * * Returns: the newly created #GtkGisLayerSourcePostgis object. */ -GtkGisLayerSourcePostgis +GtkGisLayerSource *gtk_gis_layer_source_postgis_new (gchar *host, gint port, gchar *username, gchar *password) { - GtkGisLayerSourcePostgis *gtk_gis_layer_source_postgis; + GtkGisLayerSource *source; GtkGisLayerSourcePostgisPrivate *priv; - gtk_gis_layer_source_postgis = GTK_GIS_LAYER_SOURCE_POSTGIS (g_object_new (gtk_gis_layer_source_postgis_get_type (), NULL)); + source = g_object_new (gtk_gis_layer_source_postgis_get_type (), NULL); - priv = GTK_GIS_LAYER_SOURCE_POSTGIS_GET_PRIVATE (gtk_gis_layer_source_postgis); + priv = GTK_GIS_LAYER_SOURCE_POSTGIS_GET_PRIVATE (GTK_GIS_LAYER_SOURCE_POSTGIS (source)); priv->host = g_strdup (host == NULL ? "localhost" : host); priv->port = (port == 0 ? 5432 : port); priv->username = g_strdup (username == NULL ? "" : username); priv->password = g_strdup (password == NULL ? "" : password); - return gtk_gis_layer_source_postgis; + return source; } static void gtk_gis_layer_source_postgis_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { - GtkGisLayerSourcePostgis *gtk_gis_layer_source_postgis = GTK_GIS_LAYER_SOURCE_POSTGIS (object); + GtkGisLayerSourcePostgis *source = GTK_GIS_LAYER_SOURCE_POSTGIS (object); - GtkGisLayerSourcePostgisPrivate *priv = GTK_GIS_LAYER_SOURCE_POSTGIS_GET_PRIVATE (gtk_gis_layer_source_postgis); + GtkGisLayerSourcePostgisPrivate *priv = GTK_GIS_LAYER_SOURCE_POSTGIS_GET_PRIVATE (source); switch (property_id) { @@ -141,9 +141,9 @@ gtk_gis_layer_source_postgis_set_property (GObject *object, guint property_id, c static void gtk_gis_layer_source_postgis_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { - GtkGisLayerSourcePostgis *gtk_gis_layer_source_postgis = GTK_GIS_LAYER_SOURCE_POSTGIS (object); + GtkGisLayerSourcePostgis *source = GTK_GIS_LAYER_SOURCE_POSTGIS (object); - GtkGisLayerSourcePostgisPrivate *priv = GTK_GIS_LAYER_SOURCE_POSTGIS_GET_PRIVATE (gtk_gis_layer_source_postgis); + GtkGisLayerSourcePostgisPrivate *priv = GTK_GIS_LAYER_SOURCE_POSTGIS_GET_PRIVATE (source); switch (property_id) { diff --git a/src/layersourcepostgis.h b/src/layersourcepostgis.h index a9bd32b..ccba260 100644 --- a/src/layersourcepostgis.h +++ b/src/layersourcepostgis.h @@ -49,10 +49,10 @@ struct _GtkGisLayerSourcePostgisClass GType gtk_gis_layer_source_postgis_get_type (void) G_GNUC_CONST; -GtkGisLayerSourcePostgis *gtk_gis_layer_source_postgis_new (gchar *host, - gint port, - gchar *username, - gchar *password); +GtkGisLayerSource *gtk_gis_layer_source_postgis_new (gchar *host, + gint port, + gchar *username, + gchar *password); G_END_DECLS diff --git a/src/layersourceshp.c b/src/layersourceshp.c index e51ecdd..5132a51 100644 --- a/src/layersourceshp.c +++ b/src/layersourceshp.c @@ -24,14 +24,13 @@ #include #include "layersourceshp.h" -#include "geometryline.h" -#include "geometrypoint.h" -#include "geometrypolygon.h" static void gtk_gis_layer_source_shp_class_init (GtkGisLayerSourceShpClass *klass); static void gtk_gis_layer_source_shp_init (GtkGisLayerSourceShp *gtk_gis_layer_source_shp); -static OGRDataSourceH gtk_gis_layer_source_get_ogr_datasource (GtkGisLayerSource *source); +static void +gtk_gis_layer_source_shp_set_ogr_datasource (GtkGisLayerSource *source, + OGRDataSourceH datasource); static void gtk_gis_layer_source_shp_set_property (GObject *object, guint property_id, @@ -49,8 +48,6 @@ typedef struct _GtkGisLayerSourceShpPrivate GtkGisLayerSourceShpPrivate; struct _GtkGisLayerSourceShpPrivate { gchar *filename; - - OGRDataSourceH datasource; }; GType @@ -74,7 +71,7 @@ gtk_gis_layer_source_shp_get_type (void) NULL }; - gtk_gis_layer_source_shp_type = g_type_register_static (G_TYPE_OBJECT, "GtkGisLayerSourceShp", + gtk_gis_layer_source_shp_type = g_type_register_static (TYPE_GTK_GIS_LAYER_SOURCE, "GtkGisLayerSourceShp", >k_gis_layer_source_shp_info, 0); } @@ -91,9 +88,6 @@ gtk_gis_layer_source_shp_class_init (GtkGisLayerSourceShpClass *klass) object_class->set_property = gtk_gis_layer_source_shp_set_property; object_class->get_property = gtk_gis_layer_source_shp_get_property; - - gtkgislayersource_class->get_geometries = gtk_gis_layer_source_shp_get_geometries; - gtkgislayersource_class->get_layer_geometry_type = gtk_gis_layer_source_shp_get_layer_geometry_type; } static void @@ -110,14 +104,15 @@ gtk_gis_layer_source_shp_init (GtkGisLayerSourceShp *gtk_gis_layer_source_shp) * * Returns: the newly created #GtkGisLayerSourceShp object. */ -GtkGisLayerSourceShp -*gtk_gis_layer_source_shp_new (gchar *filename) +GtkGisLayerSource +*gtk_gis_layer_source_shp_new (const gchar *filename) { - GtkGisLayerSourceShp *gtk_gis_layer_source_shp; + GtkGisLayerSource *source; + GtkGisLayerSourceShpPrivate *priv; - gtk_gis_layer_source_shp = GTK_GIS_LAYER_SOURCE_SHP (g_object_new (gtk_gis_layer_source_shp_get_type (), NULL)); + source = g_object_new (gtk_gis_layer_source_shp_get_type (), NULL); - GtkGisLayerSourceShpPrivate *priv = GTK_GIS_LAYER_SOURCE_SHP_GET_PRIVATE (gtk_gis_layer_source_shp); + priv = GTK_GIS_LAYER_SOURCE_SHP_GET_PRIVATE (GTK_GIS_LAYER_SOURCE_SHP (source)); priv->filename = g_strdup (filename); @@ -125,9 +120,10 @@ GtkGisLayerSourceShp OGRRegisterAll (); /* Open data source */ - priv->datasource = OGROpen (priv->filename, 0 /* bUpdate */, NULL); + gtk_gis_layer_source_shp_set_ogr_datasource (source, + OGROpen (priv->filename, 0 /* bUpdate */, NULL)); - return gtk_gis_layer_source_shp; + return source; } /** @@ -144,144 +140,22 @@ gchar return g_strdup (priv->filename); } -/** - * gtk_gis_layer_source_shp_get_geometries: - * @source: - * @name: - * - * Loads the #GtkGisLayerSource from the disk. - * - * Returns: . - */ -GList -*gtk_gis_layer_source_shp_get_geometries (GtkGisLayerSource *source, - const gchar *name) +static void +gtk_gis_layer_source_shp_set_ogr_datasource (GtkGisLayerSource *source, + OGRDataSourceH datasource) { - GList *geometries = NULL; - - GtkGisLayerSourceShpPrivate *priv = GTK_GIS_LAYER_SOURCE_SHP_GET_PRIVATE (source); - - if (priv->datasource == NULL) - { - /* TO DO */ - } - else + if (IS_GTK_GIS_LAYER_SOURCE (source) && GTK_GIS_LAYER_SOURCE_GET_CLASS (source)->set_ogr_datasource != NULL) { - gint numLayers; - gint i; - - OGRLayerH layer; - OGRFeatureDefnH layerDefn; - - GtkGisLayerSourceShpPrivate *priv = GTK_GIS_LAYER_SOURCE_SHP_GET_PRIVATE (source); - - numLayers = OGR_DS_GetLayerCount (priv->datasource); - for (i = 0; i < numLayers; i++) - { - layer = OGR_DS_GetLayer (priv->datasource, i); - layerDefn = OGR_L_GetLayerDefn (layer); - - if (strcmp (name, OGR_FD_GetName(layerDefn)) == 0) - { - OGRFeatureH feature; - OGRGeometryH poGeometry; - GtkGisGeometry *geometry; - - while ((feature = OGR_L_GetNextFeature (layer)) != NULL) - { - poGeometry = OGR_F_GetGeometryRef (feature); - if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType (poGeometry)) == wkbPoint) - { - geometry = GTK_GIS_GEOMETRY (gtk_gis_geometry_point_new (OGR_G_GetX (poGeometry, 0), OGR_G_GetY (poGeometry, 0))); - } - else if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType (poGeometry)) == wkbLineString) - { - } - else if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType (poGeometry)) == wkbPolygon) - { - } - - geometries = g_list_append (geometries, geometry); - } - - break; - } - } - } - - return geometries; -} - -/** - * gtk_gis_layer_source_shp_get_layer_geometry_type: - * @source: - * @name: - * - * Returns: a #GtkGisLayerGeometryType or -1 on error. - */ -GtkGisLayerGeometryType -gtk_gis_layer_source_shp_get_layer_geometry_type (GtkGisLayerSource *source, - gchar *name) -{ - GtkGisLayerGeometryType type = -1; - gint numLayers; - gint i; - - OGRLayerH layer; - OGRFeatureDefnH layerDefn; - - GtkGisLayerSourceShpPrivate *priv = GTK_GIS_LAYER_SOURCE_SHP_GET_PRIVATE (source); - - numLayers = OGR_DS_GetLayerCount (priv->datasource); - for (i = 0; i < numLayers; i++) - { - layer = OGR_DS_GetLayer (priv->datasource, i); - layerDefn = OGR_L_GetLayerDefn (layer); - - if (strcmp (name, OGR_FD_GetName(layerDefn)) == 0) - { - OGRFeatureH feature; - - feature = OGR_L_GetNextFeature (layer); - - if (feature != NULL) - { - OGRGeometryH poGeometry; - - poGeometry = OGR_F_GetGeometryRef (feature); - if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType(poGeometry)) == wkbPoint) - { - type = LAYER_GEOMETRY_TYPE_POINT; - } - else if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType(poGeometry)) == wkbLineString) - { - type = LAYER_GEOMETRY_TYPE_LINE; - } - else if (poGeometry != NULL - && wkbFlatten(OGR_G_GetGeometryType(poGeometry)) == wkbPolygon) - { - type = LAYER_GEOMETRY_TYPE_POLYGON; - } - } - - break; - } + GTK_GIS_LAYER_SOURCE_GET_CLASS (source)->set_ogr_datasource (source, datasource); } - - return type; } static void gtk_gis_layer_source_shp_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { - GtkGisLayerSourceShp *gtk_gis_layer_source_shp = GTK_GIS_LAYER_SOURCE_SHP (object); + GtkGisLayerSourceShp *source = GTK_GIS_LAYER_SOURCE_SHP (object); - GtkGisLayerSourceShpPrivate *priv = GTK_GIS_LAYER_SOURCE_SHP_GET_PRIVATE (gtk_gis_layer_source_shp); + GtkGisLayerSourceShpPrivate *priv = GTK_GIS_LAYER_SOURCE_SHP_GET_PRIVATE (source); switch (property_id) { @@ -294,9 +168,9 @@ gtk_gis_layer_source_shp_set_property (GObject *object, guint property_id, const static void gtk_gis_layer_source_shp_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { - GtkGisLayerSourceShp *gtk_gis_layer_source_shp = GTK_GIS_LAYER_SOURCE_SHP (object); + GtkGisLayerSourceShp *source = GTK_GIS_LAYER_SOURCE_SHP (object); - GtkGisLayerSourceShpPrivate *priv = GTK_GIS_LAYER_SOURCE_SHP_GET_PRIVATE (gtk_gis_layer_source_shp); + GtkGisLayerSourceShpPrivate *priv = GTK_GIS_LAYER_SOURCE_SHP_GET_PRIVATE (source); switch (property_id) { diff --git a/src/layersourceshp.h b/src/layersourceshp.h index fba6629..efe82e7 100644 --- a/src/layersourceshp.h +++ b/src/layersourceshp.h @@ -49,15 +49,10 @@ struct _GtkGisLayerSourceShpClass GType gtk_gis_layer_source_shp_get_type (void) G_GNUC_CONST; -GtkGisLayerSourceShp *gtk_gis_layer_source_shp_new (gchar *filename); +GtkGisLayerSource *gtk_gis_layer_source_shp_new (const gchar *filename); gchar *gtk_gis_layer_get_filename (GtkGisLayerSourceShp *source); -GList *gtk_gis_layer_source_shp_get_geometries (GtkGisLayerSource *source, const gchar *name); - -GtkGisLayerGeometryType gtk_gis_layer_source_shp_get_layer_geometry_type (GtkGisLayerSource *source, - gchar *name); - G_END_DECLS diff --git a/tests/Makefile.am b/tests/Makefile.am index a6fb6c5..1a40d6a 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,14 +1,16 @@ SUBDIRS = samples -LIBS = $(GTKGIS_LIBS) +LIBS = $(GTKGIS_LIBS) \ + $(GDAL_LIBS) AM_CPPFLAGS = $(GTKGIS_CFLAGS) \ + $(GDAL_CFLAGS) \ -I$(top_srcdir)/src libgtkgis = $(top_builddir)/src/libgtkgis.la -noinst_PROGRAMS = from_xml +LDADD = $(libgtkgis) -LDADD = $(libgtgkis) +noinst_PROGRAMS = from_xml EXTRA_DIST = test1.gtkgis diff --git a/tests/from_xml.c b/tests/from_xml.c index f7107fc..afa64c7 100644 --- a/tests/from_xml.c +++ b/tests/from_xml.c @@ -3,5 +3,24 @@ int main (int argc, char *argv[]) { + GtkWidget *window; + GtkWidget *gtkgis; + + gtk_init (&argc, &argv); + + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_default_size (GTK_WINDOW (window), 640, 600); + + g_signal_connect (window, "delete_event", (GtkSignalFunc) gtk_main_quit, + NULL); + + gtkgis = gtk_gis_new_from_file (argv[1]); + + gtk_container_add (GTK_CONTAINER (window), gtkgis); + + gtk_widget_show_all (window); + + gtk_main (); + return 0; } diff --git a/tests/test1.gtkgis b/tests/test1.gtkgis index 9fd53c6..bf96191 100644 --- a/tests/test1.gtkgis +++ b/tests/test1.gtkgis @@ -2,26 +2,26 @@ - + - + -- 2.49.0