1 : /******************************************************************************
2 : * $Id: ogrgeojsonutils.cpp 19489 2010-04-21 21:39:05Z rouault $
3 : *
4 : * Project: OpenGIS Simple Features Reference Implementation
5 : * Purpose: Implementation of private utilities used within OGR GeoJSON Driver.
6 : * Author: Mateusz Loskot, mateusz@loskot.net
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2007, Mateusz Loskot
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 : #include "ogrgeojsonutils.h"
30 : #include <cpl_port.h>
31 : #include <cpl_conv.h>
32 : #include <ogr_geometry.h>
33 : #include <jsonc/json.h> // JSON-C
34 :
35 : /************************************************************************/
36 : /* GeoJSONIsObject() */
37 : /************************************************************************/
38 :
39 202 : int GeoJSONIsObject( const char* pszText )
40 : {
41 202 : if( NULL == pszText )
42 0 : return FALSE;
43 :
44 : /* -------------------------------------------------------------------- */
45 : /* This is a primitive test, but we need to perform it fast. */
46 : /* -------------------------------------------------------------------- */
47 404 : while( *pszText != '\0' && isspace( (unsigned char)*pszText ) )
48 0 : pszText++;
49 :
50 202 : if( EQUALN( pszText, "{", 1) )
51 23 : return TRUE;
52 :
53 179 : return FALSE;
54 : }
55 :
56 : /************************************************************************/
57 : /* GeoJSONGetSourceType() */
58 : /************************************************************************/
59 :
60 201 : GeoJSONSourceType GeoJSONGetSourceType( const char* pszSource )
61 : {
62 201 : GeoJSONSourceType srcType = eGeoJSONSourceUnknown;
63 :
64 : // NOTE: Sometimes URL ends with .geojson token, for example
65 : // http://example/path/2232.geojson
66 : // It's important to test beginning of source first.
67 201 : if ( eGeoJSONProtocolUnknown != GeoJSONGetProtocolType( pszSource ) )
68 : {
69 0 : srcType = eGeoJSONSourceService;
70 : }
71 201 : else if( EQUAL( CPLGetExtension( pszSource ), "geojson" )
72 : || EQUAL( CPLGetExtension( pszSource ), "json" )
73 : || ((EQUALN( pszSource, "/vsigzip/", 9) || EQUALN( pszSource, "/vsizip/", 8)) &&
74 : (strstr( pszSource, ".json") || strstr( pszSource, ".JSON") ||
75 : strstr( pszSource, ".geojson") || strstr( pszSource, ".GEOJSON")) ))
76 : {
77 21 : srcType = eGeoJSONSourceFile;
78 : }
79 : else
80 : {
81 180 : if( GeoJSONIsObject( pszSource ) )
82 1 : srcType = eGeoJSONSourceText;
83 : }
84 :
85 201 : return srcType;
86 : }
87 :
88 : /************************************************************************/
89 : /* GeoJSONGetProtocolType() */
90 : /************************************************************************/
91 :
92 201 : GeoJSONProtocolType GeoJSONGetProtocolType( const char* pszSource )
93 : {
94 201 : GeoJSONProtocolType ptclType = eGeoJSONProtocolUnknown;
95 :
96 201 : if( EQUALN( pszSource, "http:", 5 ) )
97 0 : ptclType = eGeoJSONProtocolHTTP;
98 201 : else if( EQUALN( pszSource, "https:", 6 ) )
99 0 : ptclType = eGeoJSONProtocolHTTPS;
100 201 : else if( EQUALN( pszSource, "ftp:", 4 ) )
101 0 : ptclType = eGeoJSONProtocolFTP;
102 :
103 201 : return ptclType;
104 : }
105 :
106 : /************************************************************************/
107 : /* GeoJSONPropertyToFieldType() */
108 : /************************************************************************/
109 :
110 24 : OGRFieldType GeoJSONPropertyToFieldType( json_object* poObject )
111 : {
112 24 : if (poObject == NULL) { return OFTString; }
113 :
114 24 : json_type type = json_object_get_type( poObject );
115 :
116 24 : if( json_type_boolean == type )
117 0 : return OFTInteger;
118 24 : else if( json_type_double == type )
119 12 : return OFTReal;
120 12 : else if( json_type_int == type )
121 0 : return OFTInteger;
122 12 : else if( json_type_string == type )
123 12 : return OFTString;
124 0 : else if( json_type_array == type )
125 0 : return OFTStringList; /* string or JSON-string */
126 : else
127 0 : return OFTString; /* null, object */
128 : }
129 :
130 : /************************************************************************/
131 : /* OGRGeoJSONGetGeometryName() */
132 : /************************************************************************/
133 :
134 26 : const char* OGRGeoJSONGetGeometryName( OGRGeometry const* poGeometry )
135 : {
136 26 : CPLAssert( NULL != poGeometry );
137 :
138 26 : OGRwkbGeometryType eType = poGeometry->getGeometryType();
139 :
140 26 : if( wkbPoint == eType || wkbPoint25D == eType )
141 3 : return "Point";
142 23 : else if( wkbLineString == eType || wkbLineString25D == eType )
143 3 : return "LineString";
144 20 : else if( wkbPolygon == eType || wkbPolygon25D == eType )
145 5 : return "Polygon";
146 15 : else if( wkbMultiPoint == eType || wkbMultiPoint25D == eType )
147 4 : return "MultiPoint";
148 11 : else if( wkbMultiLineString == eType || wkbMultiLineString25D == eType )
149 4 : return "MultiLineString";
150 7 : else if( wkbMultiPolygon == eType || wkbMultiPolygon25D == eType )
151 5 : return "MultiPolygon";
152 2 : else if( wkbGeometryCollection == eType || wkbGeometryCollection25D == eType )
153 2 : return "GeometryCollection";
154 : else
155 0 : return "Unknown";
156 : }
157 :
|