1 : /******************************************************************************
2 : * $Id$
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 103 : int GeoJSONIsObject( const char* pszText )
40 : {
41 103 : if( NULL == pszText )
42 0 : return FALSE;
43 :
44 : /* -------------------------------------------------------------------- */
45 : /* This is a primitive test, but we need to perform it fast. */
46 : /* -------------------------------------------------------------------- */
47 206 : while( *pszText != '\0' && isspace( (unsigned char)*pszText ) )
48 0 : pszText++;
49 :
50 103 : if( EQUALN( pszText, "{", 1) )
51 0 : return TRUE;
52 :
53 103 : return FALSE;
54 : }
55 :
56 : /************************************************************************/
57 : /* GeoJSONGetSourceType() */
58 : /************************************************************************/
59 :
60 117 : GeoJSONSourceType GeoJSONGetSourceType( const char* pszSource )
61 : {
62 117 : 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 117 : if ( eGeoJSONProtocolUnknown != GeoJSONGetProtocolType( pszSource ) )
68 : {
69 0 : srcType = eGeoJSONSourceService;
70 : }
71 117 : else if( EQUAL( CPLGetExtension( pszSource ), "geojson" )
72 : || EQUAL( CPLGetExtension( pszSource ), "json" ) )
73 : {
74 14 : srcType = eGeoJSONSourceFile;
75 : }
76 : else
77 : {
78 103 : if( GeoJSONIsObject( pszSource ) )
79 0 : srcType = eGeoJSONSourceText;
80 : }
81 :
82 117 : return srcType;
83 : }
84 :
85 : /************************************************************************/
86 : /* GeoJSONGetProtocolType() */
87 : /************************************************************************/
88 :
89 117 : GeoJSONProtocolType GeoJSONGetProtocolType( const char* pszSource )
90 : {
91 117 : GeoJSONProtocolType ptclType = eGeoJSONProtocolUnknown;
92 :
93 117 : if( EQUALN( pszSource, "http:", 5 ) )
94 0 : ptclType = eGeoJSONProtocolHTTP;
95 117 : else if( EQUALN( pszSource, "https:", 6 ) )
96 0 : ptclType = eGeoJSONProtocolHTTPS;
97 117 : else if( EQUALN( pszSource, "ftp:", 4 ) )
98 0 : ptclType = eGeoJSONProtocolFTP;
99 :
100 117 : return ptclType;
101 : }
102 :
103 : /************************************************************************/
104 : /* GeoJSONPropertyToFieldType() */
105 : /************************************************************************/
106 :
107 12 : OGRFieldType GeoJSONPropertyToFieldType( json_object* poObject )
108 : {
109 12 : if (poObject == NULL) { return OFTString; }
110 :
111 12 : json_type type = json_object_get_type( poObject );
112 :
113 12 : if( json_type_boolean == type )
114 0 : return OFTInteger;
115 12 : else if( json_type_double == type )
116 6 : return OFTReal;
117 6 : else if( json_type_int == type )
118 0 : return OFTInteger;
119 6 : else if( json_type_string == type )
120 6 : return OFTString;
121 0 : else if( json_type_array == type )
122 0 : return OFTStringList; /* string or JSON-string */
123 : else
124 0 : return OFTString; /* null, object */
125 : }
126 :
127 : /************************************************************************/
128 : /* OGRGeoJSONGetGeometryName() */
129 : /************************************************************************/
130 :
131 6 : const char* OGRGeoJSONGetGeometryName( OGRGeometry const* poGeometry )
132 : {
133 : CPLAssert( NULL != poGeometry );
134 :
135 6 : OGRwkbGeometryType eType = poGeometry->getGeometryType();
136 :
137 6 : if( wkbPoint == eType || wkbPoint25D == eType )
138 1 : return "Point";
139 5 : else if( wkbLineString == eType || wkbLineString25D == eType )
140 1 : return "LineString";
141 4 : else if( wkbPolygon == eType || wkbPolygon25D == eType )
142 1 : return "Polygon";
143 3 : else if( wkbMultiPoint == eType || wkbMultiPoint25D == eType )
144 1 : return "MultiPoint";
145 2 : else if( wkbMultiLineString == eType || wkbMultiLineString25D == eType )
146 1 : return "MultiLineString";
147 1 : else if( wkbMultiPolygon == eType || wkbMultiPolygon25D == eType )
148 1 : return "MultiPolygon";
149 0 : else if( wkbGeometryCollection == eType || wkbGeometryCollection25D == eType )
150 0 : return "GeometryCollection";
151 : else
152 0 : return "Unknown";
153 : }
154 :
|