I've converted a huge Shapefile into a SpatiaLite file because I need to make a query that uses a spatial index. My problem started on this question where I wanted to filter a specific area of a Shapefile once I give it a central point. To sum up, I already have the query that I need to use inside SpatiaLite to get the result that I want, and it's the following:
SELECT AsText(GEOMETRY) FROM lotes WHERE ST_Area(ST_Intersection(ST_Buffer(ST_GeomFromText('POINT ( 339298.446287788 7391719.02695803 )', 4326), 100), lotes.geometry)) > 0 AND lotes.rowid IN (SELECT rowid FROM SpatialIndex WHERE f_table_name = 'lotes' AND search_frame = ST_Buffer(ST_GeomFromText('POINT ( 339298.446287788 7391719.02695803 )', 4326), 100));
So far everything is fine. Since I'm using an index inside Spatialite, I get the result of this query very fast (that doesn't happen working directly with the Shapefile). In this particular case, I'm filtering an area of my spatial data and printing only one column as the following:
POLYGON((339380.3182 7391671.753232, 339374.934121 7391681.671268, 339374.280746 7391682.874854, 339370.941839 7391689.025468, 339370.551171 7391689.745119, 339368.918082 7391692.75344, 339366.908313 7391691.723008, 339365.730444 7391691.1191, 339364.196167 7391690.332457, 339361.078168 7391695.89327, 339362.318072 7391696.583444, 339366.930247 7391699.069134, 339370.258422 7391700.862824, 339371.949654 7391701.774299, 339377.618093 7391691.15344, 339386.428107 7391675.083422, 339380.3182 7391671.753232))
POLYGON((339279.256409 7391651.056254, 339282.375504 7391645.321987, 339283.474543 7391643.301472, 339289.499822 7391632.224363, 339293.61682 7391624.655511, 339287.856062 7391621.52085, 339284.819854 7391619.849901, 339283.129676 7391622.964252, 339282.675141 7391623.801785, 339280.113922 7391628.521133, 339274.707503 7391638.483095, 339272.05454 7391643.371491, 339270.457411 7391646.314388, 339278.437978 7391650.623368, 339279.256409 7391651.056254))
POLYGON((339328.628779 7391643.652018, 339324.398045 7391641.363351, 339322.038033 7391645.683371, 339310.01731 7391667.687396, 339314.238657 7391670.032229, 339316.051101 7391666.709626, 339319.190366 7391660.954674, 339321.27978 7391657.124326, 339323.157066 7391653.682853, 339328.628779 7391643.652018))
POLYGON((339374.568446 7391654.673207, 339378.758524 7391652.424178, 339377.488583 7391650.053132, 339370.857976 7391637.673437, 339368.977998 7391634.163418, 339368.379575 7391633.046131, 339364.608089 7391635.123367, 339362.545075 7391636.197734, 339360.998084 7391637.003369, 339359.458082 7391638.01337, 339358.25808 7391639.053371, 339357.408079 7391639.873371, 339356.668077 7391643.633376, 339358.458079 7391645.873379, 339363.48115 7391648.621743, 339366.77357 7391650.420439, 339369.631314 7391651.981666, 339372.621277 7391653.611683, 339374.568446 7391654.673207))
POLYGON((339245.337594 7391672.994081, 339251.434035 7391660.501458, 339252.832571 7391657.635626, 339256.731889 7391649.645276, 339260.88073 7391641.143611, 339242.445386 7391631.18974, 339236.358771 7391643.483767, 339226.387344 7391663.624516, 339236.36559 7391668.558057, 339245.337594 7391672.994081))
POLYGON((339345.539182 7391631.400763, 339341.776631 7391622.903986, 339339.795756 7391618.473851, 339338.909817 7391616.492489, 339337.183689 7391612.63208, 339336.196467 7391610.424204, 339335.418578 7391608.684491, 339333.857078 7391609.521532, 339332.038896 7391610.496169, 339330.121376 7391626.79506, 339330.050935 7391627.393801, 339329.697556 7391630.397522, 339331.940096 7391631.651616, 339332.607729 7391631.967136, 339333.302411 7391632.220249, 339334.008241 7391632.441579, 339334.723724 7391632.630659, 339335.447345 7391632.787087, 339336.177572 7391632.910533, 339336.912858 7391633.000736, 339337.651648 7391633.057503, 339338.392376 7391633.080716, 339339.133474 7391633.070325, 339339.873373 7391633.026352, 339340.610506 7391632.94889, 339341.527311 7391632.801685, 339342.070241 7391632.694225, 339342.778782 7391632.483273, 339345.539182 7391631.400763))
POLYGON((339269.158185 7391645.612891, 339270.457411 7391646.314388, 339272.05454 7391643.371491, 339274.707503 7391638.483095, 339280.113922 7391628.521133, 339282.675141 7391623.801785, 339283.129676 7391622.964252, 339284.819854 7391619.849901, 339274.36854 7391614.091183, 339273.066244 7391619.062384, 339272.182833 7391622.434589, 339270.351498 7391629.425262, 339270.071456 7391630.494253, 339266.488471 7391644.171422, 339269.158185 7391645.612891))
POLYGON((339296.868015 7391660.383355, 339300.027383 7391654.713032, 339301.190816 7391652.624943, 339302.439346 7391650.384125, 339307.102077 7391642.015619, 339309.845443 7391637.091921, 339311.390204 7391634.319439, 339301.647443 7391629.024359, 339292.222049 7391645.835651, 339290.288803 7391649.283821, 339288.926974 7391651.712803, 339286.999041 7391655.151496, 339287.228276 7391655.272769, 339296.457124 7391660.155117, 339296.868015 7391660.383355))
POLYGON((339357.66806 7391728.533483, 339360.978064 7391730.173486, 339371.878888 7391710.113868, 339375.459772 7391703.52435, 339377.846385 7391699.132523, 339387.884077 7391680.661234, 339389.888111 7391676.973425, 339386.428107 7391675.083422, 339377.618093 7391691.15344, 339371.949654 7391701.774299, 339357.66806 7391728.533483))
POLYGON((339298.446288 7391719.026958, 339297.347986 7391721.293461, 339285.157265 7391745.884489, 339292.010873 7391749.300595, 339292.550974 7391749.663495, 339293.586875 7391750.167396, 339306.519997 7391723.117265, 339298.446288 7391719.026958))
Here I'm getting only the column with the polygons because that's the only thing I want inside this database.
My problem starts when I try to convert this data to GeoJSON, I know that I can use the ogr2ogr
to convert CSV to GeoJSON, but that would require a specific syntax and only using ogr2ogr -f GeoJSON file.geojson file.csv
wouldn't work. I also know that gdal has a VRT
file that is supposed to be used with CSV when I want to convert them to GeoJSON, so I can specify the meaning of the columns as described on this question. However, right now I'm not even sure if the VRT
file can do something about the SpatiaLite column syntax of representing polygons as POLYGON((339380.3182 7391671.753232, 339374.934121 7391681.671268, 339374.280746...))
, or if I need to do it myself before using this VRT
file. Also, I'm not sure if I'm overcomplicating the problem since the only thing I want is to get a GeoJSON file as a result from a query that I make on my SpatiLite database.
Is there any easier way of transforming the results of a SpatiaLite query into GeoJSON? Or transforming a SpatiaLite query on CSV, and then converting this CSV file to GeoJSON using a VRT
file is the way to go?
ogr2ogr a.json input.csv -oo X_POSSIBLE_NAMES="X*" -oo Y_POSSIBLE_NAMES="Y*" -oo KEEP_GEOM_COLUMNS=NO
should work. Shapefiles do in fact have indices, but you must create them. QGIS can do it, so can ogrinfo viaogrinfo -sql 'CREATE SPATIAL INDEX ON "layername"' shapefile.shp