upstream-extended/iuc/tools/geopandas/table2geojson.xml
1<tool id="geopandas_table2geojson" name="Table to GeoJSON" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@" license="MIT">
2 <description></description>
3 <macros>
4 <token name="@TOOL_VERSION@">1.2.0</token>
5 <token name="@VERSION_SUFFIX@">0</token>
6 <token name="@PROFILE@">24.2</token>
7 </macros>
8 <requirements>
9 <requirement type="package" version="@TOOL_VERSION@">geopandas</requirement>
10 </requirements>
11 <command detect_errors="exit_code"><![CDATA[
12 python '$csv2geojson'
13 ]]></command>
14 <configfiles>
15 <configfile name="csv2geojson">
16import json
17import pandas as pd
18import geopandas as gpd
19
20df = pd.read_csv('$infile', encoding="utf-8", sep=None, engine='python', header=None if '$has_header' == "false" else 0)
21
22inc = [int(x)-1 for x in str('$include_columns').split(",") if x.strip().isdigit()] if '$include_columns' and str('$include_columns').lower() != "none" else []
23exc = [int(x)-1 for x in str('$exclude_columns').split(",") if x.strip().isdigit()] if '$exclude_columns' and str('$exclude_columns').lower() != "none" else []
24
25property_df = df
26
27if inc:
28 property_df = df.iloc[:, inc]
29if exc:
30 property_df = property_df.drop(columns=df.columns[exc])
31
32gdf = gpd.GeoDataFrame(
33 property_df,
34 geometry=gpd.points_from_xy(
35 df.iloc[:, int($lat_column)-1],
36 df.iloc[:, int($long_column)-1]),
37 crs="$crs"
38)
39
40# drop_id: bool, default: False
41data = json.loads(gdf.to_json())
42with open('outfile.geojson', "w", encoding="utf8") as f:
43 ## Minify output json
44 f.write(json.dumps(data, indent=4))
45 </configfile>
46 </configfiles>
47 <inputs>
48 <param name="infile" type="data" format="csv,tabular,tsv" label="The file you want to convert"/>
49 <param name="has_header" type="boolean" truevalue="true" falsevalue="false" label="First row contains column names (header)?" help="Does your input file consists of a header"/>
50 <param name="lat_column" type="integer" value="1" label="Latitude column"/>
51 <param name="long_column" type="integer" value="2" label="Longitude column"/>
52 <param name="include_columns" type="data_column" data_ref="infile" use_header_names="true" multiple="true" optional="true"
53 label="Include the following columns" help="Either include or exclude columns, by default, all columns will be taken over into the properties field"/>
54 <param name="exclude_columns" type="data_column" data_ref="infile" use_header_names="true" multiple="true" optional="true"
55 label="Exclude the following columns" help="Either include or exclude columns, by default, all columns will be taken over into the properties field"/>
56 <param name="crs" type="select" label="Coordinate Reference System" help="Choose the Coordinate Reference Systems (CRS) your data is using">
57 <option value="EPSG:3857">Web Mercator projection</option>
58 <option value="EPSG:4326" selected="true">WGS84 projection (used by Google Earth)</option>
59 </param>
60
61 </inputs>
62 <outputs>
63 <data name="outfile" format="geojson" from_work_dir="outfile.geojson" label="${tool.name} on ${on_string}" />
64 </outputs>
65 <tests>
66 <test expect_num_outputs="1">
67 <param name="infile" value="geolocation.csv"/>
68 <param name="has_header" value="true"/>
69 <param name="lat_column" value="1"/>
70 <param name="long_column" value="2"/>
71 <param name="crs" value="EPSG:3857"/>
72 <output name="outfile" file="geolocation_1.geojson">
73 <assert_contents>
74 <has_json_property_with_text property="type" text="FeatureCollection"/>
75 <!-- all 5 columns are includes as properties, nothing excluded, nothing explicitly included -->
76 <has_text text='value": 10'/>
77 <has_text text='temperature": 23'/>
78 <has_text text='longitude'/>
79 <has_text text='latitude'/>
80 <has_text text='timestamp": "2023-01-01T12:05:00Z"'/>
81 <has_n_lines n="371"/>
82 </assert_contents>
83 </output>
84 </test>
85 <test expect_num_outputs="1">
86 <param name="infile" value="geolocation_without_header.csv"/>
87 <param name="has_header" value="false"/>
88 <param name="lat_column" value="1"/>
89 <param name="long_column" value="2"/>
90 <param name="crs" value="EPSG:3857"/>
91 <output name="outfile">
92 <assert_contents>
93 <has_json_property_with_text property="type" text="FeatureCollection"/>
94 <has_n_lines n="371"/>
95 </assert_contents>
96 </output>
97 </test>
98 <test expect_num_outputs="1">
99 <param name="infile" value="geolocation.csv"/>
100 <param name="has_header" value="true"/>
101 <param name="lat_column" value="1"/>
102 <param name="long_column" value="2"/>
103 <param name="include_columns" value="3,4"/>
104 <param name="crs" value="EPSG:3857"/>
105 <output name="outfile" ftype="geojson" file="geolocation_2.geojson">
106 <assert_contents>
107 <!-- 2 columns are explicitly included as properties -->
108 <has_text text='timestamp": "2023-01-01T12:05:00Z"'/>
109 <has_text text='value": 10'/>
110 <has_text text="FeatureCollection"/>
111 <has_n_lines n="311"/>
112 </assert_contents>
113 </output>
114 </test>
115 <test expect_num_outputs="1">
116 <param name="infile" value="geolocation.csv"/>
117 <param name="has_header" value="true"/>
118 <param name="lat_column" value="1"/>
119 <param name="long_column" value="2"/>
120 <param name="exclude_columns" value="5"/>
121 <param name="crs" value="EPSG:3857"/>
122 <output name="outfile" ftype="geojson" file="geolocation_3.geojson">
123 <assert_contents>
124 <has_json_property_with_text property="type" text="FeatureCollection"/>
125 <!-- 4 columns are includes as properties ... one is excluded -->
126 <has_text text='value": 10'/>
127 <has_text text='longitude'/>
128 <has_text text='latitude'/>
129 <has_text text='timestamp": "2023-01-01T12:05:00Z"'/>
130 <has_n_lines n="351"/>
131 </assert_contents>
132 </output>
133 </test>
134
135 </tests>
136 <help format="markdown"><.
145
146## What is GeoJSON?
147
148GeoJSON is a widely-used format for encoding geographic data structures using JSON (JavaScript Object Notation).
149A GeoJSON file contains an array of geographic features, where each feature includes:
150* A geometry field describing the spatial shape (e.g., point, line, polygon),
151* A properties field containing non-spatial attributes.
152
153GeoJSON uses the WGS84 coordinate system by default (EPSG:4326), with coordinates expressed in decimal degrees.
154To explore and edit GeoJSON interactively, visit [geojson.io](https://geojson.io), select a location or shape, and inspect the resulting GeoJSON output.
155
156๐ Pro Tip, Galaxy can visualise GeoJSON files.
157
158 ]]></help>
159 <citations>
160 <citation type="doi">10.5281/zenodo.3946761</citation>
161 </citations>
162</tool>