Kabeja is an open source JAVA library for dxf parsing.
Details of the Kabeja project may be found here --> http://kabeja.sourceforge.net/
Now we are going to create a java servlet that gets a dxf file and translate it into GML.
First you need to download the kabeja.jar file and reference it within your project. You may download it form here --> http://sourceforge.net/projects/kabeja/?source=typ_redirect. Zip file contains the kabeja.jar and other goodies of the project. For our example to work kabeja.jar should be just enough.
If you are using maven (no need to download kabeja.jar) the following entry within pom.xml should be enough -->
<dependency>
<groupId>org.kabeja</groupId>
<artifactId>kabeja</artifactId>
<version>0.4</version>
</dependency>
Servlet CODE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | package freeopengis.servlets; import java.awt.Color; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Iterator; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.kabeja.dxf.DXFColor; import org.kabeja.dxf.DXFDocument; import org.kabeja.dxf.DXFEntity; import org.kabeja.dxf.DXFLayer; import org.kabeja.dxf.DXFLine; import org.kabeja.dxf.DXFPoint; import org.kabeja.dxf.DXFPolyline; import org.kabeja.dxf.DXFText; import org.kabeja.dxf.DXFVertex; import org.kabeja.parser.ParseException; import org.kabeja.parser.Parser; import org.kabeja.parser.ParserBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class dxfParser extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=UTF-8"; private static Logger logger = LoggerFactory.getLogger(dxfParser.class); //REPLACE THE EPSG CODE WITH THE ONE CORESPONDS TO YOUR DXF FILE private static final String EPSG_CODE = "3857"; private static final long serialVersionUID = 1L; public void init(ServletConfig config) throws ServletException { super.init(config); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); //DECLARE THE LOCATION OF THE DXF FILE //THIS MAY BE SUPPLIED AS A PARAMETER TO THE SERVLET String filLoc = "C:/projects/DXFS/TEST1.dxf"; logger.debug(filLoc); PrintWriter out = response.getWriter(); String dxfParsingString = ""; Parser parser = ParserBuilder.createDefaultParser(); StringBuilder buffer = new StringBuilder(); buffer.append("<wfs:FeatureCollection xmlns:wfs=\"http://www.opengis.net/wfs\">"); try { InputStream in = null; in = new FileInputStream(filLoc); //PARSE THE SUPPLIED DXF FILE parser.parse(in, "UTF-8"); //get the documnet and its layers DXFDocument doc = parser.getDocument(); Iterator lyrsIt = doc.getDXFLayerIterator(); int counter = 0; //Loop through layers found while (lyrsIt.hasNext()) { DXFLayer layer = (DXFLayer) lyrsIt.next(); System.out.println("layername="+layer.getName()); String lyrname = layer.getName(); int lyrcolor = layer.getColor(); String rgbcolor = DXFColor.getRGBString(lyrcolor); String [] cols = rgbcolor.split(","); //convert the layer color to hex color code Color mycolor = new Color(Integer.parseInt(cols[0]),Integer.parseInt(cols[1]),Integer.parseInt(cols[2])); String hex = "#"+Integer.toHexString(mycolor.getRGB()).substring(2); Iterator lyrTypesExist = layer.getDXFEntityTypeIterator(); while (lyrTypesExist.hasNext()) { String type = (String) lyrTypesExist.next(); Iterator entities = layer.getDXFEntities(type).iterator(); //loop through layer entities while (entities.hasNext()) { DXFEntity entity = (DXFEntity) entities.next(); String entType = entity.getType(); System.out.println("entType found="+entType); if (entType.equals("LWPOLYLINE") || entType.equals("POLYLINE")) { //System.out.println("LWPOLYLINE FOUND IN LAYER "+lyrname); counter = counter+1; DXFPolyline poly = (DXFPolyline) entity; int vertCount = poly.getVertexCount(); //System.out.println("VERTS COUNT="+vertCount); buffer.append("<gml:featureMember xmlns:gml=\"http://www.opengis.net/gml\">"); buffer.append("<feature:features xmlns:feature=\"http://mapserver.gis.umn.edu/mapserver\" >"); buffer.append("<feature:geometry>"); buffer.append("<gml:LineString srsName=\"http://www.opengis.net/def/crs/EPSG/0/"+EPSG_CODE+"\"><gml:coordinates xmlns:gml=\"http://www.opengis.net/gml\" decimal=\".\" cs=\",\" ts=\"\">"); for (int i=0;i<vertCount;i++) { DXFVertex vertex = poly.getVertex(i); double vertX = vertex.getPoint().getX(); double vertY = vertex.getPoint().getY(); buffer.append(vertX); buffer.append(","); buffer.append(vertY); buffer.append(" "); } buffer.append("</gml:coordinates></gml:LineString>"); buffer.append("</feature:geometry>"); buffer.append("<feature:id>"); buffer.append(counter); buffer.append("</feature:id>"); buffer.append("<feature:layername>"); buffer.append(lyrname); buffer.append("</feature:layername>"); buffer.append("<feature:type>"); buffer.append(entType); buffer.append("</feature:type>"); buffer.append("<feature:label>"); buffer.append(" "); buffer.append("</feature:label>"); buffer.append("<feature:color>"); buffer.append(hex); buffer.append("</feature:color>"); buffer.append("</feature:features>"); buffer.append("</gml:featureMember>"); } if ( entType.equals("LINE")) { counter = counter+1; //System.out.println("LINE FOUND"); DXFLine line = (DXFLine) entity; double vertX1 = line.getStartPoint().getX(); double vertY1 = line.getStartPoint().getY(); double vertX2 = line.getEndPoint().getX(); double vertY2 = line.getEndPoint().getY(); buffer.append("<gml:featureMember xmlns:gml=\"http://www.opengis.net/gml\">"); buffer.append("<feature:features xmlns:feature=\"http://mapserver.gis.umn.edu/mapserver\" >"); buffer.append("<feature:geometry>"); buffer.append("<gml:LineString srsName=\"http://www.opengis.net/def/crs/EPSG/0/"+EPSG_CODE+"\"><gml:coordinates xmlns:gml=\"http://www.opengis.net/gml\" decimal=\".\" cs=\",\" ts=\"\">"); buffer.append(vertX1); buffer.append(","); buffer.append(vertY1); buffer.append(" "); buffer.append(vertX2); buffer.append(","); buffer.append(vertY2); buffer.append(" "); buffer.append("</gml:coordinates></gml:LineString>"); buffer.append("</feature:geometry>"); buffer.append("<feature:id>"); buffer.append(counter); buffer.append("</feature:id>"); buffer.append("<feature:layername>"); buffer.append(lyrname); buffer.append("</feature:layername>"); buffer.append("<feature:type>"); buffer.append(entType); buffer.append("</feature:type>"); buffer.append("<feature:label>"); buffer.append(" "); buffer.append("</feature:label>"); buffer.append("<feature:color>"); buffer.append(hex); buffer.append("</feature:color>"); buffer.append("</feature:features>"); buffer.append("</gml:featureMember>"); } if ( entType.equals("POINT")) { counter = counter+1; //System.out.println("POINT FOUND"); DXFPoint point = (DXFPoint) entity; double pointX = point.getX(); double pointY = point.getY(); buffer.append("<gml:featureMember xmlns:gml=\"http://www.opengis.net/gml\">"); buffer.append("<feature:features xmlns:feature=\"http://mapserver.gis.umn.edu/mapserver\" >"); buffer.append("<feature:geometry>"); buffer.append("<gml:Point srsName=\"http://www.opengis.net/def/crs/EPSG/0/"+EPSG_CODE+"\"><gml:coordinates xmlns:gml=\"http://www.opengis.net/gml\" decimal=\".\" cs=\",\" ts=\"\">"); buffer.append(pointX); buffer.append(","); buffer.append(pointY); buffer.append(" "); buffer.append("</gml:coordinates></gml:Point>"); buffer.append("</feature:geometry>"); buffer.append("<feature:id>"); buffer.append(counter); buffer.append("</feature:id>"); buffer.append("<feature:layername>"); buffer.append(lyrname); buffer.append("</feature:layername>"); buffer.append("<feature:type>"); buffer.append(entType); buffer.append("</feature:type>"); buffer.append("<feature:label>"); buffer.append(" "); buffer.append("</feature:label>"); buffer.append("<feature:color>"); buffer.append(hex); buffer.append("</feature:color>"); buffer.append("</feature:features>"); buffer.append("</gml:featureMember>"); } if ( entType.equals("TEXT")) { counter = counter+1; //System.out.println("TEXT FOUND"); DXFText text = (DXFText) entity; double vertX = text.getInsertPoint().getX(); double vertY = text.getInsertPoint().getY(); String label = text.getText().replace("\"", " "); buffer.append("<gml:featureMember xmlns:gml=\"http://www.opengis.net/gml\">"); buffer.append("<feature:features xmlns:feature=\"http://mapserver.gis.umn.edu/mapserver\" >"); buffer.append("<feature:geometry>"); buffer.append("<gml:Point srsName=\"http://www.opengis.net/def/crs/EPSG/0/"+EPSG_CODE+"\"><gml:coordinates xmlns:gml=\"http://www.opengis.net/gml\" decimal=\".\" cs=\",\" ts=\"\">"); buffer.append(vertX); buffer.append(","); buffer.append(vertY); buffer.append(" "); buffer.append("</gml:coordinates></gml:Point>"); buffer.append("</feature:geometry>"); buffer.append("<feature:id>"); buffer.append(counter); buffer.append("</feature:id>"); buffer.append("<feature:layername>"); buffer.append(lyrname); buffer.append("</feature:layername>"); buffer.append("<feature:type>"); buffer.append(entType); buffer.append("</feature:type>"); buffer.append("<feature:label>"); buffer.append(label); buffer.append("</feature:label>"); buffer.append("<feature:color>"); buffer.append(hex); buffer.append("</feature:color>"); buffer.append("</feature:features>"); buffer.append("</gml:featureMember>"); } } } } buffer.append("</wfs:FeatureCollection>"); } catch (ParseException e) { e.printStackTrace(); } dxfParsingString = buffer.toString(); buffer.setLength(0); //flush it out.println(dxfParsingString); out.close(); } } |
good work. thanks man
ReplyDelete